ALB
ALB will expose our application to outer world and will handle all incoming traffic.
Application Load Balancer
- Create a
resource
block with typeaws_alb
and namethis
with parameters:name
="${var.service_name}-alb"
subnets
=data.aws_subnet_ids.public.ids
security_groups
=[aws_security_group.alb.id]
Application Load Balancer Target Group
- Create a
resource
block with typeaws_alb_target_group
and namefrontend
with parameters:name
="${var.service_name}-tg"
port
=80
protocol
="HTTP"
vpc_id
=var.vpc_id
target_type
="ip"
Application Load Balancer Listener
- Create a
resource
block with typeaws_alb_listener
and namefrontend
with parameters:load_balancer_arn
=aws_alb.this.id
port
=80
protocol
="HTTP
"- Default Action:
default_action { target_group_arn = aws_alb_target_group.frontend.id type = "forward" }
Check your configuration
To check your Terraform configuration run
terraform plan
It will show you the resources that Terraform will create,delete or change. It will not apply anything.
Answer
terraform/alb.tf
resource "aws_alb" "this" {
name = "${var.service_name}-alb"
subnets = data.aws_subnet_ids.public.ids
security_groups = [aws_security_group.alb.id]
}
resource "aws_alb_target_group" "frontend" {
name = "${var.service_name}-tg"
port = 80
protocol = "HTTP"
vpc_id = var.vpc_id
target_type = "ip"
}
resource "aws_alb_listener" "frontend" {
load_balancer_arn = aws_alb.this.id
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = aws_alb_target_group.frontend.id
type = "forward"
}
}