Deploying AWS Elastic Load Balancer with Terraform: IaC Patterns and Module Configuration

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, Elastic Load Balancing 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 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.

This article provides a complete journey through the deployment of AWS load balancers utilizing Terraform, with important information and abilities to engineer powerful and versatile load-balancing solutions in AWS infrastructure. The focus is on principal ideas, terminologies, and a step-by-step manual for deploying AWS load balancers using Terraform.

Primary Terminologies

Understanding the core vocabulary is essential before implementing ELB with Terraform.

  • AWS Elastic Load Balancer, ELB: Flexible Burden Balancer is an overseen administration given by Amazon Web Administrations that naturally disseminates approaching application traffic across different targets, for example, EC2 occasions, holders, IP locations, or Lambda capabilities, in various accessibility zones. It guarantees high accessibility, adaptation to internal failure, and adaptability of utilizations by uniformly appropriating the responsibility and rerouting traffic away from undesirable targets.
  • Terraform: Terraform is an open-source Infrastructure as Code tool created by HashiCorp. It allows users to define and provision data center infrastructure utilizing a declarative configuration language called HashiCorp Configuration Language. Terraform empowers clients to manage and automate the deployment of infrastructure resources across different cloud providers, including AWS, Azure, Google Cloud Platform, and others.
  • Terraform Configuration: Terraform configuration refers to a set of records containing infrastructure code written in HCL, determining the ideal condition of the infrastructure.

By arranging load balancer configurations, listeners, target groups, and related resources, groups can automate the provisioning system, ensuring consistency and reliability quality across deployments. Terraforms declarative syntax structure works on infrastructure management, empowering users to define the ideal condition of their AWS environment and apply changes reliably. The integration with AWS services, for example, Elastic Load Balancing works with high availability, fault tolerance, and scalability of applications. With Terraform, associations can undoubtedly increase assets or down, adjust to evolving responsibilities, and integrate load adjusting into their CI/CD pipelines for continuous delivery.

Generally speaking, deploying AWS load balancers with Terraform upgrades functional effectiveness, speeds up infrastructure deployment, and enables groups to fabricate versatile and scalable architectures in the cloud.

Cloud load balancers, like the AWS Elastic Load Balancer, offer extra elements like automatic scaling, health checks, and integration with other cloud services, pursuing famous decisions for present-day cloud-native applications. These load balancers are highly adaptable, effectively configurable, and flawlessly coordinated with cloud environments.

Terraform Module for Classic Load Balancer

A reusable Terraform module which creates Classic Load Balancer resources on AWS is available for community use.

Example usage:

hcl module "elb_http" { source = "terraform-aws-modules/elb/aws" name = "elb-example" subnets = ["subnet-12345678", "subnet-87654321"] security_groups = ["sg-12345678"] internal = false listener = [ { instance_port = 80 instance_protocol = "HTTP" lb_port = 80 lb_protocol = "HTTP" }, { instance_port = 8080 instance_protocol = "http" lb_port = 8080 lb_protocol = "http" ssl_certificate_id = "arn:aws:acm:eu-west-1:235367859451:certificate/6c270328-2cd5-4b2d-8dfd-ae8d0004ad31" }, ] health_check = { target = "HTTP:80/" interval = 30 healthy_threshold = 2 unhealthy_threshold = 2 timeout = 5 } access_logs = { bucket = "my-access-logs-bucket" } number_of_instances = 2 instances = ["i-06ff41a77dfb5349d", "i-4906ff41a77dfb53d"] tags = { Owner = "user" Environment = "dev" } }

Valid SSL certificate has to be specified as sslcertificateid argument for secure listener.

Module dependencies and requirements are:

  • terraform >= 1.0
  • No providers explicitly required in the module root
  • Name | Source | Version
  • elb | ./modules/elb | n/a
  • elbattachment | ./modules/elbattachment | n/a

No resources are declared directly in the root module.

Module Input Variables

The module exposes a set of inputs to configure Classic ELB behavior.

Name Description Type Default Required
access_logs An access logs block map(string) {} no
connection_draining Boolean to enable connection draining bool false no
connectiondrainingtimeout The time in seconds to allow for connections to drain number 300 no
create_elb Create the elb or not bool true no
crosszoneload_balancing Enable cross-zone load balancing bool true no
health_check A health check block map(string) n/a yes
idle_timeout The time in seconds that the connection is allowed to be idle number 60 no
instances List of instances ID to place in the ELB pool list(string) [] no
internal If true, ELB will be an internal ELB bool false no
listener A list of listener blocks list(map(string)) n/a yes
name The name of the ELB string null no
name_prefix The prefix name of the ELB string null no
numberofinstances Number of instances to attach to ELB number 0 no
security_groups A list of security group IDs to assign to the ELB list(string) n/a yes
subnets A list of subnet IDs to attach to the ELB list(string) n/a yes
tags A mapping of tags to assign to the ELB map(string) n/a no

The healthcheck block defines how the load balancer evaluates target health. Typical attributes include target, interval, healthythreshold, unhealthy_threshold, and timeout.

Step-by-Step Process to Create AWS Load Balancer Using Terraform

The following workflow illustrates a manual provisioning path for an Application Load Balancer using Terraform with explicit resources.

Step 1: Launch An Instance

  • Launch EC2 instance with Amazon Linux2 Kernel 5.10 AMI along with port numbers set SSH – 22, HTTP 80 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.

Step 2: Install Terraform

Now install terraform packages from official site of hashicorp or follow below commands

bash 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

hcl resource "aws_vpc" "siva" { cidr_block = var.vpc_cidr instance_tenancy = "default" tags = { Name = "siva-vpc" } }

Provider Configuration

hcl provider "aws" { region = "us-east-1" }

Creation of Subnet

In this section we are creating subnets to VPC

hcl resource "aws_subnet" "public-subnet1" { vpc_id = aws_vpc.siva.id cidr_block =

Load Balancer definition using native AWS resources:

hcl { 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] }

Target group for the Application Load Balancer:

hcl 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" } }

Target group attachments:

hcl 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, ] }

hcl 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, ] }

Listener definition:

hcl 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

Conclusion

Deploying AWS Elastic Load Balancing with Terraform delivers repeatable, versioned, and auditable infrastructure. ELB provides automatic distribution of traffic across EC2 instances, containers, IP addresses, or Lambda functions across availability zones for high availability and fault tolerance. Terraform provides declarative HCL configuration that defines the ideal state of load balancers, listeners, target groups, security groups, and VPC networking.

Using native resources allows fine-grained control over Application Load Balancer type, internal flag, subnets, security groups, target groups with health checks, and listeners with default forward actions. Using community modules such as the Classic Load Balancer module abstracts common patterns for listener definitions, health checks, access logs, connection draining, cross-zone load balancing, and instance attachments with tagging.

Operational benefits include automation of provisioning, consistency across deployments, integration with CI/CD pipelines, and the ability to scale resources up or down in response to changing load. Health checks, idle timeouts, access logs, and SSL certificate binding via sslcertificateid provide production-grade observability and security.

Together, ELB and Terraform enable organizations to build versatile and scalable architectures in the cloud with improved operational effectiveness and faster infrastructure deployment.

Sources

  1. GeeksforGeeks
  2. terraform-aws-modules

Related Posts