Amazon EC2 Public IP vs. Public DNS
In many cases, there is no reason to use a CNAME. You have a single public IP address, so point an A record at your IP address, to avoid the second lookup that a CNAME requires. Also, for example.com
(no www
) the A-record is your only option when pointing directly to an EC2 instance that isn't using Elastic Load Balancer (ELB) or CloudFront as a front-end.
Exception:
For a hostname inside your domain, like www
, it can be useful to use the CNAME, if you want instances in your AWS account (and in the same region) to be able to access the instance using the external hostname but the internal IP address. The EC2 infrastructure does this automatically when you use a CNAME.
For example:
www.example.com CNAME ec2-203-0-113-25.compute-1.amazonaws.com.
If queried outside of your EC2 region and account, www.example.com would return the public IP of the instance, 203.0.113.25.
If queried inside your EC2 region and account, www.example.com would return the private IP address of the instance, for example 172.31.1.20.
We resolve an external DNS hostname to the public IP address of the instance outside the network of the instance, and to the private IP address of the instance from within the network of the instance.
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html
This automatic mapping allows you to save on data transfer charges when internal machines access other internal machines using public hostnames. If that access uses a public IP, elastic IP, or NAT gateway, you'll pay extra for sending the traffic out and back in again, and this avoids that.