EC2 client

This page contains examples with the EC2 client. See the client introduction for a more detailed description how to use a client. You may also want to consider the authentication documentation to understand the many ways you can authenticate with AWS.

The EC2 package could be installed with Composer.

composer require async-aws/ec2

A new client object may be instantiated by:

use AsyncAws\EC2\EC2Client; $eC2 = new EC2Client();

The authentication parameters is read from the environment by default. You can also specify a AWS access id and secret:

use AsyncAws\EC2\EC2Client; $eC2 = new EC2Client([ 'accessKeyId' => 'my_access_key', 'accessKeySecret' => 'my_access_secret', 'region' => 'eu-central-1', ]);

For all available options, see the configuration reference.

Usage

Describe images

use AsyncAws\Ec2\Ec2Client; use AsyncAws\Ec2\ValueObject\Filter; $ec2 = new Ec2Client(); $result = $ec2->describeImages([ 'Owners' => ['self'], 'Filters' => [ new Filter(['Name' => 'state', 'Values' => ['available']]), ], ]); foreach ($result as $image) { echo $image->getImageId() . ' ' . $image->getCreationDate() . PHP_EOL; foreach ($image->getBlockDeviceMappings() as $bdm) { if (null !== $ebs = $bdm->getEbs()) { echo ' snapshot: ' . $ebs->getSnapshotId() . PHP_EOL; } } }

Deregister an image

use AsyncAws\Ec2\Ec2Client; $ec2 = new Ec2Client(); $ec2->deregisterImage([ 'ImageId' => 'ami-0abcdef1234567890', ]);

Delete a snapshot

use AsyncAws\Ec2\Ec2Client; $ec2 = new Ec2Client(); $ec2->deleteSnapshot([ 'SnapshotId' => 'snap-0abcdef1234567890', ]);

The source code to this page is found on GitHub.