How to automatically assign Elastic Ip to AWS EC2 instances


Install aws-ec2-assign-elastic-ip on the target instance – make it part of the AMI.

pip install aws-ec2-assign-elastic-ip

Add the following script in user data section

/usr/local/bin/aws-ec2-assign-elastic-ip --access-key ACCESS_KEY --secret-key SECRET_KEY --valid-ips IP


For those who are interested in how this all works, lets spent some time understanding the different terminologies.

So what is Elastic Ip and why do we need one?

As per Amazon’s documentation –

An Elastic IP address is a static IPv4 address designed for dynamic cloud computing. An Elastic IP address is associated with your AWS account. With an Elastic IP address, you can mask the failure of an instance or software by rapidly remapping the address to another instance in your account.

An Elastic IP address is a public IPv4 address, which is reachable from the Internet. If your instance does not have a public IPv4 address, you can associate an Elastic IP address with your instance to enable communication with the Internet; for example, to connect to your instance from your local computer.

So an Elastic IP is part of the Amazon’s account and can be assigned at will to EC2 instances. One of the use cases where I needed an Elastic Ip was hosting an application on EC2 Spot Instances. As you can imagine there is no guarantee for how long the spot instance will remain alive, a new instance would have a new IP assigned to it making it really difficult to share the connection credentials to customer.

There were two solutions to the problem

  1. Use a service like DynDNS
  2. Use Elastic IP – a static IP that I can assign to the new Spot Instance that comes up when my bid is same or above the current spot instance pricing.

So I configured everything and got stuck at how can I assign the Elastic IP address to my instance automatically (or as soon as the machine is up) After some searching I came across a python module named aws-ec2-assign-elastic-ip, it is a command line idempotent utility that can assign an Elastic IP from the passed in pool of IPs to the machine running the command.

This utility can easily be installed using pip

pip install aws-ec2-assign-elastic-ip

We are going to use the user data field, which is available in advanced configurations of EC2 instance to put in a cloud-init script.


Here is the script that does the trick

/usr/local/bin/aws-ec2-assign-elastic-ip --access-key ACCESS_KEY --secret-key SECRET_KEY --valid-ips IP

Notice the full path to the utility, this makes the script more robust as it doesn’t matter whether it is part of PATH or not.

How to automatically assign Elastic Ip to AWS EC2 instances

Leave a Reply

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

You are commenting using your 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