AWS, Cloud Computing, Tutorials

3 Mins Read

Auto Attach Elastic IP to EC2 Classic Instance For Autoscaling

Voiced by Amazon Polly


AWS has made it really simple to attach an Elastic IP to an EC2 instance. It is 4 Simple Steps using AWS console,

  1. Login into your AWS Management Console
  2. Go to EC2 Management -> Elastic IPs
  3. Allocate a new Elastic IP (Either Standard or VPC)
  4. Attach the EIP to the instance

Though AWS discourages using Elastic IPs exclusively for every instance, there could be use cases where you might need Elastic IP attached to each of your instances. This gets very tedious to manage when you have autoscaling enabled and you have instances starting based on load. The four simple steps are not simple anymore if you have to perform them for instances that are starting automatically. So much for autoscaling on aws, you now might have to constantly monitor for new instances.

As a first step, I would be describing how EC2 API could be used to automate attaching elastic IP to instances. As you might have already guessed, we are looking at attaching an Elastic IP to an instance during boot. I assume you already have aws global command line tools installed.

Interestingly, this process also is of four simple steps but the purpose is to automate the first four steps. The below steps have been tested on an Amazon Linux instance.

  1. Script to Allocate and Attach EIP
  2. Place the script on the instance with permissions to execute
  3. Call the script during Instance Boot
  4. Take an AMI and include in autoscaling policy



Step 1: Script to Allocate and Attach Elastic IP

 (say filename: auto-attach-eip)

  •  Allocate a new EIP and store it in a variable (say allocated_eip)               
  •  Get the instance ID of the current instance from its metadata             
  •  Associate allocated_eip to instance_id

Note: The above statements could also be placed in “User Data” field while creating a new instance as part of launch configuration. In that case, all you have to do is take an AMI of the new instance and use in the autoscaling group.

Helping organizations transform their IT infrastructure with top-notch Cloud Computing services

  • Cloud Migration
  • Devops
  • AIML & IoT
Know More

Step 2: Place the script on the instance with permissions to execute

  • Copy the script to the instance
  • SSH into the instance
  • Update file permissions

Step 3: Call the script during Instance Boot

  • Open /etc/rc.local using your favourite editor (I prefer vim)
  •  Add the below command at the end of the file    

You can look into /tmp/attach-output.txt in case of any issues to see the error(if any)

Step 4: Take an AMI and include in your Autoscaling Policy


  • Login to your AWS management Console
  • Take an AMI of the instance where the script is updated
  • Update your autoscaling policy with the new AMI ID.

Note 1: Start a new instance with the AMI before updating autoscaling policy to check if things are working fine.

Note 2: Procedure to update autoscaling policy would be a different discussion altogether.

Now that you are attaching the elastic IP to the instance on boot, you also need to detach and deallocate the elastic IP when the instance is shutting down(not reboot). Note that AWS bills you for unattached EIPs.

A similar script could be written with commands to detach and deallocate the EIP of the instance before it shuts down. That would be my next blog post. So stay tuned and keep checking the blog. Till then keep calm and architect on AWS.

Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.

  • Cloud Training
  • Customized Training
  • Experiential Learning
Read More




  1. Manjunatha T N

    Feb 8, 2019


    Excellent solution!! thanks for your support.

  2. Jaime Gonzalez

    Jul 13, 2017


    Hi, great info…i adjusted to my need.But i don’t know where to find from aws.

    • Jaime Gonzalez

      Jul 13, 2017


      identity_file where is locate as default?? or i have to download from aws?

  3. Chris

    Nov 17, 2016


    Great stuff! Thanks so much for this info! I adjusted this to pick a set IP and assign that one EIP to a new instance if it is launched effectively adding the new server to the required whitelist. Do you have any ideas for selecting from a Pool of EIPs perhaps using aws ec2 describe-addresses to work out which one isn’t in use?

  4. Click to Comment