STEP 1- Launch an EC2 instance with user data (user data provided in steps)
- Search for EC2 in the search bar and click on it.
b. Once you are in the EC2 Dash, click on Launch to launch an instance
c. Provide a Name for your instance (I’m saving mine as Ikenna-Webserver1)
d. Choose Amazon Linux, select Amazon Linux 2023 AMI (HVM) architecture and Click on Select.
- Select the t2.micro instance type
- Select an existing key pair or create a new one
- Now click on Edit (Network settings)
- Select VPC (I’m selecting my custom VPC named IkennaVPC, and for the subnet, I’m selecting us-east- 1e
- Create a Security Group and ensure both the name and description of the security group are the same. (I created LBSG). Make sure you have HTTP and (or) HTTPS- ports 80 and 443 respectively, open from anywhere, else, it won’t work
- Scroll Down, Click on Advance Details.
e. Scroll down to User data add the below bootstrap script and Click on Launch instance
#!/bin/bash
sudo su
yum update -y
yum install -y httpd
echo ‘<h1> Hello from Ikenna_WebServer-01</h1>’ > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
- Go back to your Instances page, copy the Public IP4 address, and paste in a new browser
STEP 2- Launch a Second ECS2 instance.
a. Follow steps from step 1, however, this time we’ll
- Provide a different name for your instance (I’m saving mine as Ikenna-Webserver2)
- Select a different AZ (this time I’m selecting us-east-1d)
b. Add the below bootstrap script and click on Launch instance
#!/bin/bash
sudo su
yum update -y
yum install -y httpd
echo ‘<h1> Hello from Ikenna_WebServer-02 </h1>’ > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
- Go back to your Instances page, copy the Public IP4 address, and paste in a new browser
STEP 3- Create the target group for the Network load balancer
- Scroll down to Target Groups
- Click on Create Target group.
- under the Basic Configuration • Target type → Instance • Target name → Linuxinstance • Protocol and Port → TCP and Port 80 • VPC → Select the VPC containing the instances we just launched (mine is IkennaVPC)
- Register target- Select your Instance on which you want to route traffic and click on Include as pending below. You’ll be selecting the instances we just launched, and then click on “include as pending below”
- Click on create target group.
STEP 4- Create the Network Load Balancer
- Scroll down in Navigation Bar and go to Load Balancing and select Load Balancers
- Click on Create Load Balancer
- Click on Create under Network Load Balancer
- Under the Basic Configuration: • Load balancer name → NetworkLoadBalncer • Scheme → Internet-facing (default) • IP address type →IPv4
Under the Networking Mapping: VPC → select the Default VPC Mapping → Select all Availability Zone in which your instance are launched. Note: Here I have selected us-east-1d and us-east-1e because our EC2 Instances are launched in the above mentioned region.
- Select the newly created Security Group, i.e., the one created during launching of the instances.
- Under the Listners and routing, Protocol and port should remain as default and under Default action menu select your target group. Scroll to the bottom and click on Create load balancer
STEP 5- Test the Network Load Balancer
Note: In this section we will be testing our Network load balancer by hitting the DNS name on our web browser
- Navigate back to the Target Groups and open the Target group that we have created
- Under the Targets you should see that your instances are Healthy. Note: It will take some time to change the state from initial to healthy
- Navigate back to Load balancers
- Copy the load balancer that we created and copy DNS name
- Paste the DNS name in your browser and you find your web server running with either 01 or 02 as it is distributing the load to two webservers (WebServer-01 and WebServer02)
Congratulations!! You’ve successfully configured the Network Balancer and tested it with two different Instances.