How to Create A Record to Point to Beanstalk Instance?

Introduction

I wanted to use AWS CLI to point www.clickplan.net to the Elastic beanstalk URL production.pf3mj4j3zw.us-east-1.elasticbeanstalk.com. Therefore, instead of hitting the URL production.pf3mj4j3zw.us-east-1.elasticbeanstalk.com in the browser to access the Rails 5.1 backend app, I can use www.clickplan.net.

The documentation is confusing because things are spread across different guides such as Route53, Beanstalks etc. It is also confusing due to two different hosted zone ids. The hosted zone id for www.clickplan.net is different from hosted zone id for the AliasTarget. The AliasTarget hosted zone id value is the same as the region where you deployed your domain. In my case us-east-1 is the region where I deployed clickplan.net using Beanstalk.

Focus Question

The focus question that I came up with during my research is: What is the hosted zone id for the alias target when creating A record?

Answer

The youtube video listed under the references section shows the demo of hosting a website on S3 and using the CLI for Route 53. It gave me the syntax for the JSON input and the command to run. This video made the distinction between the two different hosted zone ids clear. Now I knew what value must go for the hosted zone id for alias target section. After a few searches and looking at the AWS forum, finally I found the value by digging into the AWS documentation. The link is listed under the references section. Z117KPS5GTRQ2G is the hosted zone id for us-east-1 for beanstalk (from the AWS docs table).

The command line will use the hosted zone id for the domain clickplan.net. This is different from the hosted zone id under the alias target. In my case, Z3F02TIXYMYTDY is the hostedzone-id value for clickplan.net. The syntax for the command is:

aws route53 change-resource-record-sets --hosted-zone-id "/hostedzone/Z3F02TIXYMYTDY" --change-batch file://changebatch.json

We can now create changebatch.json with values for our task as follows:

{
    "Comment": "Create A record for the backend www.clicplan.net Rails app.",
    "Changes": [{
        "Action": "CREATE",
        "ResourceRecordSet": {
            "Name": "www.clickplan.net",
            "Type": "A",
            "AliasTarget": {
                "HostedZoneId": "Z117KPS5GTRQ2G",
                "EvaluateTargetHealth": false,
                "DNSName": "production.pf3mj4j3zw.us-east-1.elasticbeanstalk.com"
            }
        }
    }]
}

The output is:

{
    "ChangeInfo": {
        "Status": "PENDING", 
        "Comment": "Create A record for the backend www.clicplan.net Rails app.", 
        "SubmittedAt": "2017-07-30T19:31:14.394Z", 
        "Id": "/change/C1BXULBGFUO5BD"
    }
}

The status is pending. You can check the status by running this command.

aws route53 get-change --id C1BXULBGFUO5BD

We provide the change id we got from the output of the previous command. The output shows that the status is INSYNC, which means Route53 has done it's job.

{
    "ChangeInfo": {
        "Status": "INSYNC", 
        "Comment": "Create A record for the backend www.clicplan.net Rails app.", 
        "SubmittedAt": "2017-07-30T19:31:14.394Z", 
        "Id": "/change/C1BXULBGFUO5BD"
    }
}

It will take some time to propagate the changes. Wait for a few hours for the DNS resolvers and servers to pick up this change. It did not work. The problem was that the domain was registered at namecheap and the old Rails app was running on Linode. The name servers entry on namecheap account was set to Linode name servers:

ns1.linode.com
ns2.linode.com
ns3.linode.com
ns4.linode.com

I logged in to the AWS Route53 console and copied the name servers from the clickplan.net domain and updated the name servers entries for clickplan.net on namecheap account. The update will take upto 48 hours.

Testing A Record

You can use the dig command to test if the setup is working.

$dig www.clickplan.net
; <<>> DiG 9.8.3-P1 <<>> www.clickplan.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 48248
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.clickplan.net.     IN  A

;; Query time: 1671 msec
;; SERVER: 10.0.1.1#53(10.0.1.1)
;; WHEN: Sun Jul 30 18:41:21 2017
;; MSG SIZE  rcvd: 35

You can see that the status is SERVFAIL. After about 12 hours:

$dig www.clickplan.net

; <<>> DiG 9.8.3-P1 <<>> www.clickplan.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2791
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.clickplan.net.     IN  A

;; ANSWER SECTION:
www.clickplan.net.  51  IN  A   23.21.185.150
www.clickplan.net.  51  IN  A   54.235.113.223

;; Query time: 4 msec
;; SERVER: 10.0.1.1#53(10.0.1.1)
;; WHEN: Mon Jul 31 11:21:31 2017
;; MSG SIZE  rcvd: 67

Now hitting the backend app URL : http://www.clickplan.net/health/index shows the status ok JSON.

References


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.