AWS makes it easy to attach an Elastic IP on an EC2 instance. It takes only 4 steps using the AWS console
Log in to your AWS Management Console
Go to EC2 Management -> ElasticIPs
Allocate a new Elastic IP either Standard or VPC
Attach the EIP to your instance
Although AWS discourages you from using Elastic IPs for every instance, there may be situations where you need Elastic IP attached each of your instances. This can be very cumbersome if you have autoscaling enabled, and instances start based on load. These four steps are no longer simple if you need to do them for instances that start automatically. You might now have to monitor for new instances, so much for autoscaling on AWS.
In this article, I will first describe how EC2 API can be used to automate attaching elastic IPs to instances. As you may have guessed, we will be attaching an Elastic IP during boot. I assume that you have already installed aws global command-line tools.
This process is actually four simple steps, but the purpose of it is to automate four of the four. These steps were tested on an Amazon Linux instance.
Script to Allocate EIP and Attach
Place the script on the instance that has the permissions to execute
Call the script during Instance boot
Include an AMI in your autoscaling policy.
Step 1: Script To Allocate And Attach Elastic IP (say filename : auto-attach eip).

Assign a new EIP to a variable (e.g. allocated_eip).

perl -lne ‘print $& if /(\d+\. )3\d+/’)1allocated_eip=$(aws ec2 allocate-address –output table | perl -lne ‘print $& if /(\d+\. )3\d+/’)
From its metadata, get the instance ID for the current instance

instance_id=$(curl -s$(curl -s
Associate allocated_eip to instance_id

aws Ec2 associate Address –instance_id $instance_id -public-ip $allocated_eip1Aws Ec2 Associate Address –instance_id $instance_id -public-ip $allocated_eip1Note: These statements can also be placed in the “User Data” field when creating a new instance for launch configuration. In this case, you will only need to take the AMI of the new instance.
Step 2: Add the script to the instance with the appropriate permissions
Copy the script to the instance

scp -i /auto-attach-eip @:/usr/bin/.1scp -i /auto-attach-eip @:/usr/bin/.
Instance SSH
Update file permissions

sudo chmod +x /usr/bin/auto-attach-eip1sudo chmod +x /usr/bin/auto-attach-eipStep 3: Call the script during Instance Boot
Use your favorite editor (I prefer vim) to open /etc/rc.local

vim /etc/rc.local1vim /etc/rc.local
Add the following command to the end of the file

Shell/usr/bin/auto-attach-eip > /tmp/attach-output.txt1/usr/bin/auto-attach-eip > /tmp/attach-output.txtYou can look into /tmp/attach-output.txt in case of any issues to see the error(if any)
Step 4: Get an AMI and include it in your Autoscaling policy
Login to your AWS management console
Take an AMI of the instance in which the script was updated
The new AMI ID will allow you to update your autoscaling policies.
Note 1: Create a new instance of the AMI before you update autoscaling policies to see if things are working.