Automatically assign an Elastic IP at Launch time

Today I have face a problem . We want to have all time same ip on our instances because of some rules on a firewall. So how to add automatically elastic ip on a ec2 instance when start automaticaly by load balancer ?
Well this is not possible to be done automaticaly using amazon, so we have to wrote a bash script to be run on start up.

Here is the script:

export EC2_HOME=/opt/aws/apitools/ec2-
export JAVA_HOME=/usr/lib/jvm/jre
export CLASSPATH=${EC2_HOME}/lib
export EC2_PRIVATE_KEY=/root/pk.pem
export EC2_CERT=/root/cert.pem
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin

ip=`/opt/aws/bin/ec2-describe-addresses -K /root/pk.pem -C /root/cert.pem |grep -v i-|head -1|awk {'print $2'}`
instance=`/opt/aws/bin/ec2-describe-instances -K /root/pk.pem -C /root/cert.pem  | grep $host |awk {'print $2'}`
runmore=`/opt/aws/bin/ec2-describe-addresses -K /root/pk.pem -C /root/cert.pem|grep $instance`

if [[ $runmore == '' ]];then
    if [[ $ip != '' ]];then
        /opt/aws/bin/ec2-associate-address -K /root/pk.pem -C /root/cert.pem -i $instance $ip

Now all you have to do is to add this script on /etc/rc.d/rc.local to be run on startup . If your instance have a elastic ip asigned will not do anything, else will assing first free elastic ip.

Rate this post

4 Responses to “ “Automatically assign an Elastic IP at Launch time”

  1. random says:

    How do you do this for a windows ami? Also if I do not have a load balancer, will the script work? And does this script just draw from a pool of elastic IP’s that you have already provisioned?

    • admin says:

      Well, sorry but I don’t have a windows ami to test it. I think this can be modify to run on windows also because they have .bat files also for windows from what I remember. If I will have time I will take a look to see, and try to addapt it for windows.
      Yes this script should run also without load balancer.
      And yes again, the script will take first free ip available from your pool .

  2. random says:

    Actually, I just realized because I am using a VPC, I cannot put this script into the instance, because the ec2 api tools won’t work without a public IP address, which is not automatically assigned to a VPC. So, essentially, I would need to do this from an outside machine. I have a mac, so using bash is fine, I just am not entirely sure how to have my newly launched VPC instances have elastic IP’s automatically associated with them. Do you have any advice on how to implement this?

  3. admin says:

    I am sorry but didn’t work with vpc, however basically that script what it do.
    /opt/aws/bin/ec2-describe-addresses -K /root/pk.pem -C /root/cert.pem |grep -v i-|head -1|awk {‘print $2’}
    this line request the first ip free
    /opt/aws/bin/ec2-describe-instances -K /root/pk.pem -C /root/cert.pem | grep $host |awk {‘print $2’}
    here I find out what id have the instance using hostname ( I use this because the main idea was that if the load balancer will launch automatically instanced to assign them ip from my pool ) . This line can be changed to use ip address I think is more ok .
    I mean :
    localip=’ifconfig eth0|grep ‘inet addr’ | awk {‘print $2’}|cut -d ‘:’ -f 2′
    and this change with
    instance=`/opt/aws/bin/ec2-describe-instances -K /root/pk.pem -C /root/cert.pem | grep $localip |awk {‘print $2’}`

    Here I check if this instance don’t have already a ip added
    runmore=`/opt/aws/bin/ec2-describe-addresses -K /root/pk.pem -C /root/cert.pem|grep $instance`
    And if not I add it
    /opt/aws/bin/ec2-associate-address -K /root/pk.pem -C /root/cert.pem -i $instance $ip

    Basically if you know the name or ip of the instance that you launch you may modify the script to work with your needs .

Leave a Reply

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