# Create an Application Load Balancer
resource "aws_lb" "main" {
name = "api-alb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb.id]
subnets = var.public_subnet_ids
}
resource "aws_lb_target_group" "api" {
name = "api-target-group"
port = 8080
protocol = "HTTP"
vpc_id = var.vpc_id
target_type = "ip"
health_check {
enabled = true
healthy_threshold = 2
interval = 30
matcher = "200"
path = "/health"
port = "traffic-port"
protocol = "HTTP"
timeout = 5
unhealthy_threshold = 3
}
}
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.main.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.api.arn
}
}
# Deploy ECS service with ALB integration
module "api_service" {
source = "registry.patterneddesigns.ca/patterneddesigns/ecs-service/aws"
version = "2.0.0"
service_name = "api-service"
cluster_arn = aws_ecs_cluster.main.arn
task_definition_arn = aws_ecs_task_definition.api.arn
desired_count = 3
load_balancer = {
target_group_arn = aws_lb_target_group.api.arn
container_name = "api"
container_port = 8080
}
network_configuration = {
subnets = var.private_subnet_ids
security_groups = [aws_security_group.ecs_tasks.id]
assign_public_ip = false
}
health_check_grace_period_seconds = 60
}