【问题标题】:Terraform: Issue getting values from map of mapTerraform:从地图中获取值的问题
【发布时间】:2020-02-12 03:08:11
【问题描述】:

我正在尝试从地图中获取值,它给出了以下错误。

错误:

Error: Incorrect attribute value type

  on ../../modules/lb-rules/rules.tf line 71, in resource "aws_alb_listener_rule" "http_header":
  71:         http_header_name = keys(lookup(var.listener_rules, "http_header_name", null))
    |----------------
    | var.listener_rules is object with 3 attributes

Inappropriate value for attribute "http_header_name": string required.

main.tf

module example {
...
...
listener_rules = {
    ...
    "http_header_name" = {
      "x-header" = "sample_server"
    }
  } 
    ...
}

rules.tf

resource "aws_alb_listener_rule" http_header {
  listener_arn = var.public_alb_listener_arn
  priority     = var.priority_number

  action {
    type             = "forward"
    target_group_arn = aws_alb_target_group.default.id
  }

  dynamic "condition" {
    for_each = lookup(var.listener_rules, "http_header_name", null)
    content {
      http_header {
        http_header_name = keys(lookup(var.listener_rules, "http_header_name", null))
        values           = ["hi"]
      }
    }
  }
}

【问题讨论】:

  • keys(lookup(var.listener_rules, "http_header_name", null)) 正在返回一个数组并且参数需要一个字符串类型。

标签: terraform terraform-provider-aws


【解决方案1】:

我不确定我是否完全理解您的目标,但看起来这个想法是在此 http_header_name 属性中为每个元素生成一个 condition 块。对于 Terraform v0.12.20 或更高版本,这是一种写法:

resource "aws_alb_listener_rule" http_header {
  listener_arn = var.public_alb_listener_arn
  priority     = var.priority_number

  action {
    type             = "forward"
    target_group_arn = aws_alb_target_group.default.id
  }

  dynamic "condition" {
    for_each = try(var.listener_rules.http_header_name, {})
    content {
      http_header {
        http_header_name = condition.key
        values           = [condition.value]
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2021-04-24
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多