Elastic IP in an Auto-Scaling Group
Co-Author: Ajay Patil
In this post, we will be explaining about, how to attach an Elastic-IP to an Instance in an Auto-scaling Group in Amazon Web Services.
Before getting into the actual topic, let’s have a brief intro about “What is an Elastic IP?”
As per the definition from Amazon Web Services, “An Elastic IP address is a static IPv4 address designed for dynamic cloud computing. With an Elastic IP address, you can mask the failure of an instance or software by rapidly remapping the address to another instance in your account.”
Consider that you wanted to allow SSH Connectivity from your data center to one of your EC2 Instance in AWS. The network admin of your data-center will now require a static public IP Address to allow connectivity from your data center to your EC2 Instance. If my public IP address keeps changing rapidly, everytime (As it happens on Reboot), the Network admin has to replace the set of firewall rules to establish connectivity to the new IP. To overcome this, what can be done?
We can create an ASG for your EC2 Instance with Min & Max capacity as 1. And by making use of Elastic IP(Basically a Public IPV4), we can overcome the above mentioned issue.
This post assumes that you have already created a Launch Template, AMI, Auto-Scaling Group and Launched an EC2 Instance from the Auto Scaling Group.
Now navigate to the Elastic IP Tab in AWS Console. Click Allocate New Address and choose the option as Amazon Pool. A new public Elastic IP will be assigned to your AWS account from the Amazon’s pool of IP Address.
Consider that the address 188.8.131.52 gets associated with your account, after performing this action. Make note of the Association ID whose format looks similar to the one as “ eipalloc-003ghw584bc102270”.
Now navigate to the Instance where you wanted to associate this Elastic IP. Click on “Create Template from Instance” and choose the option as “Create New Template Version from an Instance”. Select the Launch Template and scroll down to the User Data of Advance Details Section towards the end.
Modify the IP address and Allocation Id as per your requirement.
The Scripts in User data Section gets executed whenever the Instance Reboots including when an Instance gets launched. Taking advantage of this, we inform the Instance to Dis-associate the elastic IP forcefully, even if it is associated with an other instance. The second command associates the Elastic IP address with your Instance.
Click on Create Template.
All Done? Probably not. Will Proceed with Verification.
Now terminate this running Instance. Wait patiently until the Auto Scaling Group launches a new Instance. Now navigate to the description section of the new Instance. Notice that the Elastic IP will now be associated with the new Instance.
This IP will be detached from the Old terminated Instance and will remain associated with the new Instance. This procedure gets repeated whenever a new Instance gets launched.
Do let us know, in case of any discrepancies !!!