Menu
AWS SDK for JavaScript
Developer Guide for SDK version 2.122.0

Using Elastic IP Addresses in Amazon EC2


                                            Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to:

  • Retrieve descriptions of your Elastic IP addresses

  • Allocate and release an Elastic IP address

  • Associate an Elastic IP address with an Amazon EC2 instance

The Scenario

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 is 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.

In this example, you use a series of Node.js modules to perform several Amazon EC2 operations involving Elastic IP addresses. The Node.js modules use the SDK for JavaScript to manage Elastic IP addresses by using these methods of the Amazon EC2 client class:

For more information about Elastic IP addresses in Amazon EC2, see Elastic IP Addresses in the Amazon EC2 User Guide for Linux Instances or Elastic IP Addresses in the Amazon EC2 User Guide for Windows Instances.

Prerequisite Tasks

To set up and run this example, first complete these tasks:

  • Install Node.js. For more information about installing Node.js, see the Node.js website.

  • Create a JSON file named config.json with your credentials and the region setting. For more information about providing your credentials by using a JSON file, see Loading Credentials in Node.js from a JSON File.

  • Create an Amazon EC2 instance. For more information about creating Amazon EC2 instances, see Amazon EC2 Instances in the Amazon EC2 User Guide for Linux Instances or Amazon EC2 Instances in the Amazon EC2 User Guide for Windows Instances.

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object, setting the region, and providing credentials for your code. In this example, you use the JSON file you created to provide the credentials.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json');

Describing Elastic IP Addresses

Create a Node.js module with the file name ec2_describeaddresses.js. Be sure to configure the SDK as previously shown. To access Amazon EC2, create an AWS.EC2 service object. Create a JSON object to pass as parameters, filtering the addresses returned by those in your VPC. To retrieve descriptions of all your Elastic IP addresses, omit a filter from the parameters JSON. Then call the describeAddresses method of the Amazon EC2 service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // Create EC2 service object var ec2 = new AWS.EC2({apiVersion: '2016-11-15'}); var params = { Filters: [ {Name: 'domain', Values: ['vpc']} ] }; // Retrieve Elastic IP address descriptions ec2.describeAddresses(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", JSON.stringify(data.Addresses)); } });

To run the example, type the following at the command line.

Copy
node ec2_describeaddresses.js

This sample code can be found here on GitHub.

Allocating and Associating an Elastic IP Address with an Amazon EC2 Instance

Create a Node.js module with the file name ec2_allocateaddress.js. Be sure to configure the SDK as previously shown. To access Amazon EC2, create an AWS.EC2 service object. Create a JSON object for the parameters used to allocate an Elastic IP address, which in this case specifies the Domain is a VPC. Call the allocateAddress method of the Amazon EC2 service object.

Upon success, the data parameter to the callback function has an AllocationId property that identifies the allocated Elastic IP address.

Create a JSON object for the parameters used to associate an Elastic IP address to an Amazon EC2 instance, including the AllocationId from the newly allocated address and the InstanceId of the Amazon EC2 instance. Then call the associateAddresses method of the Amazon EC2 service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // Create EC2 service object var ec2 = new AWS.EC2({apiVersion: '2016-11-15'}); var paramsAllocateAddress = { Domain: 'vpc' }; // Allocate the Elastic IP address ec2.allocateAddress(paramsAllocateAddress, function(err, data) { if (err) { console.log("Address Not Allocated", err); } else { console.log("Address allocated:", data.AllocationId); var paramsAssociateAddress = { AllocationId: data.AllocationId, InstanceId: 'INSTANCE_ID' }; // Associate the new Elastic IP address with an EC2 instance ec2.associateAddress(paramsAssociateAddress, function(err, data) { if (err) { console.log("Address Not Associated", err); } else { console.log("Address associated:", data.AssociationId); } }); } });

To run the example, type the following at the command line.

Copy
node ec2_allocateaddress.js

This sample code can be found here on GitHub.

Releasing an Elastic IP Address

Create a Node.js module with the file name ec2_releaseaddress.js. Be sure to configure the SDK as previously shown. To access Amazon EC2, create an AWS.EC2 service object. Create a JSON object for the parameters used to release an Elastic IP address, which in this case specifies the AllocationId for the Elastic IP address. Releasing an Elastic IP address also disassociates it from any Amazon EC2 instance. Call the releaseAddress method of the Amazon EC2 service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // Create EC2 service object var ec2 = new AWS.EC2({apiVersion: '2016-11-15'}); var paramsReleaseAddress = { AllocationId: 'ALLOCATION_ID' }; // Disassociate the Elastic IP address from EC2 instance ec2.releaseAddress(paramsReleaseAddress, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Address released"); } });

To run the example, type the following at the command line.

Copy
node ec2_releaseaddress.js

This sample code can be found here on GitHub.