Creating a Highly-Available website in AWS
Published: 2024-05-02 17:55:09



1.Prerequisites:

AWS Account: You'll need an active AWS account with appropriate permissions to create EC2 instances, Application Load Balancers, Auto Scaling Groups, and related resources.

Basic Knowledge of AWS Services: Familiarity with EC2, Auto Scaling, Application Load Balancer, and related AWS services is recommended.

2.Aim:

The aim of this project is to create a highly available website infrastructure using AWS services including EC2 instances, Application Load Balancer, and Auto Scaling Group.

Ensure that the website remains accessible and responsive even during high traffic loads or in the event of instance failures.

Implement auto-scaling to automatically adjust the number of EC2 instances based on demand, optimizing costs and performance.

3.Resources Used: VPC(subnets, internet gateway, route tables) EC2, Security Groups, Load Balancer, Template, Auto Scaling Group.
  

4.Steps:

 > Set up VPC (Virtual Private Cloud): Create a VPC (Select an appropriate region of your choice ) to isolate your resources and provide network security. 

points to remember:
When creating a VPC you will have two options - VPC only and VPC and more. 
In a VPC-only scenario:
You create the VPC itself, but it doesn’t automatically include any subnets, internet gateways, or route tables.

You have to manually add subnets to the VPC, allocate IP addresses, and configure routing.

Here, we will be manually adding all the resources so as to get the underlying concepts and working of our architecture.

No internet gateway is automatically created, so instances within the VPC cannot directly access the internet.

VPC with More Options:
When you choose to create a VPC with more options, AWS automatically sets up additional components along with the VPC
You need to set up custom route tables to define how traffic flows within the VPC.

![VPC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lghx6fs7e3n6l2fmv8da.png)

> Create Subnets: Create subnets, one in each availability zone of the region selected. 
points to remember - 
CIDR block of the subnets should not overlap with each other, or it will show an error. you can use online available subnet calculator tool or simple use the arrows highlighted.


![Creating subnets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2fwf1t0h7g8laocx628.png)

> Create an Internet gateway and attach the gateway to the above vpc created.

![Internet gateway in aws](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xu7sjejp3ermb80d95qs.png)

> Create Route tables: 
create a route table and associate it with all 3 subnets that you created earlier.

![creating route table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otn6egwnwnfuw1nhjt37.png)

also edit routes

![routes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8uk8h80u28oj7fb39b7.png)

> Creating Security Groups:
Security Group for Application Load Balancer.
inbound rules:

![Inbound Rules for Load Balancer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp26458x7k1rib29d1ri.png)

> Security group for EC2 fleet.
inbound rules 

![Inbound rules for ec2 instances](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/809mru6ajwr2zy1re4l1.png)

only inbound rules for security group for ec2 fleet is that it will receive traffic from security group of your load balancer. 

> Launch EC2 Instances:

Launch an EC2 instance, select the same region, VPC, any 1 subnet, Security group for EC2, and enable auto-allocation of public IP.

in the use data script, add the following script.

![userdata script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hp0md5q2ban7a761892i.png)


> Create a template from the EC2 created, this template will be used to create your Auto Scaling group.


> Create Application Load Balancer (ALB): Set up an ALB to distribute incoming traffic across multiple EC2 instances.


![Application Load Balancer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utx91kjkcyg28k3rxc0q.png)
points to remember:
select security group created for ALB
select all the subnets
for target groups create a target group, add the created instance in the target group.

![Target Group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0u18zlvl8vo0bsygqdg.png)
![Target Group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6le40iv6kznvm2xhvcrr.png)

check your Elastic load balancer by copying its DNS name and pasting it to a browser, preferably in a new Cognito window.
your should see the Hello world page, along with private IP of your instance
 

> Create Auto Scaling Group (ASG):
 Configure an ASG to automatically adjust the number of EC2 instances based on demand
points to remember:
select the template we created earlier.
select the VPC and all the subnets
![auto scaling group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q893lrwaid3j3bnxrs9i.png)

select the elastic load balancer option, and select the target group created above.


![autoscaling group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dndt5lss2gu0re7dmulq.png)

check the activity tab of your auto scaling group, it shows alerts regarding....well it's activities. instances will spin up in accordance with your Desired capacities.

refresh the tab where you pasted the DNS name. you should see the change in private ip addresses, that means the load balancer is working and distributing load to all EC2 instances.


![Instance-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tknw68640z8vmyyqr2hr.png)


![Instance -2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lr0hdqa2la8cape8w3ob.png)


![Instance 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x6feo1j6b23ou3spoip8.png)

Test High Availability: Conduct tests to ensure that your website remains accessible and responsive even during instance failures or high traffic loads.

try terminating instances, auto scaling group will automatically spin up more to take its place.

5.Conclusion:

By implementing the above steps, you have successfully created a highly available website infrastructure on AWS using EC2 instances, Application Load Balancer, and Auto Scaling Group.
Your website is now capable of handling varying levels of traffic and instances failures without affecting user experience.
Regular monitoring and optimization of your infrastructure will help ensure continued high availability and performance.

6.Clean-Up:

Once you have completed testing and are finished with your project, remember to clean up all resources to avoid unnecessary costs.
Terminate EC2 instances, delete Auto Scaling Group, remove Application Load Balancer, and delete any other associated resources.
Optionally, you may also want to delete the VPC and related networking resources if they are no longer needed.