Skip to main content

Application Load Balancer Module

Deploy an AWS Application Load Balancer with configurable listeners, target groups, and security group management.

What You'll Build

  • Application Load Balancer with HTTP/HTTPS listeners
  • Target groups with health checks
  • Listener rules for advanced routing
  • Optional WAF integration
  • Security group for ALB inbound/egress rules

How to Use

module "alb" {
source = "github.com/nnthanh101/terraform-aws/modules/alb"

name = "app-alb"
load_balancer_type = "application"
internal = false
subnets = var.public_subnets
security_groups = [aws_security_group.alb.id]

listeners = {
http = {
port = 80
protocol = "HTTP"
forward = {
target_group_key = "app"
}
}
}

target_groups = {
app = {
name = "app-tg"
port = 8080
protocol = "HTTP"
target_type = "instance"
health_check = {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 3
interval = 30
path = "/"
matcher = "200"
}
}
}

tags = {
Environment = "prod"
Service = "api"
}
}

Key Variables

VariableTypePurpose
namestringLoad balancer name (max 32 chars, alphanumeric + hyphens)
load_balancer_typestringType: "application", "network", or "gateway"
internalboolInternal vs internet-facing LB
subnetslist(string)Subnets for LB deployment
security_groupslist(string)Security group IDs for network rules
listenersmap(object)Listener configurations (port, protocol, rules)
target_groupsmap(object)Target group definitions (health check, routing)
enable_deletion_protectionboolPrevent accidental deletion (default: true)

Outputs

OutputUse Case
idLoad balancer ID for resource references
arnARN for CloudWatch metrics, WAF association
dns_nameDNS CNAME for Route53 alias records
zone_idALB zone ID for Route53 alias setup
listenersMap of created listener resources
target_groupsMap of created target group resources

Integration

  • EC2 Instances: Register instances in additional_target_group_attachments
  • CloudWatch: Use arn_suffix for ALB metrics and alarms
  • WAF: Attach Web ACL via web_acl_arn variable
  • Route53: Create alias record pointing to dns_name
  • Auto Scaling: Attach ASG to target group via create_attachment

Source Reference

Module: terraform-aws/modules/alb