Step-by-Step Process to create AWS load balancer using terraform
Step 1: Launch An Instance
- Launch EC2 instance with Amazon Linux2 Kernel 5.10(AMI) along with port numbers set SSH – 22, HTTP 8o and allow all traffic and select storage t2.micro.
- Now connect with git bash terminal or any other terminals like putty, command prompt and PowerShell e.t.c.
Step 2: Install Terraform
Now install terraform packages from official site of hashicorp or follow below commands
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
Step 3: Create A File And Write Terraform Script for to create AWS load balancer
Creation of VPC
In this section we are creating VPC for our EC2 Instance
resource "aws_vpc" "siva" {
cidr_block = var.vpc_cidr
instance_tenancy = "default"
tags = {
Name = "siva-vpc" # configure our own name
}
}
Provider Configuration
provider "aws" {
region = "us-east-1"
}
Creation of Subnet
In this section we are creating subnets to VPC
resource "aws_subnet" "public-subnet1" {
vpc_id = aws_vpc.siva.id
cidr_block = var.subnet1_cidr
map_public_ip_on_launch = "false"
availability_zone = "us-east-1a"
tags = {
Name = "public-subnet1"
}
}
#public-subnet2 creation
resource "aws_subnet" "public-subnet2" {
vpc_id = aws_vpc.siva.id
cidr_block = var.subnet2_cidr
map_public_ip_on_launch = "false"
availability_zone = "us-east-1b"
tags = {
Name = "public-subnet2"
}
}
#private-subnet1 creation
resource "aws_subnet" "private-subnet1" {
vpc_id = aws_vpc.siva.id
cidr_block = var.subnet3_cidr
availability_zone = "us-east-1b"
tags = {
Name = "private-subnet1"
}
}
Internet Gateway Creation
In this section we creating internet gateway for VPC to allow internet traffic
resource "aws_internet_gateway" "siva-gateway" {
vpc_id = aws_vpc.siva.id
}
Creation of Rout Table
resource "aws_route_table" "route" {
vpc_id = aws_vpc.siva.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.siva-gateway.id
}
tags = {
Name = "route to internet"
}
}
#route 1
resource "aws_route_table_association" "route1" {
subnet_id = aws_subnet.public-subnet1.id
route_table_id = aws_route_table.route.id
}
#route 2
resource "aws_route_table_association" "route2" {
subnet_id = aws_subnet.public-subnet2.id
route_table_id = aws_route_table.route.id
}
Creation of Security Group
resource “aws_security_group” “web-sg” {
vpc_id = aws_vpc.siva.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "web-sg"
}
}
User data for EC2 instance
In this section we are providing user data for EC2 Instance to clone from GitHub because to host application
Creation of EC2 instance
resource "aws_instance" "ecomm" {
ami = "ami-07761f3ae34c4478d"
instance_type = "t2.micro"
key_name = "WORDPRESS"
vpc_security_group_ids = [aws_security_group.web-sg.id]
subnet_id = aws_subnet.public-subnet1.id
associate_public_ip_address = true
user_data = file("data1.sh")
tags = {
name = "EC2-1"
}
}
Creation of load balancer
resource "aws_lb" "external-alb" {
name = "External-LB"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.web-sg.id]
subnets = [aws_subnet.public-subnet1.id, aws_subnet.public-subnet2.id]
}
resource "aws_lb_target_group" "target_elb" {
name = "ALB-TG"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.siva.id
health_check {
path = "/health"
port = 80
protocol = "HTTP"
}
}
resource "aws_lb_target_group_attachment" "ecomm" {
target_group_arn = aws_lb_target_group.target_elb.arn
target_id = aws_instance.ecomm.id
port = 80
depends_on = [
aws_lb_target_group.target_elb,
aws_instance.ecomm,
]
}
resource "aws_lb_target_group_attachment" "food" {
target_group_arn = aws_lb_target_group.target_elb.arn
target_id = aws_instance.food.id
port = 80
depends_on = [
aws_lb_target_group.target_elb,
aws_instance.food,
]
}
resource "aws_lb_listener" "listener_elb" {
load_balancer_arn = aws_lb.external-alb.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.target_elb.arn
}
}
Creation of variable file
The variable file in Terraform serves as a centralized location for defining and managing input variables used across multiple Terraform configurations. These variables allow users to parameterize their infrastructure code, making it more flexible, reusable, and maintainable.
variable "vpc_cidr" {
default = "10.0.0.0/16"
}
#cidr block for 1st subnet
variable "subnet1_cidr" {
default = "10.0.1.0/24"
}
#cidr block for 2nd subnet
variable "subnet2_cidr" {
default = "10.0.2.0/24"
}
Creation of Output file
#DNS of LoadBalancer
output "lb_dns_name" {
description = "DNS of Load balancer"
value = aws_lb.external-alb.dns_name
}
Step 4: Now Initialize Terraform And Execute Terraform Commands
Now initialize terraform by using following command
terraform init
Now execute terraform execution commands by using following commands
terraform fmt
terraform validate
terraform plan
Now execute terraform apply command to build our infrastructure and its give DNS link. Execute using following command
terraform apply --auto-approve
In above figure shows that Load Balancer DNS lin. By coping DNS Link and browse it in google then we see that our application
The following screenshot shows that we successfully created a sqs topic in aws using terraform
AWS Application Load Balancer Using Terraform
In contemporary cloud infrastructure setups, managing and distributing incoming traffic effectively across various instances is central to ensuring the high accessibility and scalability of applications. Among the bunch of services given by AWS (Amazon Web Services), Elastic Load Balancing (ELB) stands apart as a basic part of working with this task. ELB automatically circulates approaching application traffic across a fleet of targets, for example, EC2 instances, containers, IP addresses, or Lambda functions, across different availability zones, ensuring adaptation to non-critical failure and high accessibility.
To streamline out the provisioning and the management of AWS resources, Infrastructure as Code (IaC) apparatuses like Terraform have acquired massive popularity. Terraform offers an explanatory way to deal with characterizing and provisioning infrastructure, allowing clients to determine the ideal condition of their infrastructure utilizing a direct and natural configuration language known as HashiCorp Configuration Language (HCL).
In this article, we embark on a complete journey through the deployment of AWS load balancers utilizing Terraform. Our point is to outfit you with the important information and abilities to engineer powerful and versatile load-balancing solutions in AWS infrastructure. We’ll dig into principal ideas, and terminologies, and give a step-by-step manual for deploying AWS load balancers using Terraform.
Contact Us