Bootstrap FreeKB - Amazon Web Services (AWS) - Modify Route 53 DNS Records using the AWS CLI
Amazon Web Services (AWS) - Modify Route 53 DNS Records using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

Route 53 is Amazon Web Services DNS. For example, Route 53 can be used to create an A record to associate a hostname such as www.example.com to a public IP address.

In Route 53, a Hosted Zone contains the DNS records.The aws route53 list-hosted-zones command can be used to list the Hosted Zones that have been created.

~]$ aws route53 list-hosted-zones
{
    "HostedZones": [
        {
            "Id": "/hostedzone/Z056866DJM1OE9C45GH42",
            "Name": "example.com.",
            "CallerReference": "RISWorkflow-RD:98abdc50-5adf-1234-abdc-471041234a6c",
            "Config": {
                "Comment": "HostedZone created by Route53 Registrar",
                "PrivateZone": false
            },
            "ResourceRecordSetCount": 3
        }
    ]
}

 

Then the aws route53 list-resource-record-sets command can be used to list the DNS records in the Hosted Zone.

~]$ aws route53 list-resource-record-sets --hosted-zone-id Z056866DJM1OE9C45GH42 
{
    "ResourceRecordSets": [
        {
            "Name": "example.com.",
            "Type": "NS",
            "TTL": 172800,
            "ResourceRecords": [
                {
                    "Value": "ns-550.awsdns-04.net."
                },
                {
                    "Value": "ns-499.awsdns-62.com."
                },
                {
                    "Value": "ns-1687.awsdns-18.co.uk."
                },
                {
                    "Value": "ns-1193.awsdns-21.org."
                }
            ]
        },
        {
            "Name": "example.com.",
            "Type": "SOA",
            "TTL": 900,
            "ResourceRecords": [
                {
                    "Value": "ns-550.awsdns-04.net. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"
                }
            ]
        },
        {
            "Name": "foo.example.com.",
            "Type": "A",
            "TTL": 300,
            "ResourceRecords": [
                {
                    "Value": "114.50.19.198"
                }
            ]
        }
    ]
}

 

The aws route53 change-resource-record-sets command can be used to modify the DNS records.

  • UPSERT to modify a record
  • CREATE to create a new record
  • DELETE to delete a record

Record Type

  • A Record - To route a request to an IPv4 address (for example, requesting www.example.com will forward a request onto 10.11.12.13)
  • AAAA (record) - To route a request to an IPV6 address
  • Alias Record 
    • route requests from DNS name "a" to DNS name "b" (for example, from foo.example.com to bar.example.com in the same Hosted Zone)
    • route requests onto some other AWS service (such as a Network Load Balancer, Cloudfront, an S3 Bucket, et cetera)
  • CNAME (canonical name) - To route a request from DNS name "a" in Hosted Zone to DNS name "b" (for example, from www.example.com to www.sample.com)

The aws route53 change-resource-record-sets command uses JSON for the record you want to update, something like this.

{
    "Comment": "update A record",
    "Changes": [
        {
            "Action": "UPSERT",
            "ResourceRecordSet": {
                "Name": "foo.exanple.com",
                "Type": "A",
                "TTL": 300,
                "ResourceRecords": [
                    {
                        "Value": "114.50.19.199"
                    }
                ]
            }
        }
    ]
}

 

Here is an example of how you could create an A Record that forwards requests from www.example.com to 10.11.12.13.

aws route53 change-resource-record-sets \
--hosted-zone-id Z056866DJM1OE9C45GH42 \
--change-batch '{ "Changes": [ { "Action": "CREATE", "ResourceRecordSet": { "Name": "www.example.com", "Type": "CNAME", "TTL": 300, "ResourceRecords": [ { "Value": "10.11.12.13" } ] } } ] }'

 

Here is an example of how you could create an Alias Record that forwards requests from bar.example.com to foo.example.com.

aws route53 change-resource-record-sets \
--hosted-zone-id Z056866DJM1OE9C45GH42 \
--change-batch '{ "Changes": [ { "Action": "CREATE", "ResourceRecordSet": { "AliasTarget": { "HostedZoneId": "Z056866DJM1OE9C45GH42", "EvaluateTargetHealth": true, "DNSName": "foo.example.com." }, "Type": "A", "Name": "bar.example.com." } } ] }'

 

Here is an example of how you could create a CNAME record that forwards requests from foo.sample.com to www.example.com.

aws route53 change-resource-record-sets \
--hosted-zone-id Z056866DJM1OE9C45GH42 \
--change-batch '{ "Changes": [ { "Action": "CREATE", "ResourceRecordSet": { "Name": "foo.sample.com", "Type": "CNAME", "TTL": 300, "ResourceRecords": [ { "Value": "www.example.com" } ] } } ] }'

 

When creating/updating/deleting a TXT record, wrap the value in double quotes, which will need to be escaped in your JSON file.

{
    "Comment": "create TXT record",
    "Changes": [
        {
            "Action": "CREATE",
            "ResourceRecordSet": {
                "Name": "my-txt-record",
                "Type": "TXT",
                "TTL": 60,
                "ResourceRecords": [
                    {
                        "Value": "\"example\""
                    }
                ]
            }
        }
    ]
}

 

The aws route53 change-resource-record-sets command can be used to modify the DNS records. In this example, file:// is used to point to a JSON file.

~]$ aws route53 change-resource-record-sets --hosted-zone-id Z056866DJM1OE9C45GH42 --change-batch file:///path/to/example.json
{
    "ChangeInfo": {
        "Id": "/change/C00766383LL9ATV76GQFK",
        "Status": "PENDING",
        "SubmittedAt": "2023-05-17T01:21:28.663000+00:00",
        "Comment": "Update A record"
    }
}

 

Or, you can include the JSON on the command line.

aws route53 change-resource-record-sets \
--hosted-zone-id Z056866DJM1OE9C45GH42 \
--change-batch '{ "Changes": [ { "Action": "CREATE", "ResourceRecordSet": { "Name": "txt.example.com", "Type": "TXT", "TTL": 300, "ResourceRecords": [ { "Value": "my-value" } ] } } ] }'

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 2035e1 in the box below so that we can be sure you are a human.