AWS SDK for PHP
Developer Guide

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

Managing Amazon EC2 Instances Using the AWS SDK for PHP Version 3

The following examples show how to:

All the example code for the AWS SDK for PHP Version 3 is available here on GitHub.

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials for the AWS SDK for PHP Version 3. Then import the AWS SDK for PHP, as described in Basic Usage Patterns of the AWS SDK for PHP Version 3.

Describe Instances

Imports

require 'vendor/autoload.php'; use Aws\Ec2\Ec2Client;

Sample Code

$ec2Client = new Aws\Ec2\Ec2Client([ 'region' => 'us-west-2', 'version' => '2016-11-15', 'profile' => 'default' ]); $result = $ec2Client->describeInstances(); var_dump($result);

Enable and Disable Monitoring

Imports

require 'vendor/autoload.php'; use Aws\Ec2\Ec2Client;

Sample Code

$ec2Client = new Aws\Ec2\Ec2Client([ 'region' => 'us-west-2', 'version' => '2016-11-15', 'profile' => 'default' ]); $instanceIds = array('InstanceID1', 'InstanceID2'); $monitorInstance = 'ON'; if ($monitorInstance == 'ON') { $result = $ec2Client->monitorInstances(array( 'InstanceIds' => $instanceIds )); } else { $result = $ec2Client->unmonitorInstances(array( 'InstanceIds' => $instanceIds )); } var_dump($result);

Start and Stop an Instance

Imports

require 'vendor/autoload.php'; use Aws\Ec2\Ec2Client;

Sample Code

$ec2Client = new Aws\Ec2\Ec2Client([ 'region' => 'us-west-2', 'version' => '2016-11-15', 'profile' => 'default' ]); $action = 'START'; $instanceIds = array('InstanceID1', 'InstanceID2'); if ($action == 'START') { $result = $ec2Client->startInstances(array( 'InstanceIds' => $instanceIds, )); } else { $result = $ec2Client->stopInstances(array( 'InstanceIds' => $instanceIds, )); } var_dump($result);

Reboot an Instance

Imports

require 'vendor/autoload.php'; use Aws\Ec2\Ec2Client;

Sample Code

$ec2Client = new Aws\Ec2\Ec2Client([ 'region' => 'us-west-2', 'version' => '2016-11-15', 'profile' => 'default' ]); $instanceIds = array('InstanceID1', 'InstanceID2'); $result = $ec2Client->rebootInstances(array( 'InstanceIds' => $instanceIds )); var_dump($result);