Using a lifecycle hook, acquire the same Private Static IP that is always attached to an EC2 instance in an Auto scaling group.

 A primary private IPv4 address for the instance is assigned when you launch an EC2 instance. An instance is assigned a primary private IP address from the subnet's IPv4 address range. When an instance is stopped and started, or hibernated and started, a private IPv4 address is associated with the network interface. When the instance is terminated, the private IP address is released, and a new instance is assigned a new private IP address.

What if your use case necessitates keeping a certain Private IP address for an EC2 instance after it has been released on termination? For example, a certain static private IP 10.8.16.34/32 must be associated to an EC2 instance at all times, regardless of termination.

One alternative is to utilise an Elastic IP address, however because EIP assigns a public IP address, it won't work if you're constructing infrastructure in a company where the public cloud platform is heavily limited and controlled, such as a bank, because of its Internet reachability.

This solution makes use of AWS's Secondary ENI (Elastic Network Interface), as well as lifecycle hooks, Lambda, and parameter stores.



If you're not sure how the lifecycle hook works, check out this link.



Instance states that scale automatically

The key problem in building this solution was ensuring that the Secondary ENI is automatically disconnected from the terminated EC2 instance and associated with the new EC2 instance, even if numerous EC2 instances are terminated.

Another issue was managing the ENIs, which was solved with the help of the parameter store.

Let's pretend there are three EC2 instances in an auto scaling group, and the auto scaling group's targeted capacity is three. All EC2 instances have been given the tag key:name=value:peer1, peer2, etc. as a pre-requisite, and a secondary ENI has been assigned to each of them.

The following is an example of how the solutions work:

Consider the case where an EC2 instance labelled as 'peer 1' goes down or is terminated.

The lifecycle hook is activated, and EC2 Auto scaling sends an event to EventBridge for a lifecycle action. (Notice that in the 'Lifecycle transition' option, 'Instance terminate' is selected.)




3. The next step is to receive the event in the EventBridge, which calls a lambda function that detaches the secondary ENI from the terminating EC2 instance and saves the tag and second ENI to the parameter store.

4. The Lambda function reads the event data, retrieves the tag key/value and secondary ENI value, stores them in a parameter store, and completes the lifecycle action.
5. When auto scaling creates a new EC2 instance (which enters the pending:wait state), another lifecycle hook is triggered, and EC2 Auto scaling sends an event to EventBridge for a lifecycle action. (Notice that the 'Instance launch' option is selected in the 'Lifecycle transition' box this time.)


6. EventBridge rule to receive the launch lifecycle hook event, which calls another lambda function to retrieve the saved tag key/value and secondary ENI from the parameter store, attach them to the newly created EC2 instance, and complete the lifecycle hook action.

When multiple EC2 terminations occur, the launch lambda function retrieves the tag and secondary ENI pair that was stored first in the parameter store. Think of it as a FIFO queue:). Once the tag and secondary ENI pair have been successfully associated with the EC2 instance, the lambda function deletes them from the parameter store.
You can not only automatically dissociate and reattach the secondary ENI to an EC2 instance in the auto scaling group but also use the same secondary ENI by applying this solution.
I hope you found my blog useful!
Good luck with your studies!



Comments