Rolling Windows and Sticky Bytes

<tagline type="clever"/>

Automating Elastic IP Assignment on Amazon EC2

on 2012-07-12

For those unfamiliar, an Amazon Elastic IP is basically a static IP address for Amazon virtual machines (EC2 or VPC). It’s a dedicated IP for your instance so you don’t have to muck around with updating the IP address you want to connect to every time.

The only catch? If, like me, you shut down your dev machine when it’s not in use to avoid paying for idle CPU time, you will have noticed that the association between the instance and the Elastic IP evaporates on shutdown.

The solution in my case was to install the AWS API SDK on my instance (I put it into /usr/local/aws by hand, not realizing there’s a package for it in my Linux distro’s repository and was too lazy to go back and fix it), download my X.509 certificate to the instance and drop the following script into /etc/init.d:



# Set environment variables
export JAVA_HOME=/usr

export EC2_HOME=/usr/local/aws
export EC2_BIN=$EC2_HOME/bin

export EC2_PRIVATE_KEY=/root/.ec2/keyfile.pem
export EC2_CERT=/root/.ec2/certfile.pem

export EC2_URL=

# Associate the elastic IP with the instance
$EC2_BIN/ec2-associate-address -i i-deadbeef

The script sets up the API environment, then calls ec2-associate-address to associate my instance with its elastic IP. Once the script was installed, I added a call to it in /etc/rc.local and now whenever my instance boots up, it automatically associates itself with its elastic IP.

Of course there are lots of ways you could implement equivalent functionality (I saw another post on the EC2 forums where they used route53 for dynamic DNS updates instead of using an Elastic IP) but this was the way I decided to approach it and it works well.


Leave a Reply

Your email address will not be published. Required fields are marked *