Skip to content

Instantly share code, notes, and snippets.

@colehocking
Last active July 16, 2019 18:38
Show Gist options
  • Save colehocking/49d4d56aa855b925e7d78ec12b2ce095 to your computer and use it in GitHub Desktop.
Save colehocking/49d4d56aa855b925e7d78ec12b2ce095 to your computer and use it in GitHub Desktop.
Get subdomain list from AWS Route53 Zone ID argument
#!/bin/bash
#
# Get Subdomain Record Sets for Hosted Zone ID in Route 53
# Requires: AWS CLI; jq
# -- Cole Hocking
# Takes Hosted Zone ID as arg
HZID=$1
OUTFILE="./subdomain_list.txt"
echo "Fetching record sets..."
get_subdomains(){
# Fetch the CNAME entries
# Inverse grep ignores domain key entries
aws route53 list-resource-record-sets --hosted-zone-id "${HZID}" | jq -r '.ResourceRecordSets[] | select(.Type == "CNAME") | .Name' | grep -v "domainkey"
# Fetch the A entries
aws route53 list-resource-record-sets --hosted-zone-id "${HZID}" | jq -r '.ResourceRecordSets[] | select(.Type == "A") | .Name'
}
# Outputs subdomain_list.txt
main(){
if [[ -f "${OUTFILE}" ]]; then
rm ${OUTFILE}
fi
get_subdomains | sort | uniq >> ${OUTFILE}
echo "Done!"
}
main
# Remove Trailing Period if needed
# sed -e 's/\.$//g' ${OUTFILE} -i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment