下面是我的列表:
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逻辑转换成块:
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映射中到底是什么。现在假设我有
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块中?
发布于 2020-09-08 08:04:07
您可以使用try()或lookup()检查密钥是否存在,如果缺少,则将其设置为null。在大多数提供者中,null就像不设置密钥一样。有时,将其设置为显式的默认值或en空字符串""可能是有意义的。null在大多数情况下都工作得很好,并使用默认提供程序。
使用try()和lookup()解决方案的示例代码:
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。
https://stackoverflow.com/questions/63769507
复制相似问题