Voiced by Amazon Polly |
Introduction
AWS has made it really simple to attach an Elastic IP to an EC2 instance. It is 4 Simple Steps using AWS console,
- Login into your AWS Management Console
- Go to EC2 Management -> Elastic IPs
- Allocate a new Elastic IP (Either Standard or VPC)
- 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.
- Script to Allocate and Attach EIP
- Place the script on the instance with permissions to execute
- Call the script during Instance Boot
- Take an AMI and include in autoscaling policy
Customized Cloud Solutions to Drive your Business Success
- Cloud Migration
- Devops
- AIML & IoT
Step 1: Script to Allocate and Attach Elastic IP
(say filename: auto-attach-eip)
- Set AWS_ACCESS_KEY, AWS_SECRET_KEY and AWS_DEFAULT_REGION
1 2 3 |
export AWS_ACCESS_KEY_ID= <Your Access Key> export AWS_SECRET_ACCESS_KEY= <Your Secret Key> export AWS_DEFAULT_REGION= <preferred aws region> |
- Allocate a new EIP and store it in a variable (say allocated_eip)
1 |
allocated_eip=$(aws ec2 allocate-address --output table | perl -lne 'print $& if /(\d+\.){3}\d+/') |
- Get the instance ID of the current instance from its metadata
1 |
instance_id=$(curl -s https://169.254.169.254/latest/meta-data/instance-id) |
- Associate allocated_eip to instance_id
1 |
aws ec2 associate-address --instance-id $instance_id --public-ip $allocated_eip |
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.
Step 2: Place the script on the instance with permissions to execute
- Copy the script to the instance
1 |
scp -i <identity_file> <path-to>/auto-attach-eip <user>@<host_name>:/usr/bin/. |
- SSH into the instance
- Update file permissions
1 |
sudo chmod +x /usr/bin/auto-attach-eip |
Step 3: Call the script during Instance Boot
- Open /etc/rc.local using your favourite editor (I prefer vim)
1 |
vim /etc/rc.local |
- Add the below command at the end of the file
1 |
/usr/bin/auto-attach-eip > /tmp/attach-output.txt |
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
WRITTEN BY CloudThat
CloudThat is a leading provider of cloud training and consulting services, empowering individuals and organizations to leverage the full potential of cloud computing. With a commitment to delivering cutting-edge expertise, CloudThat equips professionals with the skills needed to thrive in the digital era.
Manjunatha T N
Feb 8, 2019
Excellent solution!! thanks for your support.
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?
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?
Click to Comment