This Node.js code example shows:
- How to retrieve descriptions of your Elastic IP addresses.
- How to allocate and release an Elastic IP address.
- How to associate an Elastic IP address with an Amazon EC2 instance.
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:
- http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#describeAddresses-property
- http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#allocateAddress-property
- http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#associateAddress-property
- http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#releaseAddress-property
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.
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 shared configurations file with your user credentials. For more information about providing a shared credentials file, see Loading Credentials in Node.js from the Shared Credentials 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.
Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2
.
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'us-west-2'});
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.
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// 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.
node ec2_describeaddresses.js
This sample code can be found here on GitHub.
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.
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// 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.
node ec2_allocateaddress.js
This sample code can be found here on GitHub.
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.
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// 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.
node ec2_releaseaddress.js
This sample code can be found here on GitHub.