How do I create alias resource record sets in Route 53 using the AWS CLI?

Last updated: 2022-08-10

How do I create alias resource record sets in Amazon Route 53 using the AWS Command Line Interface (AWS CLI)?

Short description

You can use an alias record in Route 53 to point to an AWS resource or an Amazon Simple Storage Service (Amazon S3) bucket. You can create an alias record through the Route 53 console or through the AWS CLI.

Resolution

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version.

Before you create alias resource record sets, create a hosted zone to contain the records for routing traffic to your domain. The hosted zone and your domain must have the same name. Depending on your use case, there are two choices. You can create a public hosted zone for routing Internet traffic. Or, you can also create a private hosted zone for routing traffic within an Amazon Virtual Private Cloud (Amazon VPC).

Create your alias resource record set

Modify the following sample JSON syntax to create your own alias resource record set, and then specify your own values for the alias records. Save the file (for example, sample.json).

Warning: Be sure that you use the hosted zone ID of your AWS resources, and not your domain name, in your configuration. In the JSON file, this value is specified in the key-value pair HostedZoneId. Before proceeding, locate the HostedZoneId for Elastic Load Balancing, AWS Elastic Beanstalk, Amazon S3, and Amazon CloudFront endpoints for each Region.

This example creates the alias resource record set for a domain ( elb.example.com) to point to a load balancer endpoint ( ALB-xxxxxxxx.us-west-2.elb.amazonaws.com). The request body includes a list of change items, known as a change batch. Change items are included with a ChangeResourceRecordSetsRequest element.

{
  "Comment": "Creating Alias resource record sets in Route 53",
  "Changes": [
    {
      "Action": "CREATE",
      "ResourceRecordSet": {
        "Name": "elb.example.com",
        "Type": "A",
        "AliasTarget": {
          "HostedZoneId": "Z1H1FL5HABSF5",
          "DNSName": "ALB-xxxxxxxx.us-west-2.elb.amazonaws.com",
          "EvaluateTargetHealth": false
        }
      }
    }
  ]
}

Next, use a change-resource-record-sets command to create your resource record set in a hosted zone. The values for record creation are specified in the JSON configuration file that you created previously.

Modify the following command for your configuration. For the --hosted-zone-id, provide your hosted zone ID for the domain name:

$ aws route53 change-resource-record-sets --hosted-zone-id ZXXXXXXXXXX --change-batch file://sample.json

Elastic Load Balancing settings

If you're pointing to a load balancer, always include dualstack in the value for the DNSName key-value pair of the JSON file. For example, if your load balancer's Amazon-provided DNS name is ALB-xxxxxxxx.us-west-2.elb.amazonaws.com, use:

"DNSName": "dualstack.ALB-xxxxxxxx.us-west-2.elb.amazonaws.com"

CloudFront distribution settings

If you're pointing to a CloudFront distribution, specify the Amazon-provided CloudFront distribution domain name in the DNSName key-value pair of the JSON file. For example, if your CloudFront distribution Amazon-provided domain name is d111111abcdef8.cloudfront.net, use:

"DNSName": "d111111abcdef8.cloudfront.net"

Warning: You must include an alternate domain name to use instead of the CloudFront-assigned one. The alternate domain name must match the domain for which you created the alias resource record set. For example, if you want to access your CloudFront distribution using the domain example.com, add the domain to the Alternate Domain Names for the distribution.

Amazon S3 bucket settings

If pointing to an S3 bucket, specify the domain name of the bucket's website endpoint for the DNSName key-value pair in the JSON file. Don't use the S3 endpoint that appears in the static website hosting from your S3 management console for the value. The domain name for which you create the resource record set must match the name of your Amazon S3 bucket.

You must also specify the Region where the bucket is hosted (for example, us-east-1).

s3-website-us-east-1.amazonaws.com

The following is example JSON syntax for pointing to an S3 bucket:

{
  "Comment": "Alias record for S3 bucket",
  "Changes": [
    {
      "Action": "CREATE",
      "ResourceRecordSet": {
        "Name": "new.example.com",
        "Type": "A",
        "AliasTarget": {
          "HostedZoneId": "Z3BJ6K6RIION7M",
          "DNSName": "s3-website-us-west-2.amazonaws.com",
          "EvaluateTargetHealth": false
        }
      }
    }
  ]
}

Note: In addition to creating Alias records pointing to the AWS resources such as CloudFront, Amazon S3 or ELB, you can create an Alias resource record. The Alias resource record points to another record in the same hosted zone that you’re creating the alias record in. The alias record must have the same type as the record you're routing to.