The Node.js AWS SDK is a great tool for working with AWS. Once you get familiar with the Node.js SDK, it's much easier to work with than the labyrinthine AWS console UI. In this article, I'll show how to use the aws-sdk module to add and remove subdomains from a domain name registered on Route 53.

Getting Started

To connect to the AWS SDK, you need to get your AWS keys, which you can find under "Security Credentials" in the AWS console. You can read more about setting up the AWS SDK here.

Once you have your key and secret, you can run the below script to list all domains that you have registered on Route 53. AWS calls a domain a "hosted zone".

const AWS = require('aws-sdk');
const { promisify } = require('util');

const accessKeyId = 'aws key here';
const secretAccessKey = 'aws secret here';

AWS.config.update({
  accessKeyId,
  secretAccessKey,
  region: 'us-east-1'
});

const route53 = new AWS.Route53();

run().catch(err => console.log(err));

async function run() {
  // AWS SDK doesn't support promises as of this writing, so you need
  // to explicitly promisify.
  const res = await promisify(route53.listHostedZones).call(route53);
  console.log(res);
}

The output from the above script looks something like what you see below. My account has exactly one domain registered, mongoosejs.link.

{ HostedZones:
   [ { Id: '/hostedzone/OMITTED',
       Name: 'mongoosejs.link.',
       CallerReference: 'RISWorkflow-RD:abc-123-456',
       Config: [Object],
       ResourceRecordSetCount: 2 } ],
  IsTruncated: false,
  MaxItems: '100' }

Creating a New Subdomain

Suppose you want to create a new subdomain under mongoosejs.link. To create a subdomain test-subdomain.mongoosejs.link, you need to add a new record to the mongoosejs.link zone's record set. A record set is just a list of DNS records.

// Create a CNAME for test-subdomain.mongoosejs.link - basically make
// this subdomain redirect to mongoosejs.com.
const res = await promisify(route53.changeResourceRecordSets).call(route53, {
  HostedZoneId: '/hostedzone/ID for mongoosejs.link here',
  ChangeBatch: {
    Changes: [{ 
      Action: 'CREATE',
      ResourceRecordSet: {
        Name: 'test-subdomain.mongoosejs.link.',
        Type: 'CNAME',
        TTL: 60 * 5, // 5 minutes
        ResourceRecords: [{ Value: 'mongoosejs.com' }]
      }
    }]
  }
});
console.log(res);

If the request succeeds, the output looks like this:

{ ChangeInfo:
   { Id: '/change/OMITTED',
     Status: 'PENDING',
     SubmittedAt: 2020-04-15T14:39:59.496Z } }

Deleting a Subdomain

Deleting a subdomain is easy: just change the Action parameter from 'CREATE' to 'DELETE'.

const res = await promisify(route53.changeResourceRecordSets).call(route53, {
  HostedZoneId: '/hostedzone/ID for mongoosejs.link here',
  ChangeBatch: {
    Changes: [{ 
      Action: 'DELETE',
      ResourceRecordSet: {
        Name: 'test-subdomain.mongoosejs.link.',
        Type: 'CNAME',
        TTL: 60 * 5, // 5 minutes
        ResourceRecords: [{ Value: 'mongoosejs.com' }]
      }
    }]
  }
});
console.log(res);

Moving On

The Route 53 API is fairly easy to work with. Once you register a domain, you can programatically add subdomains. Combine that with uploading a function to Lambda using the AWS SDK and you can start building your own serverless continuous deployment platform.

Found a typo or error? Open up a pull request! This post is available as markdown on Github
comments powered by Disqus