Home > Amazon EC2 > 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 http://169.254.169.254/latest/meta-data/instance-id`
# 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/

References:
https://www.linkedin.com/groups/Assigning-Elastic-IP-Auto-Scaled-3684105.S.209039689
http://stackoverflow.com/questions/12973777/how-to-run-a-shell-script-at-startup



Flag Counter