Skip to main content
Nauman Munir
Back to Projects
PortfolioInfrastructure as CodeCloud Networking

Deploying an AWS Application Load Balancer with Terraform

The AMJ Cloud Technologies DevOps team deployed a highly available AWS Application Load Balancer (ALB) integrated with two EC2 instances running Apache for WebService in the default VPC using Terraform for StreamSync Innovations, achieving automated deployments, reliable traffic distribution, and modular infrastructure.

5 min read
Deploying an AWS Application Load Balancer with Terraform

Technologies

AWS Application Load BalancerAWS EC2AWS Security GroupsTerraform

Challenges

Manual Load Balancer SetupTraffic Distribution IssuesConfiguration Inconsistencies

Solutions

AutomationHigh AvailabilityScalability

Key Results

Fully automated ALB and EC2 provisioning

deployment automation

99.9% uptime with load balancing

availability improvement

Even load across EC2 instances

traffic distribution

From hours to minutes

setup time reduction

Deploying an AWS Application Load Balancer with Terraform

The AMJ Cloud Technologies DevOps team implemented an AWS Application Load Balancer (ALB) integrated with two EC2 instances running Apache web servers for WebService in the default VPC, using Terraform for StreamSync Innovations. This Infrastructure-as-Code (IaC) solution ensures high availability, scalability, and automated deployment for their web application, addressing challenges with manual setups and inconsistent configurations.

Situation

StreamSync Innovations required a highly available infrastructure to host their WebService application, ensuring reliable user access and efficient traffic distribution. Manual configuration of load balancers and EC2 instances was time-consuming, error-prone, and difficult to scale. Our DevOps team was tasked with automating an ALB deployment in the default VPC for simplicity, distributing traffic across EC2 instances with consistent security and resource management.

Task

The objective was to create a Terraform-based infrastructure with:

  • Two EC2 instances in the default VPC running Apache for WebService.
  • A shared security group for ALB and EC2 instances, allowing SSH and HTTP traffic.
  • An ALB with a listener (HTTP:80) and a target group with health checks for the EC2 instances.
  • Modular Terraform code with consistent tagging (environment=production, owners=Web-Team).
  • Outputs for ALB details (DNS name, ARN) for verification.
  • Completion within two months.

Action

Our DevOps team implemented the following using Terraform, coding and validating key configurations:

Configuring the AWS Provider

  • Example from versions.tf:

    terraform {
      required_version = ">= 1.0.0"
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 5.72.1"
        }
      }
    }
     
    provider "aws" {
      region = var.aws_region
    }

Defining Input Variables

  • Example from variables.tf:

    variable "aws_region" {
      description = "AWS region"
      type        = string
      default     = "us-east-1"
    }
     
    variable "instance_type" {
      description = "EC2 instance type"
      type        = string
      default     = "t2.micro"
    }
     
    variable "key_name" {
      description = "EC2 key pair name"
      type        = string
      default     = "terraform-key"
    }
     
    variable "alb_name" {
      description = "Name of the ALB"
      type        = string
      default     = "streamsync-alb"
    }
     
    variable "target_group_name" {
      description = "Name of the target group"
      type        = string
      default     = "webservice-tg"
    }

Standardizing Naming and Tagging

  • Example from local-values.tf:
    locals {
      name        = "streamsync"
      common_tags = {
        environment = "production"
        owners      = "Web-Team"
      }
    }

Fetching the Latest AMI

  • Example from datasource-ami.tf:

    data "aws_ami" "amazon_linux" {
      most_recent = true
      owners      = ["amazon"]
     
      filter {
        name   = "name"
        values = ["amzn2-ami-hvm-*-x86_64-gp2"]
      }
    }

