21
Jun

Build a resilient website with Amazon EC2 and S3

In some instances, there may be failure from your EC2 instances web server to serve your website to your users unknown to you.

During this period you might serve your user with static web pages from Amazon Simple Storage Service(S3) that display necessary contents from your main web server while you fix the EC2 instance issue.

This blog explain how you can create a failover to a static S3 website in event of failure of EC2 web server and get notified of the EC2 failure for immediate remediation.

Required Resources
Route53
Amazon Elastic Compute Cloud(EC2)
Amazon Simple Storage Service (S3)
Registered Domain name
Elastic IP
Security Group
Amazon Simple Notification Service(SNS)

Severity Level: Beginner

step1 : Spin up EC2 instance web server with elastic IP address.

NB: If you don’t know how to do that, check this tutorial https://codysense.com/launch-free-amazon-cloud-server-with-public-ip/

Step 2 : Configure alternate web page using S3

Amazon S3 enable you to host static website which is our failover website incase of main web server failure.

You can learn how to do that from this tutorial :https://codysense.com/create-a-static-website-with-s3/

NB: The domain name you intend to use for your dynamic website must be your bucket name.

Step 3: Configure Route53 to link your websites to your domain

Search for Route53 from the services

Click Create Hosted zone.

Hosted zone name is your domain or subdomain name you have registered

Select public hosted zone as Type

Click Create hosted zone

When you create Hosted zone Start Of Authority (SOA) and Nameserver record are created automatically.
If your domain is registered with aws, you can go ahead and create first record but if your domain is registered with third party registrar.

Click on the checkbox for NS record and click Edit.
Copy each record and paste it in the nameserver of your domain registrar.

Choose Health Check in the left pane of the Route53

This health check will be monitoring your EC2 web server for reachability check. This would be use to determine when the server is not reachable to failover to  another server, It also include SNS notification to alert the server administrator of the server failure.

Name the health check

What to monitor – Endpoint

Specify Endpoint by – IP address

IP address – The server Elastic IP address

Port – 80

Click Next

Create alarm – Yes

Send Notification to – New SNS Topic

Topic name – name your SNS

Recipient email address – you personal email address

Click Create health check

record name – type the subdomain name or leave it blank if you are using the domain name

Value – EC2 elastic IP address

Routing policy – failover

Failover record type – primary

Health check Id – choose the health check created earlier (click the refresh button if not showing)

Record ID – give it a unique name

Click save

type the domain or subdomain name in the browser – web server from your EC2 should display in the browser

Click create record again

Now Let’s create a secondary failover to S3 static website

Record name – same as first record name

Alias – switch it ON

Route traffic to – Alias to S3 Endpoint

Region – choose region the S3 bucket was created (preferably same region the EC2 was created)

Endpoint – choose the S3 bucket

Routing policy – failover

Failover record type – secondary

Record ID – give it a unique name

Click Save

Refresh the url in the browser to be sure the EC2 web server is still reachable.

Go to the EC2 instances page

Click Instance State

Click stop instances

wait for  3 mins and refresh the browser again, the S3 web page will display.

NB

Thank you

Happy learning!

In my next post, I will provide a means make the system repair itself by automatically start EC2 instance if a stop attempt is made to it or alert the administrator if security group is modified.

For question on this tutorial, you can connect with me on these channels and let’s learn from each other.

Linkedin : https://www.linkedin.com/in/jeleel-kolapo-abidemi-899418a2/

Twitter: https://twitter.com/Jakolapo