I'm trying to setup an auto-healing EC2 instance using an Auto Scaling Group and a user-data startup script. If the current server has an issue where it is no longer reachable, the instance should terminate and a new one take its place. This is easy enough, but one requirement is proving difficult.
I need the replacement server to have the same private IP as the previous server. My thought is to have a secondary private IP (this is within a VPC) assigned to the original server, and then re-assign it to the new server.
I assume I can use the aws-cli installed during the user-data startup script to re-assign the private IP, but how do I know which server is being replaced and re-assign the IP from it (for example, if in the future the pool of servers is larger and 2 happen to go down at the same time).
If the original server is being terminated, am I going to be able to re-assign the private IP at all?
After a lot of research and trial/error, here's what we ended up doing:
We've had this running for a few days now and it seems to be working quite well (though that remains to be seen when an instance actually fails for something other than us terminating it directly for testing).
AWS recently announced the "Auto-Recovery for EC2 Instances" feature. As far as I understand it this works basically the same as an auto-scaling group, but preserves instance IP and volumes etc.
At the moment this feature is only available for newer instances and only in us-east region.