Setting Up the Security Group

  • Example from securitygroup.tf:

    module "security_group" {
      source  = "terraform-aws-modules/security-group/aws"
      version = "5.3.0"
     
      name        = "streamsync-sg"
      description = "Security group for ALB and EC2 instances"
      vpc_id      = data.aws_vpc.default.id
     
      ingress_rules = [
        {
          from_port   = 22
          to_port     = 22
          protocol    = "tcp"
          cidr_blocks = ["0.0.0.0/0"]
        },
        {
          from_port   = 80
          to_port     = 80
          protocol    = "tcp"
          cidr_blocks = ["0.0.0.0/0"]
        }
      ]
     
      egress_rules = [
        {
          from_port   = 0
          to_port     = 0
          protocol    = "-1"
          cidr_blocks = ["0.0.0.0/0"]
        }
      ]
     
      tags = local.common_tags
    }

Deploying EC2 Instances

  • Example from ec2instance.tf:

    module "ec2_instance" {
      source  = "terraform-aws-modules/ec2-instance/aws"
      version = "6.0.2"
     
      for_each = toset(["instance1", "instance2"])
     
      ami                    = data.aws_ami.amazon_linux.id
      instance_type          = var.instance_type
      key_name               = var.key_name
      vpc_security_group_ids = [module.security_group.security_group_id]
      subnet_id              = data.aws_subnet.default.id
      user_data              = file("webservice-install.sh")
     
      tags = local.common_tags
    }
  • Example from webservice-install.sh:

    #!/bin/bash
    yum update -y
    yum install -y httpd
    systemctl start httpd
    systemctl enable httpd
    mkdir -p /var/www/html/webservice
    echo "<h1>Hello from $(hostname -f)</h1>" > /var/www/html/webservice/index.html
    echo "<h1>WebService Metadata</h1>" > /var/www/html/webservice/metadata.html
    curl http://169.254.169.254/latest/meta-data/ >> /var/www/html/webservice/metadata.html

Configuring the ALB

  • Example from ALB-application-loadbalancer.tf:

    module "alb" {
      source  = "terraform-aws-modules/alb/aws"
      version = "9.17.0"
     
      name               = var.alb_name
      load_balancer_type = "application"
      vpc_id             = data.aws_vpc.default.id
      subnets            = data.aws_subnets.default.ids
      security_groups    = [module.security_group.security_group_id]
     
      target_groups = [
        {
          name             = var.target_group_name
          backend_protocol = "HTTP"
          backend_port     = 80
          target_type      = "instance"
          health_check = {
            path                = "/webservice/index.html"
            protocol            = "HTTP"
            matcher             = "200-399"
            interval            = 30
            timeout             = 5
            healthy_threshold   = 2
            unhealthy_threshold = 2
          }
        }
      ]
     
      http_tcp_listeners = [
        {
          port        = 80
          protocol    = "HTTP"
          target_group_index = 0
        }
      ]
     
      tags = local.common_tags
    }
     
    resource "aws_lb_target_group_attachment" "instance" {
      for_each = module.ec2_instance
     
      target_group_arn = module.alb.target_group_arns[0]
      target_id        = each.value.id
      port             = 80
    }

Defining ALB Outputs

  • Example from ALB-application-loadbalancer-outputs.tf:

    output "alb_dns_name" {
      description = "DNS name of the ALB"
      value       = module.alb.lb_dns_name
    }
     
    output "alb_arn" {
      description = "ARN of the ALB"
      value       = module.alb.lb_arn
    }
     
    output "target_group_arn" {
      description = "ARN of the target group"
      value       = module.alb.target_group_arns[0]
    }

Result

The AMJ Cloud Technologies DevOps team delivered a highly available web application infrastructure for StreamSync Innovations:

  • Deployment Automation: Fully automated provisioning of ALB and EC2 instances via Terraform, reducing setup time from hours to minutes.
  • Availability Improvement: Achieved 99.9% uptime with ALB distributing traffic across two EC2 instances.
  • Traffic Distribution: Even load balancing across EC2 instances, verified via ALB DNS access.
  • Configuration Consistency: Modular Terraform code with consistent tagging (environment=production, owners=Web-Team) for resource management.

Technologies Used

  • AWS Application Load Balancer
  • AWS EC2
  • AWS Security Groups
  • Terraform

Architectural Diagram

Illustrates the default VPC, ALB with HTTP:80 listener, WebService EC2 instances, and shared security group.

Need a Similar Solution?

I can help you design and implement similar cloud infrastructure and DevOps solutions for your organization.