Skip to content

Instantly share code, notes, and snippets.

@porjo
Last active February 15, 2024 14:19
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save porjo/7447463ce0d14f212e58f9eabad77090 to your computer and use it in GitHub Desktop.
Save porjo/7447463ce0d14f212e58f9eabad77090 to your computer and use it in GitHub Desktop.
Export route53 records to CSV

Retrieve hosted zones with aws route53 list-hosted-zones then enter the zone Id below:

aws route53 list-resource-record-sets --hosted-zone-id "/hostedzone/xxxxxxxxxxx" | \
   jq -r '.ResourceRecordSets[] | [.Name, .Type, (.ResourceRecords[]? | .Value), .AliasTarget.DNSName?]  | @tsv'
@aa6my
Copy link

aa6my commented Apr 22, 2020

for csv
aws route53 list-resource-record-sets --hosted-zone-id "/hostedzone/xxxxxxxxxxx" | jq -r '.ResourceRecordSets[] | [.Name, .Type, (.ResourceRecords[]? | .Value), .AliasTarget.DNSName?] | @csv'

@dmytropetryk
Copy link

Thanks, work as expected 👍

@saqib-s
Copy link

saqib-s commented Aug 26, 2020

Really helpful thank you!

@landsman
Copy link

thanks!

@singaravelanpalani
Copy link

aws route53 list-resource-record-sets --hosted-zone-id "/hostedzone/Z071111111111111" | jq -r '.ResourceRecordSets[] | [.Name, .Type, (.ResourceRecords[]? | .Value), .AliasTarget.DNSName?] | @csv' > abc.csv

@eramm
Copy link

eramm commented Jun 28, 2022

aws route53 list-resource-record-sets --hosted-zone-id "/hostedzone/Z071111111111111" --output json | jq -r '.ResourceRecordSets[] | [.Name, .Type, (.ResourceRecords[]? | .Value), .AliasTarget.DNSName?]

For the copy and pasters out there :-)

@thomazbarros
Copy link

Thanks for the command.

@Jackman3005
Copy link

I was trying to get the output into a CSV file that was uniform and able to be uploaded to Notion. After tweaking, I came up with the following, which works well for me. It combines the values and any alias into one column and provides a header row so the output is readily importable as a CSV.

aws route53 list-resource-record-sets --hosted-zone-id "/hostedzone/Z071111111111111" --output json \
  | jq -r '["Name", "Type", "Value"], (.ResourceRecordSets[] | [.Name, .Type, (.ResourceRecords | select(. != null) | map(.Value) | join(" ")), (.AliasTarget.DNSName? | select(. != null))]) | @csv'

@hopewise
Copy link

hopewise commented Apr 16, 2023

I needed to get all domains and their A Alias record, using ChatGPT, here is how it went, and it worked nicely ~:)

write a ruby code to fetch AWS Route53 records and generate a CSV file that has two columns, domain name and A record value
alter the code so that there is a outer loop to get all hosted zone ids first
alter the code to get all records as that code will get only first 100 record
alter the code to include only record name and value incase of record type is A and Alias

I also managed to get the AWS CLI version after some prompt tweaking, basically it started as:

write the same code using aws cli tool
for hzid in $(aws route53 list-hosted-zones --profile mfa --output json | jq -r '.HostedZones[].Id'); do aws route53 list-resource-record-sets --profile mfa --hosted-zone-id "$hzid" --output json | jq -r '.ResourceRecordSets[] | select((.Type == "A" or .Type == "ALIAS") and (.AliasTarget == null or (.AliasTarget.DNSName | test("^((?!ecs).)*$")))) | [.Name, (.AliasTarget | .DNSName) // (.ResourceRecords[0] | .Value)] | @csv' ; done > route53_records.csv

@alanaveena
Copy link

now that we have the route53 details into a csv, and someone deleted the current one, I would like to write a shell script that will take the csv as input and create hosted zone and all of the resource record sets also

@andrehcampos
Copy link

To print the CSV data with headers:

aws route53 list-resource-record-sets --hosted-zone-id "/hostedzone/ZRxxxxxxxxxxx" | jq -r '
    ["Name", "Type", "Value", "AliasTargetDNSName"],
    (.ResourceRecordSets[] | [.Name, .Type, (.ResourceRecords[]? | .Value), .AliasTarget.DNSName?])
    | @csv
' | (echo "Name,Type,Value,AliasTargetDNSName" && cat)

@swoodford-clear
Copy link

swoodford-clear commented Dec 18, 2023

this solution handles ALIAS records which are exported as A records but invalid to import anywhere but AWS

  • also replaces extraordinarily high TTL values with 86400 which is the highest possible, and null TTL values with 300
  • any other null values are exported as "unknown" for troubleshooting later
  • exports as tab separated for BIND format that can be imported elsewhere
profile=example
hosted_zone="HOSTEDZONEID"

aws route53 list-resource-record-sets --profile $profile --hosted-zone-id $hosted_zone --output json | \
jq -r '.ResourceRecordSets[] | "\(.Name // "unknown")\t\((if .TTL > 86400 then 86400 else .TTL // 300 end))\tIN\t\((if .AliasTarget.DNSName then "CNAME" else .Type // "unknown" end))\t\(.ResourceRecords[]?.Value // .AliasTarget.DNSName // "unknown")"'

@rikwouters
Copy link

rikwouters commented Feb 15, 2024

Thank you!

Note: I ran a few of these aws/jq commands in a Microsoft Windows shell and got errors like

'[.Name' is not recognized as an internal or external command, operable program or batch file.

Using double quotes makes it work.

list-resource-record-sets --hosted-zone-id "/hostedzone/ZXXXXXXXXXXXXX" | jq -r ".ResourceRecordSets[] | [.Name, .Type, (.ResourceRecords[]? | .Value), .AliasTarget.DNSName?] | @tsv" > abc.tsv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment