You may also watch the video version of this blog post here.
When we deploy a production application we usually to attach a custom domain so our customers can easily find it on the internet. If you have a web application (E.g. S3 static web app, beanstalk web app, etc…) on AWS. Normally we create a hosted zone on Route53 and copy the nameservers and replace them with your domain provider’s nameservers. This is the standard process.
But sometimes, we may already have other applications that are not running on AWS (e.g. Email servers) attached to the domain provider. When we paste Route53 nameservers for the parent domain, those applications stop working.
Now you probably want your application on AWS to attach to a subdomain. (e.g. If your parent domain is example.com, you may want your web application attached to app.example.com). So how do this? There are several ways really. You can set up URL forwarding with or without URL masking, or you can create a CNAME record to AWS application endpoint. But what I usually do is delegating sub-domain DNS resolution to AWS. So I can use AWS specific DNS records like “Alias” records to serve my applications.
Let’s imagine we have a beanstalk environment that runs a web application. We need to attach a subdomain. But the original domain has a mail server configured so we cannot replace the parent domain nameservers with AWS nameservers. Instead, let’s follow these steps to setup subdomain for the website with subdomain delegation.
In this example, I use namecheap.com as my domain name registrar.
Goto route53 and create a hosted zone. The hosted zone name should be the domain name. (E.g. awslearners.online)
Goto your domain provider. E.g. Namecheap, GoDaddy etc… and navigate to the DNS advance settings page. Create new Nameserver records for the sub-domain you plan to delegate to Route53.
If you’ve registered the domain using namecheap.com, login to the account and goto the Advance DNS Settings of the domain you have purchased. (Similar configuration is available for other domain registers like GoDaddy.com etc…)
Now click on Add New Record then select NS record from the list and start adding nameserver records found for your hosted zone in Route53 one by one.
In the example below, the Host is added as “app” so the sub-domain is app.awslearner.online
Now, all requests for app.awslearner.com will be resolved by the nameservers of Route53. All other requests for the root domain and other subdomains will be resolved by namecheap.com nameservers. Thereby MX records for mail-servers will still work since the root-domain nameservers are still handled by namecheap.com.
Finally, go back to AWS Route53 hosted zone and create the corresponding subdomain record and point it to the beanstalk application using AWS specific “Alias” record.
AWS specific Alias records have many benefits over general CNAME records. (See the documentation)
Once the record is created, you may have to wait sometime (up to 48hours) for DNS propagation to happen. After that, you should be able to browse your beanstalk web application with the subdomain.
Hope this helps!