【问题标题】:Terraform - Dynamic variables argumentsTerraform - 动态变量参数
【发布时间】:2021-03-26 13:14:37
【问题描述】:

我觉得我已经尝试了很多不同的方法,但我在如何调用这些变量方面可能有点偏离。我有以下代码:

  config_rule_params = {
      "access_keys_rotated" = {
          "input_parameters" = "{\"maxAccessKeyAge\": \"90\"}",
          "maximum_execution_frequency" = "TwentyFour_Hours",
          "source" = {
              "owner" = "AWS",
              "source_identifier" = "ACCESS_KEYS_ROTATED"
          }
      },
      "acm_certificate_expiration_check" = {
          "input_parameters" = "{\"daysToExpiration\": \"30\"}",
          "maximum_execution_frequency" = "TwentyFour_Hours",
          "source" = {
              "owner" = "AWS",
              "source_identifier" = "ACM_CERTIFICATE_EXPIRATION_CHECK"
          },
          "scope" = {
              "compliance_resource_types" = "AWS::ACM::Certificate"
          }
      }
  }
}

resource "aws_config_config_rule" "parameterised_config_rules" {
    for_each                    = local.config_rule_params
    name                        = each.key
    input_parameters            = each.value.input_parameters
    maximum_execution_frequency = each.value.maximum_execution_frequency
    
    dynamic "source" {
        for_each = local.config_rule_params[*].source[*]
        content {
            owner = each.value.owner
            source_identifier = each.source_identifier
        }
    }

    dynamic "scope" {
        for_each = local.config_rule_params[*].scope[*]
        content {
            compliance_resource_types = each.value.compliance_resource_types
        }
    }
}

最终我将在config_rule_params 下添加大量规则,但并非所有规则都有sourcescope 甚至其他参数。创建资源时如何正确调用这些变量?当前出现以下错误:

Error: Unsupported attribute
  on .terraform/modules/baselines_config_rules_module/modules/baseline-config-rules/main.tf line 53, in resource "aws_config_config_rule" "parameterised_config_rules":
  53:         for_each = local.config_rule_params[*].source[*]
This object does not have an attribute named "source".
Error: Unsupported attribute
  on .terraform/modules/baselines_config_rules_module/modules/baseline-config-rules/main.tf line 61, in resource "aws_config_config_rule" "parameterised_config_rules":
  61:         for_each = local.config_rule_params[*].scope[*]
This object does not have an attribute named "scope".
ERROR: Job failed: exit code 1

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws terraform0.12+ aws-config


    【解决方案1】:

    您正确地使用 [*] 运算符作为一种简洁的方式来将可能为 null 或不为 null 的值调整为包含零个或一个元素的列表,但这里有两件事需要更改:

    • 默认情况下,dynamic 块的迭代器符号是您正在生成的块的名称。 each 是顶级资源本身的迭代器符号,即使在 dynamic 块内也是如此。
    • 作为上一项的结果,您可以在dynamic 块中使用each.value 作为for_each 表达式的一部分,以引用local.config_rule_params 的当前元素。

    将它们放在一起,我们会得到这样的结果:

    resource "aws_config_config_rule" "parameterised_config_rules" {
      for_each                    = local.config_rule_params
    
      name                        = each.key
      input_parameters            = each.value.input_parameters
      maximum_execution_frequency = each.value.maximum_execution_frequency
        
      dynamic "source" {
        for_each = each.value.source[*]
        content {
          owner             = source.value.owner
          source_identifier = source.value.source_identifier
        }
      }
    
      dynamic "scope" {
        for_each = each.value.scope[*]
        content {
          compliance_resource_types = scope.value.compliance_resource_types
        }
      }
    }
    

    请注意,在dynamic "source" 块中,当前元素是source.value,而在dynamic "scope" 块中,当前元素是scope.value。因此,在那些 dynamic 块中使用 each.value 是有效的,因此您可以在构建这些嵌套块时同时参考这两个重复级别。

    【讨论】:

    • 谢谢,这适用于源(我猜是因为这两个项目都有源参数)。第一项(access_keys_rotated,没有范围参数)的范围仍然存在错误:错误:不支持的属性 - for_each = each.value.scope[*]。 each.value 是具有 3 个属性的对象。此对象没有名为“范围”的属性。
    • 搞定了。只需将范围 + 范围值添加到所有项目(“范围”={“compliance_resource_types”=[]},但将其保留为空就可以了。再次感谢。
    • 确实,因为对象类型是由它们在该上下文中具有哪些属性定义的,所以显式设置为 null 的属性与完全省略一个属性相同:如果省略该属性,则该值具有不同的对象类型。
    【解决方案2】:

    当您在dynamic blocks 中使用for_each 时,默认情况下迭代器被引用到使用块的标签(sourcescope),而不是each

    迭代器参数(可选)设置代表复数值的当前元素的临时变量的名称。如果省略,则变量的名称默认为 动态块的标签(上例中的“设置”)。

    在您的示例中,它将是 sourcescope

        dynamic "source" {
            for_each = local.config_rule_params[*].source[*]
            content {
                owner = source.value.owner
                source_identifier = source.source_identifier
            }
        }
    
        dynamic "scope" {
            for_each = local.config_rule_params[*].scope[*]
            content {
                compliance_resource_types = scope.value.compliance_resource_types
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 2020-10-13
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多