Skip to content

Instantly share code, notes, and snippets.

@jftuga
Last active May 1, 2022 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jftuga/c7842711baa7325f92813eed13817c12 to your computer and use it in GitHub Desktop.
Save jftuga/c7842711baa7325f92813eed13817c12 to your computer and use it in GitHub Desktop.
Change or Insert a Route53 entry from the command line
#!/bin/bash
# change_route53_dns_entry.sh
# -John Taylor
# 2022-05-01
# Change or add a route53 DNS entry
# dependencies: the aws cli, jq
# a AWS profile that has IAM permissions for Route53 API calls
# a Route53 hosted zone
DOMAIN="example.com"
# what you want the IP address of the new or changed DNS entry to be
IP="127.0.0.1"
# the fully qualified host name
ENTRY="test.${DOMAIN}"
# which profile the aws cli command should use
AWS_PROFILE="default"
aws --profile ${AWS_PROFILE} route53 list-hosted-zones-by-name --dns-name ${DOMAIN} > hosted.json
ZONE=$(cat hosted.json| jq -r ".HostedZones | .[] | select(.Name|test(\"${DOMAIN}.\")) | .Id | ltrimstr(\"/hostedzone/\")")
cat > change.json <<EOF
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "${ENTRY}.",
"Type": "A",
"TTL": 300,
"ResourceRecords": [
{
"Value": "${IP}"
}
]
}
}
]
}
EOF
echo "zone: ${ZONE} ip: ${IP} entry: ${ENTRY}"
aws --profile ${AWS_PROFILE} route53 change-resource-record-sets --hosted-zone-id ${ZONE} --change-batch file://./change.json
# You can check the status of the updates with this command:
# aws --profile ${AWS_PROFILE} route53 get-change --id CXXXXXXXXXXXXX
#!/bin/bash
# change_route53_dns_entry_optimized.sh
# -John Taylor
# 2022-05-01
# Change or add a route53 DNS entry
# this "optimized" version does not use any temporary files
# dependencies: the aws cli, jq
# a AWS profile that has IAM permissions for Route53 API calls
DOMAIN="example.com" # a Route53 hosted zone
IP="127.0.0.1" # what you want the IP address of the new or changed DNS entry to be
ENTRY="test.${DOMAIN}" # the fully qualified host name
AWS_PROFILE="default" # which profile the aws cli command should use
ZONE=$(aws --profile ${AWS_PROFILE} route53 list-hosted-zones-by-name --dns-name ${DOMAIN} | jq -r ".HostedZones | .[] | select(.Name|test(\"${DOMAIN}.\")) | .Id | ltrimstr(\"/hostedzone/\")")
aws --profile ${AWS_PROFILE} route53 change-resource-record-sets --hosted-zone-id ${ZONE} --change-batch file:///dev/stdin <<EOF
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "${ENTRY}.",
"Type": "A",
"TTL": 300,
"ResourceRecords": [
{
"Value": "${IP}"
}
]
}
}
]
}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment