【问题标题】:Terraform. How to identify element (key) using for_each地形。如何使用 for_each 识别元素(键)
【发布时间】:2021-07-07 09:15:26
【问题描述】:

我正在为目标群体构建 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,
  ]
}

【问题讨论】:

    标签: amazon-ec2 terraform aws-load-balancer


    【解决方案1】:

    当您使用动态块时,您的密钥不是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
      }
    

    【讨论】:

    • Marcin 谢谢!这解决了我的问题,但我得到了与 AWS 相关的新问题。 ``` 错误:创建 ELBv2 侦听器时出错 (arn:aws:elasticloadbalancing:us-east-1:.....): ValidationError: 必须只有一个操作 ... 使用 aws_lb_listener.frontend_http_tcp["http"],使用 aws_lb_listener.frontend_http_tcp["https"],在资源 "aws_lb_listener" "frontend_http_tcp": ```
    • @RostyslavMalenko 您当前的代码创建了两个默认操作。让我编辑答案。
    • 感谢您的帮助。这项工作target_group_arn = aws_lb_target_group.main[each.value].arn 但使用aws_lb_target_group.main[each.key].arn 而不是each.value 我的下一步是如果protocol =! TLS 然后不使用变量certificate_arn。
    • @RostyslavMalenko 谢谢。答案更新为each.key。不确定,TLS 是一个新问题吗?如果是这样,可以提出一个新问题。
    • Marcin 你介意看看这个问题吗,请Terraform set a variable with conditional another variable present。
    猜你喜欢
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多