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:
#!/bin/bash
date
export EC2_HOME=/opt/aws/apitools/ec2-1.4.4.2
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'}`
host=`hostname`
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
fi
fi
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.
Hi,
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?
Comment by random — July 21, 2012 @ 9:35 am
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 .
Comment by admin — July 27, 2012 @ 11:19 am
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?
Comment by random — August 16, 2012 @ 7:47 am
Hello
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 .
Comment by admin — August 16, 2012 @ 10:34 am
Hai,i have seen your code but where and how can we use this code..
my requirement is to attach an static eip to instance in autoscaling group without load balancer,,if possible can you give some detailed explanation,so that it helps me a lot.
Comment by Anoosh — February 19, 2020 @ 7:17 pm
Hello
That is a old script. And don’t have time to check if still work . Basically all that line should be put into one file and made to be executable. Should work on autoscaling group also .
Is not related to load balancer or other things.
Basically It find first free ip , then add it to the host . This should be input into the images for those autoscalling machines
Comment by admin — April 3, 2020 @ 12:59 pm