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

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:

# # 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
# Access Key of the User
#Secret Access Key of the user
#Elastic IP Which will be assigned to Instance
#Instance ID captured through Instance meta data
InstanceID=`/usr/bin/curl -s`
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 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 automatically attached to it. It is advisable to remove the script before creating an AMI as the script has AWS credentials.

Get Your Free Cloud Health Check

For Cloud Operators

You can take a deep breath now that you’ve automated your Elastic IP assignments for your EC2 Instances. But there is still more work to be done. It’s dangerous to just let automated processes run without a monitoring tool. That’s where Newvem comes in.

Newvem Cloud Care continuously tracks and analyzes your resources and utilization patterns, and provides a down-to-the-hour picture of your AWS consumption and usage behavior such as Elastic IP association (or disassociation) patterns. Newvem gives you the visibility you need to better understand, assess, and respond to vulnerabilities, abnormalities, and other hidden issues enabling you to:

  • Triage urgent cloud risks
  • Diagnose cost, risk, and governance issues
  • Track cloud cost and asset vitals

Newvem Cloud Care tracks your AWS cloud’s health

Keywords: Amazon AWS elastic cloud services, AWS Console, Cloud Utilization, Cloud Computing, EC2 CLI, Amazon Web Services, AWS EC2, Elastic IP, EC2 Instances, EIP, AMI, DNS

There are 2 comments .

Ashok Kumar —

I want to add automatic elastic ip address when new server launch via Auto-scaling.
Please give me any script.

Thanks in advance.


    taralshah99 —

     Hi Ashok,

    To clarify more, when you mean Launch New Instance, you mean launching it with your own AMI. The one possible solution is, while creating an AMI, you setup a script which will fetch the user data on startup as explained here
    Now in the user data, you pass the elastic IP, the user wants to assign.
    The script will fetch that IP and assign to the instance.

    Does this solve your purpose or are you looking for something else?


You must be to post a comment.

* As a bonus, you'll receive our weekly newsletter!

Hitchhiker's Guide to The Cloud

Newvem's eBook for Cloud Operations