EC2: is an instance's public DNS stable? Can I rely on it not changing?

I've launched my first instance, and am using it as a web server. I see that it has a public DNS (a public URL), e.g.:

ec2-123-45-6-789.compute-1.amazonaws.com

I can successfully go to this server in my browser, hit it via cURL, etc.

I want to use this web server for a back-end service in an app I'm building, so I placed this URL in my app's config, and it works great.

But when I manually stop and re-start my instance, I see that the public DNS changes! I've read that this happens when you explicitly stop and re-start, but doesn't happen if you just "reboot".

I don't plan on explicitly stopping and re-starting this server ever, but my question is: will this public DNS ever change on its own for any reason? E.g. if the machine abnormally crashes, or whatever.

In other words, is it safe to ship an app that's wired to this URL?


Solution 1:

The public DNS name always matches the public IP address.

The public IP address stays the same for an instance until it is terminated or stopped. A reboot does not change the public IP address.

If an EC2 instance is in a VPC, then it will retain the same public IP address across a stop and start.

If an EC2 instance that is not in a VPC is stopped and then started started again, it will probably receive a different public IP address.

Instances can fail. When you start a new instance to replace a failed or terminated instance, it will probably receive a different public IP address.

Because instances can fail, and because you may want to change the size of an instance (with a stop/start) it is not recommended to "ship an app that's wired to [the public IP address]" (or DNS name). Once your instance is stopped/terminated/failed another user could get that IP address assigned to their instance and all your traffic would go to them.

It is recommended to use Elastic IP Addresses to associate public services with your instance. You get to keep the Elastic IP address and you can assign it to any instance you want over time, even if it's the same instance after a stop/start.

Each Elastic IP Address comes with a public DNS name, but you would probably be better off mapping your own hostname to the Elastic IP address so that the name makes more sense to humans.

Here's a guide to Elastic IP Addresses:

http://aws.amazon.com/articles/1346

Here's an article I wrote that talks about the differences between rebooting and stop/start of an instance:

Rebooting vs. Stop/Start of Amazon EC2 Instance
http://alestic.com/2011/09/ec2-reboot-stop-start

Here's an article I wrote that provides a reason you may want to stop/start an instance even though you don't think you will today:

Moving an EC2 Instance to a Larger Size
http://alestic.com/2011/02/ec2-change-type