5

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?

  • You cannot use a CNAME record as nameserver cloaking (that is to say, have custom nameservers like ns1.yourdomain.com) – td512 May 21 '16 at 11:25
  • If you are using route53, you can use an alias record instead (which is like a CNAME that gets returned as an A). – jordanm May 21 '16 at 14:55
  • @jordanm Alias records only work for CloudFront, ELB, and ELB targets, not EC2 instances directly. I think this question only refers to the "compute" DNS hostname of the instance's public IP. – Michael - sqlbot May 21 '16 at 15:22
5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.