Skip to content

Instantly share code, notes, and snippets.

@sebmaynard
Created March 7, 2019 16:36
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 sebmaynard/6e51f191c4ee4e93292c17feb77f1011 to your computer and use it in GitHub Desktop.
Save sebmaynard/6e51f191c4ee4e93292c17feb77f1011 to your computer and use it in GitHub Desktop.
Create dns records on route53 from a text file
#!/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