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.)
Comments
Post a Comment