DEV Community

Genne23v
Genne23v

Posted on

Create Domains Using Route 53 SDK

The team for Starchart project is in planning phase. It's not as exciting as coding. But research is important and it's good time to explore technologies that I have not tried or learn more details about things that I tried. Starchart will create a domain for Seneca students. To do so, the team will use Route 53 and I spent some time on understanding AWS SDK for Route 53 since we need to automate the steps to create a domain in Starchart web app.

Setup Route 53 SDK

To learn Route 53 SDK, I started with official SDK page. To use Route 53 SDK, you need to install SDK by running npm install @aws-sdk/client-route-53.

Now I need to start coding. First step is import Route53Client and set up configuration.



const { Route53Client } = require('@aws-sdk/client-route-53');

const config = {
    credentials: {
        accessKeyId: '******',
        secretAccessKey: '******',
        sessionToken: '******',
    },
};


Enter fullscreen mode Exit fullscreen mode

You need to get your own credential information from your AWS account. I use student account which might be different from normal users. I can get my accessKeyId, secretAccessKey, and sessionToken in AWS Details as shown in below picture.

AWS Learner Lab

Creating Hosted Zone

The first method that I need to try is CreateHostedZoneCommand. Hosted zone is a AWS term that represents a collection of records that can be managed together, belonging to single parent domain name. And here's code snippet to use CreateHostedZoneCommand.



const { Route53Client, CreateHostedZoneCommand } = require('@aws-sdk/client-route-53');

const input = {
        CallerReference: new Date().toString(),
        Name: 'test.starchart.com',
    };

const client = new Route53Client(config);
const command = new CreateHostedZoneCommand(input);
const response = await client.send(command); 


Enter fullscreen mode Exit fullscreen mode

To create a hosted zone, all you need is CallerReference and Name. You can also set up more input parameters such as HostedZoneConfig, VPC, etc. based on your need. CallerReference must a unique value for your request. If you use the same CallerReference, you will get an error. And Name is for your domain name. If response is successful, you will receive metadata of your request, location, change info, hosted zone info, nameserver info.

Now I have records inside the hosted zone that I created.

Hosted zone details

Add/Update/Delete Records in Hosted Zone

I can add a new record or update existing records with ChangeResourceRecordsSetCommand function. Below is the basic syntax how to create or update your record.



const { Route53Client, ChangeResourceRecordSetsCommand } = require('@aws-sdk/client-route-53');

const changeBatch = {
    Action: 'UPSERT',
    ResourceRecordSet: {
        Name: 'mydomain.starchart.com',
        Type: 'A', 
        TTL: 60,
        ResourceRecords: [
            {
                Value: '192.168.0.1',
            },
        ],
    },
};

const input = {
    ChangeBatch: changeBatch,
    HostedZoneId: '****************',
};

const client = new Route53Client(config);
const command = new ChangeResourceRecordSetsCommand(input);
const response = await client.send(command); 


Enter fullscreen mode Exit fullscreen mode

For input parameter, you need to provide changeBatch object and HostedZoneId. In this case, I use UPSERT action. This is a way to simplify the method that it can add a new record if it doesn't exist or update existing one. In the ResourceRecordSet, you should provide domain name, type, TTL (i.e. record cache time to live), and route traffic to. In this example, the new domain mydomain.starchart.com will be directed to A record, 192.168.0.1. You can find more options when you set routing policy other than this simple example. I found AWS CLI Command Reference page is very helpful to understand the parameters and refer to other ChangeBatch syntax.

List Records and View Change Status

When you fetch all your records or partial records, you can use ListResourceRecordSetsCommand.

Lastly GetChangeCommand method can check the status of specific ChangeBatch by providing ChangeInfo.Id from ChangeResourceRecordSetsCommand response as a parameter. GetChangeCommand returns ChangeInfo and ChangeInfo.Status will show either PENDING or INSYNC.

Conclusion

I used AWS S3, DynamoDB SDK before. So it didn't take much time to set up and configure. AWS SDK syntax is highly consistent and it's easy to use once you learn any one of these SDKs. As I mentioned above, AWS CLI reference page has more details explaining each parameter and response although you will rely on JavaScript SDK page more to write your code. Now I'm eager to write code for Starchart.

Top comments (0)