Create DNS records in Amazon Route 53 with Node.js
Route 53 is Amazon's hosted DNS service. You create a zone (a domain name) and then add records to it e.g, "A" records or "CNAME" records. If you are writing your software in Node.js then Amazon's aws-sdk is here to help.
First of all, load the module:
npm install aws-sdk
and in your code:
var AWS = require('aws-sdk'); // AWS Config AWS.config.update({accessKeyId: YOURKEY, secretAccessKey: YOURSECRET}); // Set your region for future requests. AWS.config.update({region: YOURREGION}); // e.g. eu-west-1 // get the Route53 library var route53 = new AWS.Route53();
If you don't know what a accesskey, secret or region are then you need to do some readin about Amazon EC2
To work on a zone, you need to know its ZoneID. This can be discovered by fetching all your zones using the "listHostedZones" function:
route53.listHostedZones({}, function(err,data) { if(!err) { console.log(data); } });
which returns a list of your domains and their Zone IDs
{ HostedZones: [ { Id: '/hostedzone/ABC123', Name: 'mydomain.com.', CallerReference: 'SOME-UNIQUE-REFERENCE', Config: {}, ResourceRecordSetCount: 15 } ] }
We can then use the Id from the above reply to set an A record using the "changeResourceRecordSets" function:
var params = { "HostedZoneId": '/hostedzone/ABC123', // our Id from the first call "ChangeBatch": { "Changes": [ { "Action": "CREATE", "ResourceRecordSet": { "Name": "subdomain.mydomain.com", "Type": "A", "TTL": 86400, "ResourceRecords": [ { "Value": "123.123.123.123" } ] } } ] } }; route53.changeResourceRecordSets(params, function(err,data) { console.log(err,data); });
The Changes array contains an object for each change to be made. The Action can be "CREATE" or "DELETE". Full documentation is here











