Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One listener rule with multiple target groups #399

Open
victoria-yaakov opened this issue Jan 26, 2025 · 0 comments
Open

One listener rule with multiple target groups #399

victoria-yaakov opened this issue Jan 26, 2025 · 0 comments

Comments

@victoria-yaakov
Copy link

Description

When using version 8.7.0, The listener rule is updated only with target-group-1 and not with target-group-2 together,
Manually via the AWS console- we can add up to 4 target groups to the same listener rule.

My code in terraform is:

module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 8.0"

name = "${var.customer}-alb"

load_balancer_type = "application"

vpc_id = var.vpc_id
subnets = var.public_subnets_ids
security_groups = [module.lb_sg.security_group_id]
create_security_group = false
preserve_host_header = true
drop_invalid_header_fields = true

https_listeners = [
{
port = 443
protocol = "HTTPS"
certificate_arn = var.load_balancer_acm_arn
target_group_index = 0
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06"
}
]

https_listener_rules = [
{
https_listener_index = 0
actions = [{
type = "fixed-response"
content_type = "text/html"
status_code = 403
message_body = "<img src="https://media.giphy.com/media/l0ErQ2UfBNFEIlqjC/giphy.gif" alt="this slowpoke moves" width="100%" height="100%" alt="404 image"/>"
}]

  conditions = [{
    path_patterns = ["/*"]
  }]
}

]

target_groups = [
{
name = "${var.customer}-target-group-1"
backend_protocol = "HTTP"
backend_port = 44
target_type = "instance"
health_check = {
enabled = true
path = "/ping"
port = "traffic-port"
protocol = "HTTP"
}
},
{
name = "${var.customer}-target-group-2"
backend_protocol = "HTTP"
backend_port = 44
target_type = "instance"
health_check = {
enabled = true
path = "/ping"
port = "traffic-port"
protocol = "HTTP"
}
}
]

Enable access logging

access_logs = {
bucket = var.access_logs_bucket
prefix = var.access_logs_bucket_prefix
enabled = true
}

tags = {
Environment = var.customer
}
}

resource "aws_lb_target_group_attachment" "ggr_attachments_1" {
count = var.ggr_count

target_group_arn = module.alb.target_groups["target_group_1"].arn
target_id = module.ggr_instance_1[count.index].id
port = 44
}

resource "aws_lb_target_group_attachment" "ggr_attachments_2" {
count = var.ggr_count

target_group_arn = module.alb.target_groups["target_group_2"].arn
target_id = module.ggr_instance_2[count.index].id
port = 44
}

⚠️ Note

Versions

  • Module version:

terraform -version
Terraform v1.5.7
on linux_amd64

  • provider registry.terraform.io/hashicorp/aws v5.84.0

Your version of Terraform is out of date! The latest version
is 1.10.5. You can update by downloading from https://www.terraform.io/downloads.html

I tried to use the latest version 9.13.0

module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 9.0"

name = "${var.customer}-alb"
load_balancer_type = "application"

vpc_id = var.vpc_id
subnets = var.public_subnets_ids
security_groups = [module.lb_sg.security_group_id]
create_security_group = false
preserve_host_header = true
drop_invalid_header_fields = true

target_groups = {
target_group_1 = {
name = "${var.customer}-target-group-1"
backend_protocol = "HTTP"
backend_port = 44
target_type = "instance"
health_check = {
enabled = true
path = "/ping"
port = "traffic-port"
protocol = "HTTP"
}
}
target_group_2 = {
name = "${var.customer}-target-group-2"
backend_protocol = "HTTP"
backend_port = 44
target_type = "instance"
health_check = {
enabled = true
path = "/ping"
port = "traffic-port"
protocol = "HTTP"
}
}
}

listeners = {
https = {
port = 443
protocol = "HTTPS"
certificate_arn = var.load_balancer_acm_arn
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06"
forward = {
target_group_key = "instance"
}

  rules = [{
    priority = 100
    actions = [{
      type = "forward"
      forward = {
        target_groups = [
          {
            arn    = module.alb.target_groups["target_group_1"].id
            weight = 1
          },
          {
            arn    = module.alb.target_groups["target_group_2"].id
            weight = 1
          }
        ]
      }
    }]

    conditions = [{
      path_pattern = {
        values = ["/*"]
      }
    }]
  },
  {
    priority = 101
    actions = [{
      type         = "fixed-response"
      content_type = "text/html"
      status_code  = 403
      message_body = "<img src=\"https://media.giphy.com/media/l0ErQ2UfBNFEIlqjC/giphy.gif\" alt=\"this slowpoke moves\" width=\"100%\" height=\"100%\" alt=\"404 image\"/>"
    }]

    conditions = [{
      path_pattern = {
        values = ["/*"]
      }
    }]
  }]
}

}

Enable access logging

access_logs = {
bucket = var.access_logs_bucket
prefix = var.access_logs_bucket_prefix
enabled = true
}

tags = {
Environment = var.customer
}
}

resource "aws_lb_target_group_attachment" "ggr_attachments_1" {
count = var.ggr_count

target_group_arn = module.alb.target_groups["target_group_1"].arn
target_id = module.ggr_instance_1[count.index].id
port = 44
}

resource "aws_lb_target_group_attachment" "ggr_attachments_2" {
count = var.ggr_count

target_group_arn = module.alb.target_groups["target_group_2"].arn
target_id = module.ggr_instance_2[count.index].id
port = 44
}

====================================================

The error that I get is

│ Error: Unsupported attribute

│ on .terraform/modules/alb/main.tf line 597, in resource "aws_lb_target_group_attachment" "this":
│ 597: target_id = each.value.target_id
│ ├────────────────
│ │ each.value is object with 5 attributes

│ This object does not have an attribute named "target_id".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant