地形。如何使用 for_each 识别元素(键)

Terraform. How to identify element (key) using for_each

我正在与目标群体一起构建 AWS 网络 LB。我坚持 aws_lb_listener 在几个 http_tcp_listeners 中添加几个 target_group_arn IE。我有两个 aws_lb_target_group 用于 80 和 443 端口,两个 http_tcp_listeners 用于这些相同的端口。

但我收到此错误消息:

in resource "aws_lb_listener" "frontend_http_tcp":
│  172:       target_group_arn = each.value.arn
│     ├────────────────
│     │ each.value is map of string with 4 elements
│ 
│ This map does not have an element with the key "arn".
variable "aws_lb_target_group" {
  description = "aws_lb_target_group"
  type        = map(any)
  default = {
    http = {
      name                 = "http"
      target_type          = "instance"
      port                 = 80
      protocol             = "TCP"
      protocol_version     = "HTTP1"
      type                 = "source_ip"
      enabled              = false
      path_health_check    = "/health.html"
      matcher_health_check = "200" # has to be HTTP 200 or fails
    },
    https = {
      name                 = "https"
      target_type          = "instance"
      port                 = 443
      protocol             = "TCP"
      protocol_version     = "HTTP2"
      type                 = "source_ip"
      enabled              = false
      path_health_check    = "/health.html"
      matcher_health_check = "200" # has to be HTTP 200 or fails
    }
  }
}

variable "http_tcp_listeners" {
  description = "aws_lb_listener"
  type        = map(any)
  default = {
    http = {
      port        = "80"
      protocol    = "TCP"
      action_type = "forward"
      alpn_policy = "HTTP1Only"
    },
    https = {
      port        = "443"
      protocol    = "TCP"
      action_type = "forward"
      certificate_arn = "data.terraform_remote_state.acm.outputs.acm_certificate_arn"
      alpn_policy     = "HTTP2Preferred"
    }
  }
}
resource "aws_lb_target_group" "main" {
  for_each         = var.aws_lb_target_group
  name             = "test-group-${random_pet.this.id}-${each.value.name}"
  target_type      = each.value.target_type
  port             = each.value.port
  protocol         = each.value.protocol
  protocol_version = each.value.protocol_version
  vpc_id           = local.vpc_id

  stickiness {
    type    = "source_ip"
    enabled = false
  }

  health_check {
    path                = each.value.path_health_check
    port                = each.value.port
    healthy_threshold   = 3
    unhealthy_threshold = 3
    interval            = 30
  }

  depends_on = [
    aws_lb.main,
  ]
}

resource "aws_lb_listener" "frontend_http_tcp" {
  for_each          = var.http_tcp_listeners
  load_balancer_arn = aws_lb.main.arn
  port              = each.value.port
  protocol          = each.value.protocol
  certificate_arn   = data.terraform_remote_state.acm.outputs.acm_certificate_arn
  alpn_policy       = each.value.alpn_policy

  dynamic "default_action" {
    for_each = aws_lb_target_group.main

    content {
      type             = "forward"
      target_group_arn = each.value.arn
    }
  }

  depends_on = [
    aws_lb.main,
    aws_lb_target_group.main,
  ]
}

当您使用动态块时,您的密钥不是 each,而是块的名称。所以我觉得应该是:

target_group_arn = default_action.value.arn

要只有一个 default_action,请尝试:

  default_action {
      type             = "forward"
      target_group_arn = aws_lb_target_group.main[each.key].arn
  }