TL;DR
Can I tell AWS EC2 to launch an instance from AMI but keep the host keys -- the ones in /etc/ssh
that were saved with the AMI -- instead of generating new keys and overwriting the ones saved with the AMI.
The full question:
On AWS, I'm setting up an EC2 instance to serve as a SFTP host. An automated system (managed by customer) will deliver files into there on regular basis. Those systems will authenticate via public keys provided by the customer.
In front of the EC2 instance there will be an elastic IP. This IP needs to remain constant "forever", since the automated system connecting to us relies on that constancy.
Behind the scenes, I would like to retain the ability to swap this EC2 instance with a different one at any point, and re-associate the existing IP address with the new server. (Some reasons to do this would be in order to launch a more powerful instance, or to consolidate various other servers into a single instance).
This kind of re-association needs to be transparent to the automated system that delivers the files. However, I expect that the automated system would freak out when I make such a switch, because it would detect that the host's fingerprint has changed. Maybe normally that's a good thing, but I believe in this case, since one host is supposed to replace another, it's appropriate to preserve the identity.
So, whenever I swap instances as described, I need to copy the original host's public key(s) into the new host. I actually half expected that this would happen automatically — if I use the same AMI to launch both hosts, but it appears that EC2 deliberately assigns new public keys to the new instance, overwriting whatever keys were saved with the AMI.
My question is what's the easiest way to achieve the desired behavior. I know how to do it manually, by copying the keys over from the old server onto the new server and overwriting the ones that were assigned to the new server. But I wonder if EC2 has an automated or idiomatic way to do this.