Menu
AWS SDK for PHP
Developer Guide

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 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 Use of the AWS SDK for PHP version 3.

Describe an Instance

Imports

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

Sample Code

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

Allocate and Associate an Address

Imports

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

Sample Code

$ec2Client = new 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'; use Aws\Ec2\Ec2Client;

Sample Code

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