Control your incoming traffic on your website AWS
Tags: aws, beginners, webdev, tutorial
Published: 2022-04-27 03:16:37

You can control traffic flow on your website using a load balancer.
![aws](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4funymbvbxtdlwz6d7du.png) 

What is Load Balancer?
A load balancer distributes incoming application traffic across multiple EC2 instances in multiple Availability Zones. This increases the fault tolerance of your applications.

Get started

Let’s start creating a EC2 instance

Now open Amazon EC2 Console
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyz50jiz25l7qdqj279l.png)
 
 
Now on the left menu, under Instances choose an instance

Create a new instance and under configure page, you will see the no of instance enter 2 (We are creating two instances with the same configurations)
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lo1q22j7fdur81m6r8pc.png)
 
Launch your instance
Done You just launched your 2 instances.


Let’s create TargetGroups

Now on the left menu, under load balancing choose TargetGroups
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtyqdx9d37f6dhnm0za0.png)

Create a new target group
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyfzh94aetcmgb22aoj0.png)
 
Under instances select your 2 instances and add them to pending instances and click on create target group.
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dan7wkm5ka7l160tlvi1.png)
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ppp2ubbp8jko8j83pni1.png)


Done you just created a target group


Let’s create a Load balancer

Now on the left menu, under load balancing choose a load balancer
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9e4l6t34n6zue6s2f31.png)
 

Create a new Load Balancer
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/547mx0mjjsqfj5c5dh6b.png)
 ![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufoe7r068bhc93dqlgfc.png)
 

Under security groups select the group which you have given to your instances
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3wg3gn1n8zh9rhh24lrt.png)
 

Under Targetgroup select the Target Group that you have created just now.
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2b6632wqiez5r8wcrzg.png)
 

At the bottom click on create a load balancer
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47y9ir3a1p00kw1tw31v.png)
 

Done You just created a Load Balancer

Now go to the instances page and click on your first instance.

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


Now click on connect and use EC2 instance connect, click on connect.

![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2imju5tgmx9q001mx06m.png)
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in4y33muyb9oquybhhwv.png)
  


Now you will see a Linux console will popup
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jq9axto0erh9pb1z6kxc.png)
 

Now we need to load a sample website in your instance, for that we need root access in your console
Type: This will move to the root directory

```
sudo su
```

Now follow the code snippets:

```
yum update -y  # it will update the system
yum install httpd -y # it will install the Apache Hypertext transfer protocol server program
systemctl start httpd # It will start the server
cd /var/www/html/ # After install httpd you will see the html directory where we can add our html pages which will be displayed on the brower
nano index.html # it will create a index.html file in html folder
```

Add this HTML data to your index.html file

```
<html>
 <head>
  <title>
     Welcome to server 1
  </title>
 </head>
 <body>
    <h1>Welcome to AWS praticse web page</h1>
 </body>
</html>
```

Save the file and enter this command

```
systemctl enable httpd # server will be enable and it runs continuously
```

Now go to your instance and select your first instance. At the bottom you will see the public address, copy that address and paste it into a new tab you will see the sample web page with the title Welcome to server 1 and the main content as Welcome to AWS practice web page.
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0mwd5h4eb6xt6wfxda7y.png)
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nc1xaftcazizd1ezetmu.png)
  

Now repeat the same process for the other instance
In the index.html file update the title to Welcome to server 2
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2ai4xyihhp5sb9zodjb.png)
 

You can have the same title but, here are able to see which instance is running while loading a webpage
Now on the left menu, under load balancing choose target groups and select your TG
At the bottom, you will see that both instances are healthy.
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7hha16oifbt6gohsuyw.png)
 

Now go to the load balancer and select your load balancer.
At the bottom, you will see the DNS name copy that URL and paste it into the new tab
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ij93ppbll39sw1j9zv8x.png)
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0bxwy6yu50ov01iqj0b.png)
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxcayfx2aqehmakt9byp.png)
   
 

Refresh your page you will notice that every time the instance is changing.
![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tba7mda9kh9oh0r5i99j.png)
 



You can able see the webpage title is changing every time while refreshing.

This is how we can control web traffic by interchanging the instance for every refresh.

Note: You need to keep the same code in both instances.

If you have more users and if your CPU is more than 80% you need to use autoscaling.

I will write a new article on autoscaling what is the CPU moves to 80%…

I am a beginner at AWS. If you feel there is something wrong with the article you can comment on it. So, that I can improve myself.

** Note:
This is only for testing after some time you can delete your instance. If you keep it active for some days it may charge you so after practicing delete your Instance.

Thanks for reading the article…

![AWS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0q74ei8drkpqai5uaqgv.png)