Deploy 3 Tier HA Application Load Balancer using Terraform in AWS

Hello Everyone,

Today we will be deploying a 3-tier ALB using Terraform in AWS.

Previously I used a classic load balancer so this is an upgrade to project1.

Services used are EC2 Instances, VPC, Nat gateway, Internet Gateway, security groups, and Application Load balancer.

GitHub link:- github.com/piya199616/Terraform-AWS-Project..

Project1 link:- priyanka-burela.hashnode.dev/deploy-3-tier-..

Project Diagram:-

What is a Load balancer:

Elastic Load Balancing automatically distributes your incoming traffic across multiple targets, such as EC2 instances, containers, and IP addresses, in one or more Availability Zones. It monitors the health of its registered targets, and routes traffic only to the healthy targets.

Why ALB

As Classic LB is a traditional LB and doesn’t provide features like ALB.

The Classic Load Balancer is a connection-based balancer where requests are forwarded by the load balancer without “looking into” any of these requests. They just get forwarded to the backend section.

An Application Load Balancer functions at the application layer, the seventh layer of the Open Systems Interconnection (OSI) model. After the load balancer receives a request, it evaluates the listener rules in priority order to determine which rule to apply and then selects a target from the target group for the rule action. You can configure listener rules to route requests to different target groups based on the content of the application traffic.

How Elastic Load balancing works

  1. Your client makes a request to your application.

  2. The listeners in your load balancer receive requests matching the protocol and port that you configure.

  3. The receiving listener evaluates the incoming request against the rules you specify, and if applicable, routes the request to the appropriate target group. You can use an HTTPS listener to offload the work of TLS encryption and decryption to your load balancer.

  4. Healthy targets in one or more target groups receive traffic based on the load-balancing algorithm, and the routing rules you specify in the listener.

Manual Creation of ALB :

You can skip this if you are aware of manual creation.

Create Target groups and register targets.

make sure that you have already EC2 instances created in 2 different AZ.

Target Group Name: app1-tg

· Protocol: HTTP

· Port: 80

· VPC: default-vpc

· Protocol Version: HTTP1

· Health Check Protocol: HTTP

Health Check Protocol: HTTP

Health check path: /app1/index.html

Now create a load balancer

Select AZ where instances are present and attach subnets to it.

Attach a Security Group

Similar way we will create using Terraform.

Pre-requisites -

  1. AWS Account

  2. Terraform installed in the local system

  3. AWS CLI installed in the Local system

  4. Knowledge of AWS and Terraform.

What we will provision:

  • 1 VPC,6 subnets (Public, Private, Database),1 Nat gateway in Public Subnet, 1 Internet Gateway attached to VPC.

  • 1 Bastion host and 2 private EC2 instances in 2 AZs.

  • 1 Application Load Balancer.

  • 3 Security Groups (bastion host, Private Ec2, and Load balancer)

  • with the help of provisioner, we will copy the file from local(the private key file you copied) to the Bastion host, Run some commands on the remote host.

Steps:

Everything will be the same as the previous project only change will be the Application Load Balancer.

Step 1: Copy Files from c1 to c9 from the previous project1.

Step2: Create the files for ALB Basic

Step3: Create AWS Application Load Balancer Terraform configuration using ALB Terraform Module

Module link-https://registry.terraform.io/modules/terraform-aws-modules/alb/aws/latest

Give the name of the ALB it’ll be HR-stag-alb.

Mention the type of load balancer.

Mention the VPC ID and public subnets.

Security group of ALB.

Below is my local file

Step 4: Create Target Groups as below.

Now the listener has some rules that can forward these to target groups.

Step 5: Register your targets.

Step 6: Configure Listener

A listener is a process that checks for connection requests, using the protocol and port that you configure. The rules that you define for a listener determine how the load balancer routes request to its registered targets.

Step 7:Now Run Terraform Init, Terraform validate, Terraform plan, and Terraform Apply.

Step 8: Verify

1. Verify EC2 Instances

2. Verify Load Balancer SG

3. Verify ALB Listeners and Rules

4. Verify ALB Target Groups, Targets (should be healthy) and Health Check settings

5. Access sample app using Load Balancer DNS Name

Step 9: Clean up

Destroy your Resource with Terraform destroy -auto-approve

Fork my repo for more details, all code is present in my Github project link as mentioned above.

If you need any help then feel free to ping me on Linkedin:

Linkedin Profile:-linkedin.com/in/priyanka-burela