Control your incoming traffic on your website AWS
You can control traffic flow on your website using a load balancer.
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.
Let’s start creating a EC2 instance
Now open Amazon EC2 Console
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)
Launch your instance
Done You just launched your 2 instances.
Let’s create TargetGroups
Now on the left menu, under load balancing choose TargetGroups
Create a new target group
Under instances select your 2 instances and add them to pending instances and click on create target group.
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
Create a new Load Balancer
Under security groups select the group which you have given to your instances
Under Targetgroup select the Target Group that you have created just now.
At the bottom click on create a load balancer
Done You just created a Load Balancer
Now go to the instances page and click on your first instance.
Now click on connect and use EC2 instance connect, click on connect.
Now you will see a Linux console will popup
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
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
Welcome to server 1
<h1>Welcome to AWS praticse web page</h1>
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.
Now repeat the same process for the other instance
In the index.html file update the title to Welcome to server 2
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.
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
Refresh your page you will notice that every time the instance is changing.
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.
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…