How to list DNS records using the Route53 Node.js API
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):
- Open the the IAM Users page in the AWS dashboard.
- Click âAdd Userâ, give it a name, and select âprogrammatic accessâ.
- Select âAttach existing policies directlyâ and search for
AmazonRoute53ReadOnlyAccess
. This means that this key will only be able to read Route53 resources, not modify them or access anything else. - Go through the rest of the setup steps and note the access key ID and secret access keys generated at the end.
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.
Subscribe to my newsletter!
A weekly round-up of new blog posts and updates to projects Iâm working on.