Delegating domains to AWS EC2 is a common issue for people who are starting their adventure with this cloud provider. It’s not complicated but some of you may find it unobvious. The explanation of this topic in the official documentation is not very clear and helpful either.
In the AWS environment, the service responsible for DNS management is called Route 53. It allows you to create DNS zones, register domains and schedule health checks of your applications.
Elastic IP
Every newly created EC2 instance has its own IPv4. However, it is not a permanent IP address and it changes every time you stop or restart your instance. That’s why you need to associate an elastic IP address to your instance. It’s a static IP and it will not change until you release it. To do this, just go to your EC2 service, click “Elastic IPs” in the left sidebar and allocate a new Elastic IP address. Then, from the “actions” dropdown menu, choose the “Associate Elastic IP address” option and select your instance. Ultimately, the details of your EC2 instance should look like this:
Always remember to release your Elastic IP addresses if you no longer need them. Otherwise, you are going to be charged for each allocated and unused IP.
Hosted zone
When we have an elastic IP created, the next step is creating a hosted zone. It’s just a set of DNS records that allows us to route a domain to a certain IP address. To do this, go to the Route 53 service in your AWS console and click a Create hosted zone button. Then, in the right pane fill the Domain Name field with your domain address (without an http or https prefix). Leave the rest of the fields with the default values.
Now the DNS records of your hosted zone should look similar to those on the screen below.
Now you need to copy the DNS addresses from the NS record and paste into your domain’s configuration. Every domain registrar has a different interface for configuring DNS servers. If you don’t know how to do it, contact the registrar with which you have a registered domain. DNS propagation may take up to 48 hours (usually less than 12) so don’t worry if you don’t notice any changes right away.
Then click Create Record Set button. If you want to route the main domain (not a subdomain) leave the Name field blank. In the Type field select A – IPv4 address option, and to the Value field paste an Elastic IP of your EC2 instance. Leave the rest of the fields with default values and click a Create button.
Summary
That’s all you have to do to point your domain registered outside AWS to an AWS EC2 instance. In the near future, I also plan to write a similar post about delegating IDN domains to the AWS and another one about creating subdomains with Route 53.