Last active
January 13, 2020 14:12
-
-
Save dmc5179/d537657da5247b1faf2407257a61708a to your computer and use it in GitHub Desktop.
AWS Update Route53 DNS Record
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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