Automatically assign Elastic IP to ec2 instance

Step 1. Create an IAM role
AWS Service: Amazon EC2
Actions: AssociateAddress, DescribeInstances
Amazon Resource Name (ARN): *

Step 2. Create this file on your Linux
vim /etc/init.d/assign-eip

export AWS_ACCESS_KEY=(insert key here) 
export AWS_SECRET_KEY=(insert key here) 
# get EC2 isntance ID

instanceid=`/usr/bin/curl -s` 
# Associate EIP with the captured instance ID.

ec2-associate-address -i $instanceid--region ap-southeast-1 (insert EIP here)

Step 3. Change file permission

chmod +x /etc/init.d/assign-eip
ln -s /etc/init.d/assign-eip /etc/rc.d/

Step 4. Create a symlink to /etc/rc.d/

ln -s /etc/init.d/assign-eip /etc/rc.d/