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
| Variable | Type | Purpose |
|---|---|---|
name | string | Load balancer name (max 32 chars, alphanumeric + hyphens) |
load_balancer_type | string | Type: "application", "network", or "gateway" |
internal | bool | Internal vs internet-facing LB |
subnets | list(string) | Subnets for LB deployment |
security_groups | list(string) | Security group IDs for network rules |
listeners | map(object) | Listener configurations (port, protocol, rules) |
target_groups | map(object) | Target group definitions (health check, routing) |
enable_deletion_protection | bool | Prevent accidental deletion (default: true) |
Outputs
| Output | Use Case |
|---|---|
id | Load balancer ID for resource references |
arn | ARN for CloudWatch metrics, WAF association |
dns_name | DNS CNAME for Route53 alias records |
zone_id | ALB zone ID for Route53 alias setup |
listeners | Map of created listener resources |
target_groups | Map of created target group resources |
Integration
- EC2 Instances: Register instances in
additional_target_group_attachments - CloudWatch: Use
arn_suffixfor ALB metrics and alarms - WAF: Attach Web ACL via
web_acl_arnvariable - 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