Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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