I try to use the module, AWS Application and Network Load Balancer (ALB & NLB) Terraform module, https://github.com/terraform-aws-modules/terraform-aws-alb. There is a sample usage of Application Load Balancer, see below:
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 5.0"
name = "my-alb"
load_balancer_type = "application"
vpc_id = "vpc-abcde012"
subnets = ["subnet-abcde012", "subnet-bcde012a"]
security_groups = ["sg-edcd9784", "sg-edcd9785"]
access_logs = {
bucket = "my-alb-logs"
}
target_groups = [
{
name_prefix = "pref-"
backend_protocol = "HTTP"
backend_port = 80
target_type = "instance"
}
]
https_listeners = [
{
port = 443
protocol = "HTTPS"
certificate_arn = "arn:aws:iam::123456789012:server-certificate/test_cert-123456789012"
target_group_index = 0
}
]
http_tcp_listeners = [
{
port = 80
protocol = "HTTP"
target_group_index = 0
}
]
tags = {
Environment = "Test"
}
}
I understand the target_groups is an array. But, why target_group_index = 0 not target_group_index = 1 or target_group_index = 2? What is the index 0?
In the example you've posted, there is only one target group (TG):
target_groups = [
{
name_prefix = "pref-"
backend_protocol = "HTTP"
backend_port = 80
target_type = "instance"
}
]
Thus the listeners use target_group_index = 0
to indicate that they apply to this one specific TG. If you have more TGs, you would use target_group_index = 1, 2, 3 ...
to specify which listener applies to each TG.