Sub topic 10
Automating record changes with the AWS CLI
This chapter assumes you have a public hosted zone, know the common record types, and have the AWS CLI configured with permission to call Route 53. You will learn how change batches work, how to apply them, and how to avoid the errors that come up most often.
Why use change batches
The console is fine for one-off edits. Scripts and pipelines need something repeatable. Route 53 records are changed through one API call, ChangeResourceRecordSets. It takes a change batch: a JSON document that lists the changes to make in a hosted zone.
All changes in one batch succeed or fail together. Route 53 applies them as a single atomic unit. This matters when you swap one record for another, because no resolver sees a half-finished state.
Anatomy of a change batch
A change batch has an optional Comment and a list of Changes. Each change has an Action and a ResourceRecordSet.
| Action | Behavior |
|---|---|
| `CREATE` | Adds the record. Fails if it already exists. |
| `UPSERT` | Creates the record if missing, otherwise replaces it. |
| `DELETE` | Removes the record. Every value must match the existing record exactly. |
UPSERT is the best default for automation because you can run it repeatedly with the same result.
This batch creates or updates the www A record with a 300-second TTL:
{
"Comment": "Create or update the www A record",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "www.example.com",
"Type": "A",
"TTL": 300,
"ResourceRecords": [{ "Value": "192.0.2.10" }]
}
}
]
}Save it as www-a-record.json and submit it:
aws route53 change-resource-record-sets \
--hosted-zone-id ZONE_ID \
--change-batch file://www-a-record.jsonReplace ZONE_ID with your zone's ID. You can find it with aws route53 list-hosted-zones. The file:// prefix tells the CLI to read the JSON from disk.
Checking that a change took effect
The command returns a ChangeInfo object with an Id and a Status of PENDING. The status becomes INSYNC once the change has reached all Route 53 name servers, usually within about a minute.
aws route53 get-change --id CHANGE_IDUse the ID from the previous output. Scripts can wait for completion with the built-in waiter:
aws route53 wait resource-record-sets-changed --id CHANGE_IDINSYNC only means Route 53 itself is updated. Resolvers that cached the old answer keep serving it until the old TTL expires.
Batching related changes
One batch can hold several changes, which is how you create weighted records. Each record in a set shares a name and type, and needs its own SetIdentifier. This batch sends about 90 percent of answers to one endpoint and 10 percent to the other:
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "app.example.com",
"Type": "A",
"SetIdentifier": "blue",
"Weight": 90,
"TTL": 60,
"ResourceRecords": [{ "Value": "192.0.2.20" }]
}
},
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "app.example.com",
"Type": "A",
"SetIdentifier": "green",
"Weight": 10,
"TTL": 60,
"ResourceRecords": [{ "Value": "192.0.2.30" }]
}
}
]
}To shift traffic during a rollout, edit the weights and submit the file again. Because the batch is atomic, both weights change together.
Inspecting existing records
Use list-resource-record-sets with a JMESPath --query to see what is there before you change it:
aws route53 list-resource-record-sets \
--hosted-zone-id ZONE_ID \
--query "ResourceRecordSets[?Name=='www.example.com.']"Names are returned in fully qualified form with a trailing dot. A query written without the dot matches nothing.
Common errors
InvalidChangeBatch, record already exists. You usedCREATEon an existing name and type. Switch toUPSERT.InvalidChangeBatch, record not found or values differ. ADELETEmust repeat the record's name, type, TTL and values exactly. Fetch the record first and copy it.InvalidInputwith a JSON parse message. Check for trailing commas and that you usedfile://, not a bare path.- A CNAME conflict. You cannot create a CNAME at a name that already has other records, and you cannot create a CNAME at the zone apex. Use an alias record at the apex.
- Wrong zone. Passing the ID of a different zone, such as a private one with the same name, changes records that public users never see. Confirm with
get-hosted-zone.
Exercise
You run an UPSERT that lowers a record's TTL from 3600 to 60. get-change reports INSYNC, yet some users still reach the old IP address 20 minutes later. Why, and what should you have done before the cutover?
Show answer
INSYNC only means Route 53's name servers have the change. Resolvers that cached the old answer keep it for the old TTL of 3600 seconds, so lowering the TTL does not shorten what is already cached. You should have lowered the TTL to 60 at least one old-TTL period (an hour) before changing the address, so caches would already be holding short-lived entries when the real change was made.
Exercise
Why is UPSERT usually safer than CREATE in a deployment script that may be re-run?
Show answer
CREATE fails with InvalidChangeBatch if the record already exists, so a second run breaks. UPSERT creates the record when it is missing and replaces it when it exists, so repeated runs give the same result.