How to Setup Website Failover to S3 Bucket in AWS
Failover is a technical term used to explain the automatic switchover from the primary server to a standby (secondary) server upon failure of the primary (main) server. A server hosting a website can go down for a number of reasons ranging from operating system crash to configuration issues. Having a failover (backup) server ensures that when such problems occur, users of the application do not experience down time.
This article demonstrates how to setup a failover to a static website using an S3 bucket in AWS.
For the purpose of clarity, the diagram below is used to depict what we want to achieve. Fig 1 Simple Failover Architecture
Table of Contents
As shown in the diagram above, we want to switch from the main server (which in this case is just an EC2 instance) to the secondary server (S3 bucket) when the health check fails. Traffic will be rerouted to the main server when the primary server becomes healthy again.
Note:
Setup Main (Primary) Server
For the purpose of this article, we shall be using nginx to host our static site on the EC2 instance on AWS. The main server does not necessarily have to host a static site or contain the same application as that of the failover. The primary server can be as large as a cluster of EC2 instances behind a load balancer. We are just keeping it simple.
Steps:
sudo apt-get update
- Install nginx server on the EC2 ubuntu instance
sudo apt-get install nginx -Y
- Navigate to the www directory and create a directory for the static website
NB: devito.ga can be any name. I just choose it because of its consistency with my domain name (www.devito.ga)
- Create the following subfolders inside the devito.ga folder
cd devito.ga
sudo mkdir public
sudo mkdir logs
- Navigate into the public folder and clone the static website from the Github repo
cd public sudo git clone https://github.com/Kenec/devito.git
- Copy the content of the clone repo (ie devito folder) into the public root folder
sudo cp -R devito/* .
- Remove the cloned project folder (devito)
sudo rm -r devito/ *
sudo vi /etc/nginx/sites-available/devito.ga
NB: devito.ga can be any name
- Paste this configuration inside the file
NB: The server_name should be replaced with your name
Also, the access_log, error_log and root should match the path in your EC2 instance
- Nginx will only serve content from sites that have been "enabled" therefore we create a symbolic links
ln -s /etc/nginx/sites-available/devito.ga /etc/nginx/site-enabled/devito.ga
- Change the default site that nginx points to
sudo vi /etc/nginx/sites-available/default
- Change the value of root from :
NB: devito.ga should be replaced with the name of the folder where your site resides
/etc/init.d/nginx restart
Setup Secondary Server
We are going to make use of our S3 bucket to host a static website which will serve as the secondary server. When the EC2 instance fails and the health check we are going to create in the next section detects it, traffic is going to be routed to the secondary server (ie static website hosted on S3 bucket)
Create an S3 bucket with the URL of your site eg www.devito.ga
Select Static website hosting and then check Use this bucket to host a website
Upload your static website to the S3 bucket
- Select the all the files and folder and make them public
Setup Route 53 and health checks
- Navigate to Route 53 on AWS and click on the Hosted zones
- Click on the Create Hosted Zone
Enter your domain name eg www.devito.ga
Copy the created hosted zone NS record generated in Route 53 and paste it in the freenom nameserver or where you got your domain name from as shown in the diagram below
Create a Health Check
- Configure the health check using the sample shown in the image below
- The IP address should be that of your EC2 instance and the domain name should be the one we pointed our Route 53 NS record to.
After creating the health checks, its status should be all green signifying that it is healthy.
Create an A Record Set in the hosted zone you created above.
- The Name should be empty because we are creating an A record
- Paste the IP of the EC2 instance hosting your website to the value section
- Under the Routing Policy , select Failover
- The Failover Record Type should be Primary
- Check Yes under the Associate with Health Check options, then select the health check we created earlier.
- Create another A Record Set and point it to the static website on S3 bucket.
- The Name field should be empty
- Select Alias to be Yes
- Under Alias Target, select the S3 website endpoint (ie the name of the S3 bucket hosting our static website)
- Choose the Routing Policy to be Failover
- Choose the Failover Record Type to Secondary
- There is no need to select health check for this secondary failover because it only serves us when the main server is down
With these setup, our website is configured to be fault tolerant. We will always have a backup server that handles traffic when the main server goes down.
Now that we are done with the setup, when we visit the domain name (eg www.devito.ga) we will see the website served from the main server.
when we stop the EC2 instance, we should see our health check become unhealthy and the traffic rerouted to the S3 bucket Stop the EC2 instance
Health Check Fails
Website served from the S3 bucket
Conclusion
And that’s it. We have our simple failover setup. This concept can be applied to a more complex network architecture.
Ensure to delete all resources you created on AWS for this tutorial purpose otherwise, you will be debited for it when you leave it running.