Sunday, November 20, 2011

Elastic IP Addresses

Each time the amazon instance is restarted it gets a different public DNS entry and IP address. In order to to get a permanent IP address associated with the instance I had to create what is known as an elastic IP address in AWS parlance. This required creating the elastic IP and associating with existing instance. Also the association of instance with elastic IP address needs to be done during startup of the instance:


  • Installed amazon api tools on the instance. I had to scp the zip file to the instance to proceed with the installation:

scp -i ~/aws/keys/thejus.pem ./aws/ec2-api-tools.zip ubuntu@ec2-107-22-175-254.compute-1.amazonaws.com:/home/ubuntu/.

  • Installed JDK 6 on the Amazon instance:

sudo apt-get install openjdk-6-jdk

  • Added these entries to .bashrc for the api variables to take effect:


export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
export EC2_HOME=/home/ubuntu/aws/ec2-api-tools-1.4.4.2


export PATH=$JAVA_HOME/bin:$EC2_HOME/bin:$PATH


export EC2_KEY_DIR=/home/ubuntu/aws/keys
export EC2_PRIVATE_KEY=${EC2_KEY_DIR}/pk-Y36R3VDKYF55PYKVXR3VFGSNCY7AQGLS.pem
export EC2_CERT=${EC2_KEY_DIR}/cert-Y36R3VDKYF55PYKVXR3VFGSNCY7AQGLS.pem


  • Quick check to verify that the changes are working:



ubuntu@domU-12-31-38-01-86-A5:~$ ec2-describe-regions
REGION  eu-west-1       ec2.eu-west-1.amazonaws.com
REGION  us-east-1       ec2.us-east-1.amazonaws.com
REGION  ap-northeast-1  ec2.ap-northeast-1.amazonaws.com
REGION  us-west-2       ec2.us-west-2.amazonaws.com
REGION  us-west-1       ec2.us-west-1.amazonaws.com
REGION  ap-southeast-1  ec2.ap-southeast-1.amazonaws.com

  • Create the auto-startup script for startup during boot up:
/etc/init.d$ vi ec2-startup
#!/bin/bash

export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
export EC2_HOME=/home/ubuntu/aws/ec2-api-tools-1.4.4.2
export EC2_KEY_DIR=/home/ubuntu/aws/keys
export EC2_PRIVATE_KEY=${EC2_KEY_DIR}/pk-Y36R3VDKYF55PYKVXR3VFGSNCY7AQGLS.pem
export EC2_CERT=${EC2_KEY_DIR}/cert-Y36R3VDKYF55PYKVXR3VFGSNCY7AQGLS.pem

export PATH=$JAVA_HOME/bin:$EC2_HOME/bin:$PATH

elastic_ip=107.22.175.254
instance_id=i-72fb9e12

start() {

        /bin/echo "associating elastic IP address..."
        ec2-associate-address $elastic_ip -i $instance_id
}


stop() {
        /bin/echo "disassocating elastip IP address..."
        ec2-disassociate-address $elastic_ip
}
case "$1" in
        start) 
                start
                ;;
        stop)  
                stop
                ;;
        restart)
                stop
                sleep 5
                start
                ;;
        *)
                echo "Usage: $SELF {start|stop|restart}"


esac


exit 0


  • Update ubuntu settings to execute script during startup:


/etc/init.d$ sudo update-rc.d ec2-startup defaults
update-rc.d: warning: /etc/init.d/ec2-startup missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
 Adding system startup for /etc/init.d/ec2-startup ...
   /etc/rc0.d/K20ec2-startup -> ../init.d/ec2-startup
   /etc/rc1.d/K20ec2-startup -> ../init.d/ec2-startup
   /etc/rc6.d/K20ec2-startup -> ../init.d/ec2-startup
   /etc/rc2.d/S20ec2-startup -> ../init.d/ec2-startup
   /etc/rc3.d/S20ec2-startup -> ../init.d/ec2-startup
   /etc/rc4.d/S20ec2-startup -> ../init.d/ec2-startup
   /etc/rc5.d/S20ec2-startup -> ../init.d/ec2-startup
  • Verify that the elastic IP address is associated with the instance with a restart of the ec2 instance:
$ ec2-stop-instances i-72fb9e12INSTANCE i-72fb9e12 running stopping

$ ec2-describe-instances
RESERVATION r-cedf27a0 880425318169 default
INSTANCE i-72fb9e12 ami-00b14b69 stopped thejus 0 t1.micro 2011-11-15T06:22:25+0000 us-east-1b aki-825ea7eb monitoring-disabled ebs paravirtual xen sg-d72124be default
BLOCKDEVICE /dev/sda1 vol-59c48233 2011-11-21T04:44:49.000Z
TAG instance i-72fb9e12 Name ubuntu-server

$ ec2-start-instances i-72fb9e12
INSTANCE i-72fb9e12 stopped pending

$ ec2-describe-instances
RESERVATION r-cedf27a0 880425318169 default
INSTANCE i-72fb9e12 ami-00b14b69 ec2-107-22-175-254.compute-1.amazonaws.com domU-12-31-38-04-22-9C.compute-1.internal running thejus 0 t1.micro 2011-11-21T04:46:13+0000 us-east-1b aki-825ea7eb monitoring-disabled 107.22.175.254 10.220.33.102 ebs paravirtual xen sg-d72124be default
BLOCKDEVICE /dev/sda1 vol-59c48233 2011-11-21T04:46:33.000Z
TAG instance i-72fb9e12 Name ubuntu-server


No comments:

Post a Comment