We have an EC2 running at Amazon. It has a Public IP and Public DNS.
We use the Public IP in our domain records by creating an A record for www.example.com. Then the site becomes accessible. If we were to use the Public DNS, we would use CNAME instead of an A record.
My question is on the technicality and functionality of both. Does one have an advantage over the other?
Best Answer
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
(nowww
) 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:
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.
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.