How to Automate Elastic IP Assignment on EC2 Instance Restart or Reboot

In AWS, users are charged for allocated Elastic IPs that are not associated with a running instance or network interface (VPC). Therefore, the accepted best practice is to keep only those IP addresses that will be needed in the future. Allocated Elastic IPs you don’t plan to use in the future, or those you just forgot to release, may contribute to unexpectedly high AWS bills.

Newvem tracks the usage of your allocated Elastic IPs and identifies those that haven’t been in use for a significant period of time. This information is gathered by analyzing your Elastic IP usage patterns, in particular their association or disassociation activity. Based on current benchmarks, Newvem recommends the release of an Elastic IP if it has not been used for more than a week.

One way to avoid having unattached Elastic IPs is to automate the assignment process in your EC2 Instance.

How to Automate Elastic IP Assignment on EC2 Instance Restart or Reboot

Elastic IP addresses are static IP addresses designed for elastic cloud computing. An elastic IP (AWS EIP)  address helps to achieve high availability as it belongs to the user’s AWS account and is not bound to a particular instance. For an EBS backed instance, when it is stopped and started again, the Public DNS assigned to the instance will change. The elastic IP helps to solve the problem of keeping the same public IP for that instance. The user has to manually assign an elastic IP when the instance is started.

The present guide demonstrates how to automate the IP assignment on an instance restart.

Check the following related guides to get started with Amazon Elastic IP (EIP):

1. Launch an EBS backed instance. The public DNS of the instance is marked for the reference.

2. Allocate a new elastic IP address.


[Newvem continuously tracks and analyzes your Elastic IPS usage and identifies unattached EIPs that are draining your resources. Create a Free Account]


3. Login to the instance and create a script file, as show below. Follow the steps given below:

  1. Export the environment variables, such as EC2_HOME & JAVA_HOME. Generally the above mentioned variables are set in the AWS Linux environment. Run the command ‘set’ to know the values of the variables.
  2. Declare the variables for the region, the AWS keys (access key as well secret access key) and assign values for them. Define the variable for the elastic IP and assign a value from step#2.
  3. Get the instance ID from the AWS EC2 instance metadata.
  4. Run the command ec2-assign-address with values, such as the region, instance ID, elastic IP, and the keys.
  5. Save the script and change the mode of the script to execute with the command “chmod”.

The script file can be seen here, for reference:

#!bin/sh
# # This script assigns an Elastic IP to Instance on Reboot or Restart
# Export Java Home and EC2 Home. If you are not sure where Java is installed or EC2 tools are installed run command 'set' to find the values.
export EC2_HOME='/opt/aws/apitools/ec2';
export JAVA_HOME='/usr/lib/jvm/jre';
# Set the variables for Instance
# Region in Which instance is running
EC2_REGION='us-west-2'
# Access Key of the User
AWS_ACCESS_KEY='AXXXXXXXXXXXXXXQ'
#Secret Access Key of the user
AWS_SECRET_ACCESS_KEY='JXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXa'
#Elastic IP Which will be assigned to Instance
Elastic_IP='54.245.229.138'
#Instance ID captured through Instance meta data
InstanceID=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id`
echo "Assigning Elastic IP to Instance"
/opt/aws/apitools/ec2/bin/ec2-associate-address -O $AWS_ACCESS_KEY -W $AWS_SECRET_ACCESS_KEY -i $InstanceID --region $EC2_REGION $Elastic_IP

The actual output of all the commands is shown below.

4. Setup the script to run on instance restart or reboot.

5. There are various ways to run a script on Linux boot. One of the ways is explained below. Select any one which suits the user’s requirement.

  1. Edit the file at /etc/rc.local with the command “vi /etc/rc.local”.
  2. Add the current path of assignIP.sh along with the file name at the end.
  3. rc.local is executed at the end when all the bootup scripts are completed.
  4. The actual output is shown below.

6. Reboot / Stop –Start the instance.

7. The instance will be rebooted and an elastic IP will be assigned.

8. If the user creates an AMI from this instance and when the new instance is launched from the AMI, it will have the Elastic IP 54.245.229.138 automatically attached to it. It is advisable to remove the script before creating an AMI as the script has AWS credentials.

Leave a comment

Leave your opinion

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: