domain search

Creating subdomains with AWS Route 53.

AWS provides the scalable, easy-to-use, and powerful DNS service Route 53. This service can be managed via the AWS Console or via API.

You may also enjoy:  How to Use Your Own Domain to Access an AWS-Hosted Website

Here we are going to see how to create a subdomain dynamically and map it to an S3 bucket using Node.js.

var AWS = require('aws-sdk')
var route53 = new AWS.Route53({ accessKeyId: cred.accesskey
secretAccessKey: cred.secretkey region: cred.region}); 

Let’s take the subdomain name from the command line argument:

var subDomainName = process.argv[2];

var params = {
  ChangeBatch: {
    Changes: [
        Action: "CREATE",
        ResourceRecordSet: {
          AliasTarget: { 
            DNSName: "",
            EvaluateTargetHealth: false,
            HostedZoneId: "********" 
          Name: subDomainName,
          Type: "A"
      Comment: "Testing subdomain creation "
      HostedZoneId: "{Replace with your TLD HoztzoneID}"// Depends on the type of resource that you want to route traffic to

Ensure you have a host zone created in Route 53.

Go to AWS Route 53, click "Hosted Zones” on the left-hand menu, and click "Create Hosted Zone.” Enter your domain name, mark it public, and save.

The moment you create a hosted zone, you will get a HostZoneID:

Comment: "Testing subdomain creation "},
HostedZoneId: "{Replace with your TLD HoztzoneID}"// 

which is suppose to be used in the HostedZoneID mentioned in the Alias Target is of S3:

AliasTarget: {DNSName: "",
EvaluateTargetHealth: false,HostedZoneId: "********" } 

It is hardcoded

Let’s invoke the changeResourceRecordSets  API:

route53.changeResourceRecordSets(params, function(err, data) {

if (err) console.log(err, err.stack); // an error occurred

else console.log(data);

When you click on the HostedZone, you will see a successful subdomain created pointing to your S3 bucket. We need to ensure  Name: subDomainName is your bucket name, and you will have successfully created your subdomain.

Hope this helps.

In the next article, we will see how a static web hosting bucket can be created dynamically.

Further Reading

The Ultimate Solution for Your Website and Its Sub-Domain’s Security 

Wildcard Subdomains in Windows Azure

from DZone Cloud Zone