Skip to content

Instantly share code, notes, and snippets.

@MatthewDaniels
Created July 18, 2023 00:41
Show Gist options
  • Save MatthewDaniels/fe558c8ed6a31f715a5208fe36794374 to your computer and use it in GitHub Desktop.
Save MatthewDaniels/fe558c8ed6a31f715a5208fe36794374 to your computer and use it in GitHub Desktop.

Output AWS hosted zones

Prerequisities

  1. AWS CLI Installed (for Debian based linux with apt: sudo apt install awscli)
  2. Credentials for a user that has Route53 read access:
  • Log into AWS Console
  • Create API credentials
  • Store credentials in ~/.aws/credentials (put it in a [default] block or create a new block to use if you have many accounts in there)

Executing

With a local json file

To cache the hosted zone json to your local machine (for other purposes or to run some tests prior to executing, run write_hosted_zones_to_json.sh first to get the JSON from AWS first. Once you have the file lcaolly, you can run get_all_hosted_zones_local.sh to create the zone files.

Directly from AWS CLI

To run the command in one go, calling the AWS CLI in real time, run get_all_hosted_zones.sh

#!/bin/bash
aws route53 list-hosted-zones | jq -c '.HostedZones | .[]' | while read i; do
name=$(echo $i | jq -r .Name)
id=$(echo $i | jq -r .Id)
aws route53 list-resource-record-sets --hosted-zone-id "${id}" | jq -r '.ResourceRecordSets[] | [.Name, .Type, (.ResourceRecords[]? | .Value), .AliasTarget.DNSName?] | @csv' > "${name//./_}DNS_Zone.csv"
done
#!/bin/bash
jq -c '.HostedZones | .[]' hosted_zones.json | while read i; do
name=$(echo $i | jq -r .Name)
id=$(echo $i | jq -r .Id)
aws route53 list-resource-record-sets --hosted-zone-id "${id}" | jq -r '.ResourceRecordSets[] | [.Name, .Type, (.ResourceRecords[]? | .Value), .AliasTarget.DNSName?] | @csv' > "${name//./_}DNS_Zone.csv"
done
#!/bin/bash
aws route53 list-hosted-zones > hosted_zones.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment