【问题标题】:Terraform Argument Block as a List of MapsTerraform 参数块作为地图列表
【发布时间】:2021-11-07 08:26:49
【问题描述】:

我正在尝试在我的 query_string 元组中获取这两个元素。但是,只有最后一个元素被拾取。下面是我的 test/main.tf 文件:

terraform {
  required_version = ">= 0.13.0"

  required_providers {
    aws = ">= 3.58"
  }
}

provider "aws" {
  region = "us-east-1"
}

module "test_module" {
  source = "../"

  lb = [
    {
      name                       = "lb-name"
      enable_deletion_protection = true
      internal                   = false
      load_balancer_type         = "application"

      lb_listener = [
        {
          port       = "8080"
          protocol   = "HTTP"
          ssl_policy = "ELBSecurityPolicy-2016-08"

          default_action = {
            type  = "redirect"
            order = 1

            redirect = {
              status_code = "HTTP_302"
            }
          }

          lb_listener_rule = [
            {
              action = {
                type = "fixed-response"

                fixed_response = {
                  content_type = "text/plain"
                  message_body = "This is a message body."
                }
              }

              condition = {
                query_string = [
                  {
                    key   = "health"
                    value = "check"
                  },
                  {
                    value = "bar"
                  }
                ]
              }
            }
          ]
        }
      ]
    }
  ]
}

这是我的 lb.tf 文件:

variable "lb" {
  description = <<DESCRIPTION
  The AWS Elastic Load Balancing v2 module.
  DESCRIPTION
  type    = any
  default = []
}

locals {
  lb_listener_flat_list = flatten([
    for lb in var.lb : [
      for lb_listener in lookup(lb, "lb_listener", []) : {
        lb_name                          = lb.name
        lb_listener_load_balancer_arn    = aws_lb.lb[lb.name].arn
        lb_listener_port                 = lookup(lb, "load_balancer_type", "application") != "gateway" ? lookup(lb_listener, "port", null) : null
        lb_listener_protocol             = lookup(lb, "load_balancer_type", "application") != "gateway" || lookup(lb, "ip_address_type", null) != "dualstack" ? lookup(lb_listener, "protocol", null) : null
        lb_listener_ssl_policy           = lookup(lb_listener, "protocol", null) != "HTTPS" || lookup(lb_listener, "protocol", null) != "TLS" ? lb_listener.ssl_policy : lookup(lb_listener, "ssl_policy", null)
        lb_listener_default_action_type  = lb_listener.default_action.type
        lb_listener_default_action_order = lookup(lb_listener.default_action, "order", null)

        lb_listener_default_action_redirect = lookup(lb_listener.default_action, "redirect", {}) != {} ? {
          lb_listener_default_action_redirect_status_code = lb_listener.default_action.redirect.status_code
        } : null
      }
    ]
  ])

  lb_listener_rule_flat_list = flatten([
    for lb in var.lb : [
      for lb_listener in lookup(lb, "lb_listener", []) : [
        for lb_listener_rule in lookup(lb_listener, "lb_listener_rule", []) : [
          # I've recently added the line below in an attempte to loop through the list of maps for query_string
          for index in range(length(lookup(lb_listener_rule.condition, "query_string", []))) : {
            lb_name                       = lb.name
            lb_listener_rule_index        = index
            lb_listener_rule_listener_arn = aws_lb_listener.lb_listener[lb.name].arn
            lb_listener_rule_action_type  = lb_listener_rule.action.type

            lb_listener_rule_action_fixed_response = lookup(lb_listener_rule.action, "fixed_response", {}) != {} ? {
              lb_listener_rule_action_fixed_response_content_type = lb_listener_rule.action.fixed_response.content_type
              lb_listener_rule_action_fixed_response_message_body = lookup(lb_listener_rule.action.fixed_response, "message_body", null)
            } : null

            lb_listener_rule_condition_query_string = lookup(lb_listener_rule.condition, "query_string", {}) != {} ? {
              lb_listener_rule_condition_query_string_key   = lookup(lb_listener_rule.condition.query_string[index], "key", null)
              lb_listener_rule_condition_query_string_value = lb_listener_rule.condition.query_string[index].value
            } : null
          }
        ]
      ]
    ]
  ])

  lb_map               = { for lb in var.lb : lb.name => lb }
  lb_listener_map      = { for lb_listener in local.lb_listener_flat_list : lb_listener.lb_name => lb_listener }
  lb_listener_rule_map = { for lb_listener_rule in local.lb_listener_rule_flat_list : "${lb_listener_rule.lb_name}-[${lb_listener_rule.lb_listener_rule_index}]" => lb_listener_rule }
}

resource "aws_lb_listener" "lb_listener" {
  for_each          = local.lb_listener_map
  load_balancer_arn = each.value.lb_listener_load_balancer_arn
  port              = each.value.lb_listener_port
  protocol          = each.value.lb_listener_protocol
  ssl_policy        = each.value.lb_listener_ssl_policy

  default_action {
    type  = each.value.lb_listener_default_action_type
    order = each.value.lb_listener_default_action_order

    dynamic "redirect" {
      for_each = each.value.lb_listener_default_action_redirect != null ? [{}] : []
      content {
        status_code = each.value.lb_listener_default_action_redirect.lb_listener_default_action_redirect_status_code
      }
    }
  }
}

resource "aws_lb_listener_rule" "lb_listener_rule" {
  for_each     = local.lb_listener_rule_map
  listener_arn = each.value.lb_listener_rule_listener_arn

  action {
    type = each.value.lb_listener_rule_action_type

    dynamic "fixed_response" {
      for_each = each.value.lb_listener_rule_action_fixed_response != null ? [{}] : []
      content {
        content_type = each.value.lb_listener_rule_action_fixed_response.lb_listener_rule_action_fixed_response_content_type
        message_body = each.value.lb_listener_rule_action_fixed_response.lb_listener_rule_action_fixed_response_message_body
      }
    }
  }

  condition {
    dynamic "query_string" {
      for_each = each.value.lb_listener_rule_condition_query_string != null ? [{}] : []
      content {
        key   = each.value.lb_listener_rule_condition_query_string.lb_listener_rule_condition_query_string_key
        value = each.value.lb_listener_rule_condition_query_string.lb_listener_rule_condition_query_string_value
      }
    }
  }
}

resource "aws_lb" "lb" {
  for_each                   = local.lb_map
  name                       = lookup(each.value, "name", null)
  enable_deletion_protection = lookup(each.value, "enable_deletion_protection", null)
  internal                   = lookup(each.value, "internal", null)
  ip_address_type            = lookup(each.value, "ip_address_type", null)
  load_balancer_type         = lookup(each.value, "load_balancer_type", null)
}

这是我运行terraform plancondition 块的外观:

condition {
  query_string {
    value = "bar"
  }
}

这是我希望 condition 块的外观:

condition {
  query_string {
    key   = "health"
    value = "check"
  }

  query_string {
    value = "bar"
  }
}

【问题讨论】:

  • 您确定这是您的代码吗?它甚至不会编译,因为它有错误的语法,例如each.value.lb_listener_rule_action_fixed_response != null [{}] : [] 是错误的,因为没有这样的表达。因此,尚不清楚您甚至可以如何应用它。请确保您的问题真实地代表您的真实代码。
  • @Marcin,是的,这是我的代码,已编辑。我用眼睛创建了这篇文章。我将编辑我的帖子并确保它正常工作。我很抱歉。
  • 我不确定你编辑了什么。它对我来说仍然是相同的语法错误。
  • @Marcin,现在更新了。我在第一次发布代码时对其进行了编辑。我的原始文件很长。
  • 进展如何?仍然遇到同样的问题?

标签: amazon-web-services terraform terraform-provider-aws


【解决方案1】:

您的代码虽然复杂,但正确创建了两个query_string

问题中发布的表单中的代码有问题数量,它不起作用。但是当您写道“我的原始文件很长。”时,我猜您的真实代码确实有效。因此我只关注它的验证,而不是修复所有可能的错误,因为我不知道你的实际代码是什么。

【讨论】:

  • 文件现在应该编译。我刚刚再次测试了它们。但我注意到这两个块仍然被创建。关于为什么它只输出query_string 中的最后一个块而不是两者都输出的任何想法?
  • @user6005619 我不知道你改变了什么。我仍然看到很多问题。例如,不能将ELBSecurityPolicy-2016-08 与HTTP 一起使用,由于无限循环,不能将端口8080 重定向到8080,等等。编写的代码永远不会成功应用。你的query_string 很好,它的工作原理如屏幕截图所示。我必须解决所有问题才能使其正常工作,但我的答案只关注query_string。我不知道 tf plan 显示什么,但 tf apply 有效(在我修改后)并创建了这两个条件。
  • 有趣。谢谢你。我从未尝试过实际应用它。我输入的值仅用于测试。我去申请一下。
  • @user6005619 当然,没问题。让我知道它会如何进行。稍后,如果您发现我的 cmets 和答案有帮助,将不胜感激。
  • @user6005619 进展如何? query_string 运气好吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-04
  • 1970-01-01
  • 2020-11-17
  • 2019-06-09
  • 2022-01-14
  • 2022-01-12
相关资源
最近更新 更多