Using Elastic IP addresses with Amazon EC2 with AWS SDK for PHP Version 3 - AWS SDK for PHP

Using Elastic IP addresses with Amazon EC2 with AWS SDK for PHP Version 3

An Elastic IP address is a static IP address designed for dynamic cloud computing. An Elastic IP address is associated with your AWS account. It’s a public IP address, which is reachable from the internet. If your instance does not have a public IP address, you can associate an Elastic IP address with your instance to enable communication with the internet.

The following examples show how to:

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

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials. Then import the AWS SDK for PHP, as described in Basic usage.

Describe 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' ]); $result = $ec2Client->describeInstances(); echo "Instances: \n"; foreach ($result['Reservations'] as $reservation) { foreach ($reservation['Instances'] as $instance) { echo "InstanceId: {$instance['InstanceId']} - {$instance['State']['Name']} \n"; } }

Allocate and associate an address

Imports

require 'vendor/autoload.php';

Sample Code

$ec2Client = new Aws\Ec2\Ec2Client([ 'region' => 'us-west-2', 'version' => '2016-11-15', 'profile' => 'default' ]); $instanceId = 'InstanceID'; $allocation = $ec2Client->allocateAddress(array( 'DryRun' => false, 'Domain' => 'vpc', )); $result = $ec2Client->associateAddress(array( 'DryRun' => false, 'InstanceId' => $instanceId, 'AllocationId' => $allocation->get('AllocationId') )); var_dump($result);

Release an address

Imports

require 'vendor/autoload.php';

Sample Code

$ec2Client = new Aws\Ec2\Ec2Client([ 'region' => 'us-west-2', 'version' => '2016-11-15', 'profile' => 'default' ]); $associationID = 'AssociationID'; $allocationID = 'AllocationID'; $result = $ec2Client->disassociateAddress([ 'AssociationId' => $associationID, ]); $result = $ec2Client->releaseAddress([ 'AllocationId' => $allocationID, ]); var_dump($result);