Created
March 7, 2019 16:36
-
-
Save sebmaynard/6e51f191c4ee4e93292c17feb77f1011 to your computer and use it in GitHub Desktop.
Create dns records on route53 from a text file
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 -e | |
usage() { | |
echo "Need a hosted zone name (abc.com.) with the dot on the end" | |
exit 1 | |
} | |
HOSTED_ZONE_NAME=$1 | |
if [[ "$HOSTED_ZONE_NAME" == "" ]] ; then | |
usage | |
fi | |
HOSTED_ZONE_ID=$(aws route53 list-hosted-zones-by-name | jq -r ".HostedZones[] | select(.Name == \"$HOSTED_ZONE_NAME\").Id " | sed -e 's/\/hostedzone\///g') | |
if [[ "$HOSTED_ZONE_ID" == "" ]] ; then | |
echo "Couldn't find hosted zone" | |
usage | |
fi | |
while read NAME VALUE; do | |
echo $NAME $VALUE | |
JSON=$(cat <<EOF | |
{ | |
"HostedZoneId": "$HOSTED_ZONE_ID", | |
"ChangeBatch": { | |
"Changes": [ | |
{ | |
"Action":"UPSERT", | |
"ResourceRecordSet": { | |
"Name": "$NAME", | |
"Type": "CNAME", | |
"TTL": 300, | |
"ResourceRecords": [ | |
{ | |
"Value": "$VALUE" | |
} | |
] | |
} | |
} | |
] | |
} | |
} | |
EOF | |
) | |
aws route53 change-resource-record-sets --cli-input-json "$JSON" | |
done < dns.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment