首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Terraform 12.动态生成块

Terraform 12.动态生成块
EN

Stack Overflow用户
提问于 2020-09-07 06:44:25
回答 1查看 48关注 0票数 1

下面是我的列表:

代码语言:javascript
复制
local {
...
  step_scaling_out_policy_configuration = {
    "adjustment_type"         = "ChangeInCapacity"
    "cooldown"                = 300
    "metric_aggregation_type" = "Maximum"
    "step_adjustment"         = {
      "metric_interval_lower_bound" = 0
      "scaling_adjustment"          = 1
    }
  }
...
}

我可以使用next逻辑转换成块:

代码语言:javascript
复制
  step_scaling_policy_configuration {
    adjustment_type          = local.step_scaling_out_policy_configuration.adjustment_type
    cooldown                 = local.step_scaling_out_policy_configuration.cooldown
    metric_aggregation_type  = local.step_scaling_out_policy_configuration.metric_aggregation_type

    step_adjustment {
      metric_interval_lower_bound = local.step_scaling_out_policy_configuration.step_adjustment.metric_interval_lower_bound
      scaling_adjustment          = local.step_scaling_out_policy_configuration.step_adjustment.scaling_adjustment
    }
  }

在map中使用固定数量的键是非常简单的。但是有没有可能动态地生成这样的块,因为我不知道local.step_scaling_out_policy_configuration映射中到底是什么。现在假设我有

代码语言:javascript
复制
local {
...
  step_scaling_out_policy_configuration = {
    "adjustment_type"         = "ChangeInCapacity"
    "cooldown"                = 300
    "metric_aggregation_type" = "Maximum"
    #######
    "some_new_key"            = "value"
    #######
    "step_adjustment"         = {
      "metric_interval_lower_bound" = 0
      "scaling_adjustment"          = 1
    }
  }
...
}

显然,在前面的逻辑中,我不会在step_scaling_policy_configuration块中有some_new_key参数。是否可以根据local.step_scaling_out_policy_configuration映射中的内容动态地将键添加到step_scaling_policy_configuration块中?

EN

回答 1

Stack Overflow用户

发布于 2020-09-08 08:04:07

您可以使用try()lookup()检查密钥是否存在,如果缺少,则将其设置为null。在大多数提供者中,null就像不设置密钥一样。有时,将其设置为显式的默认值或en空字符串""可能是有意义的。null在大多数情况下都工作得很好,并使用默认提供程序。

使用try()lookup()解决方案的示例代码:

代码语言:javascript
复制
step_scaling_policy_configuration {
  ....

  some_new_key   = try(local.step_scaling_out_policy_configuration.some_new_key, null)
  some_other_key = lookup(local.step_scaling_out_policy_configuration, "some_other_key", null)

  step_adjustment {
    ....
  }
}

如果您还想让step_scaling_policy_configuration和/或step_adjustment依赖于本地地图中是否存在值,请查看terraform文档中的dynamic-blocks

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63769507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档