Skip to content

Instantly share code, notes, and snippets.

@dmc5179
Last active January 13, 2020 14:12
Show Gist options
  • Save dmc5179/d537657da5247b1faf2407257a61708a to your computer and use it in GitHub Desktop.
Save dmc5179/d537657da5247b1faf2407257a61708a to your computer and use it in GitHub Desktop.
AWS Update Route53 DNS Record
#!/bin/bash
#
# Script to change the Route53 public DNS entry associated to this host
# with the new EIP assigned to this host when booted.
#
# Need to get EIP using the CLI since that IP is not actually assigned to the host
set -xe
INSTANCE_ID=""
HOSTED_ZONE=""
DNS_RECORD=""
PUBLIC_IP=$(aws ec2 describe-instances --instance-ids ${INSTANCE_ID} | jq '.Reservations[0].Instances[0].PublicIpAddress' | tr -d '"')
cat > /tmp/change-resource-record-sets.json << EOF
{
"Comment": "Update record to reflect new IP address of ${DNS_RECORD}",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "${DNS_RECORD}",
"Type": "A",
"TTL": 300,
"ResourceRecords": [
{
"Value": "${PUBLIC_IP}"
}
]
}
}
]
}
EOF
aws route53 change-resource-record-sets --hosted-zone-id ${HOSTED_ZONE} --change-batch file:///tmp/change-resource-record-sets.json
exit 0
@dmc5179
Copy link
Author

dmc5179 commented Jan 13, 2020

Use this service file content to launch the script above on boot. Note that the root user will need the aws cli and credentials to make the route53 calls.

[Unit]
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/bash /usr/local/bin/route53_dns_update.sh
[Install]
WantedBy=multi-user.target

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment