Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active October 22, 2021 04:03
Show Gist options
  • Save guitarrapc/6b113666b7834183632d80e333f60006 to your computer and use it in GitHub Desktop.
Save guitarrapc/6b113666b7834183632d80e333f60006 to your computer and use it in GitHub Desktop.
Clean up All A record alias in target HostedZone.
#!/bin/bash -e
# obtains records
hostedZone=xxxxxxxx
records=$(aws route53 list-resource-record-sets --hosted-zone-id "$hostedZone" --query 'ResourceRecordSets[?Type==`A`].[Name,AliasTarget.HostedZoneId,AliasTarget.DNSName]' --output text)
head=$(cat <<EOS
{
"Comment": "DELETE record",
"Changes": [
EOS
)
tail=$(cat <<EOS
]
}
EOS
)
contents=$(while IFS= read -r line; do
name=$(echo "$line" | cut -f 1)
hostedZoneId=$(echo "$line" | cut -f 2)
dnsName=$(echo "$line" | cut -f 3)
cat <<EOS
{
"Action": "DELETE",
"ResourceRecordSet": {
"Name": "$name",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "$hostedZoneId",
"DNSName": "$dnsName",
"EvaluateTargetHealth": true
}
}
},
EOS
done <<< "$records"
)
# create json
echo "${head}${contents::-1}${tail}" > change_batch_delete.json
# preview json
cat change_batch_delete.json
# delete target dns records
aws route53 change-resource-record-sets --hosted-zone-id "${hostedZone}" --change-batch file://change_batch_delete.json
#!/bin/bash -e
# obtains records
hostedZone=xxxxxxxx
records=$(aws route53 list-resource-record-sets --hosted-zone-id "$hostedZone" --query 'ResourceRecordSets[?Type==`TXT`].[Name,TTL,ResourceRecords[0].Value]' --output text)
head=$(cat <<EOS
{
"Comment": "DELETE record",
"Changes": [
EOS
)
tail=$(cat <<EOS
]
}
EOS
)
contents=$(while IFS= read -r line; do
name=$(echo "$line" | cut -f 1)
ttl=$(echo "$line" | cut -f 2)
value=$(echo "$line" | cut -f 3 | sed 's/"/\\"/g')
cat <<EOS
{
"Action": "DELETE",
"ResourceRecordSet": {
"Name": "$name",
"Type": "TXT",
"TTL": $ttl,
"ResourceRecords": [{
"Value": "${value}"
}]
}
},
EOS
done <<< "$records"
)
# create json
echo "${head}${contents::-1}${tail}" > change_batch_delete.json
# preview json
cat change_batch_delete.json
# delete target dns records
aws route53 change-resource-record-sets --hosted-zone-id "${hostedZone}" --change-batch file://change_batch_delete.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment