How to Host a Website Using an EC2 Instance, Route 53, and a Custom Domain
Table of contents
- Prerequisites
- Step 1: Set Up Your EC2 Instance
- Step 2: Set Up Route 53 Hosted Zone
- Step 3: Configure Domain Settings in your Domain Provider
- Step 4: Add an A Record in Route 53
- Step 5: Access Your Website
- Step 6: Add Latency-Based Routing for Multiple Regions
- Step 7: Enable Health Checks for High Availability
- Summary
Setting up a website with AWS Route 53 and EC2 makes hosting easy and adds powerful features like latency-based routing and health checks. This guide will show you how to connect a domain you’ve purchased to AWS, set up latency-based routing to make your site load faster for visitors from different locations, and add health checks to keep your site running smoothly all the time.
Prerequisites
An AWS Account: If you don’t already have an AWS account, create one.
A Purchased Domain: Obtain a domain from a provider like Hostinger, GoDaddy, or Namecheap. For this guide, we'll assume the domain is managed by Hostinger.
Step 1: Set Up Your EC2 Instance
Launch an EC2 instance: In the AWS Management Console, go to the EC2 dashboard and create a new instance.
Choose an Amazon Linux 2 AMI (or another OS of your choice).
Select instance type (t2.micro works well for small websites).
Configure security groups to allow HTTP traffic on port 80.
Install HTTPD server: Go to the advanced details and paste the script in user data.
#!/bin/bash sudo yum update -y # Install Apache web server (httpd) sudo yum install -y httpd sudo systemctl start httpd sudo systemctl enable httpd # Create a simple HTML file to verify the web server is running echo "<html><h1>Welcome to Apache Web Server from Mumbai</h1></html>" >/var/www/html/index.html
Step 2: Set Up Route 53 Hosted Zone
Create a Hosted Zone:
In the Route 53 console, create a new hosted zone.
Use your domain name (e.g.,
yourdomain.com
) as the hosted zone name.
Get the Name Servers:
Once the hosted zone is created, AWS will provide a list of name servers. Copy these, as you’ll need them in the next step.
Step 3: Configure Domain Settings in your Domain Provider
In my case, I’m using Hostinger for my domain.
Update Name Servers:
Log in to your Hostinger account and navigate to the DNS settings for your domain.
Replace the existing name servers with the ones provided by Route 53.
Save the changes; DNS updates can take 15-30 minutes to propagate fully.
Step 4: Add an A Record in Route 53
Create an A Record:
Go back to Route 53 and open your hosted zone.
Create an “A” record with simple routing, and paste the public IP address of your EC2 instance.
Save the record.
Step 5: Access Your Website
After 40-50 minutes, your domain should direct you to your EC2 instance's HTTPD server. Enter your domain name in a browser, and you should see the webpage hosted on your EC2 instance.
Step 6: Add Latency-Based Routing for Multiple Regions
Set Up an Additional EC2 Instance in a Different Region
Repeat the above steps to set up a second EC2 instance in the N. Virginia region.
Configure this instance with the same files so it mirrors the Mumbai instance. Change the script a little instead of from Mumbai change it to from N. Virginia.
Update Route 53 with Latency-Based Routing
In your hosted zone, create a new A record but select Latency Routing instead of simple routing.
Add the IP address of each instance in its respective region (Mumbai and N. Virginia) and specify the regions.
Route 53 will direct users to the instance with the lowest latency based on their location.
-
Use VPN to check the N. Virginia server. Now, we’ll use health checks in our server so I’m not using VPN right now.
Step 7: Enable Health Checks for High Availability
To ensure visitors aren’t routed to an offline server, set up health checks:
Create Health Checks for Both Instances: In Route 53, configure health checks for each EC2 instance. Set the endpoint as the public IP address and choose HTTP as the protocol.
Associate Health Checks with Route 53 Records: Link each A record to its corresponding health check. Now, Route 53 will monitor the instances, redirecting traffic to the healthy server if one instance goes down.
Check the health check whether your servers are healthy or not.
Terminate the Mumbai server, wait a little and check the health check again.
Since we terminated the Mumbai server health check now divert all the traffics to N. Virginia server or we can say the healthy server. Try to access your server and see the magic.
Security Tip: To keep your website safe, add an SSL/TLS certificate for HTTPS. This makes sure all data shared on your site is private and secure, building trust with your users.
Summary
By following these steps, you have successfully hosted a website on AWS EC2, set up latency-based routing for optimal performance, and implemented health checks for high availability. This configuration ensures users from different regions experience minimal latency and uninterrupted service.