DEV Community

Cover image for Control your incoming traffic on your website AWS
Aravind Kumar Vemula
Aravind Kumar Vemula

Posted on

Control your incoming traffic on your website AWS

You can control traffic flow on your website using a load balancer.
aws

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

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

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

Create a new target group
AWS

Under instances select your 2 instances and add them to pending instances and click on create target group.
AWS
AWS

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

Create a new Load Balancer
AWS
AWS

Under security groups select the group which you have given to your instances
AWS

Under Targetgroup select the Target Group that you have created just now.
AWS

At the bottom click on create a load balancer
AWS

Done You just created a Load Balancer

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

AWS

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

AWS
AWS

Now you will see a Linux console will popup
AWS

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Save the file and enter this command

systemctl enable httpd # server will be enable and it runs continuously
Enter fullscreen mode Exit fullscreen mode

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
AWS

Now repeat the same process for the other instance
In the index.html file update the title to Welcome to server 2
AWS

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

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
AWS
AWS

Refresh your page you will notice that every time the instance is changing.
AWS

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

Top comments (0)