December 30, 2020
Recently, I had a project where I wanted to programmatically get all the DNS records for my domain. The DNS was being handled on AWS Route53.
First, I created an IAM User for my code to use (it's AWS' version of an API key):
AmazonRoute53ReadOnlyAccess
. This means that this key will only be able to read Route53 resources, not modify them or access anything else.In your project, create a JSON file (I called mine aws.json
) with your keys in it:
{
"accessKeyId": "AKIA5POZ6AJXFCGJPE4H",
"secretAccessKey": "n1YrVagQ8/Cz3nwLMoiK4OlSudzbKFCbVzMRZhjI",
"region": "us-east-1"
}
Next, open up your Hosted zones on Route53 and open the domain whose DNS records you want to read. Copy down the ID at the end of the URL bar ā it'll probably start with a Z
.
Now, you're ready to use the AWS API:
const AWS = require('aws-sdk')
AWS.config.loadFromPath('./aws.json') // your JSON file with access keys
const route53 = new AWS.Route53()
route53.listResourceRecordSets({
HostedZoneId: 'Z...', // hosted zone ID from earlier
MaxItems: '300'
}, (err, data) => {
console.log(data)
})
And that's it! You can look at the logged data
variable to see the DNS records for this domain.