terraform - Handling null with for_each
问题描述
How can I handle the following where the variables
var can be null
locals {
tf_variables = (var.variables == null) ? null : jsondecode(var.variables)["variables"]
}
resource "tfe_variable" "this" {
for_each = local.tf_variables
...
}
I'm hitting
Error: Invalid for_each argument
on ..\..\main.tf line 63, in resource "tfe_variable" "this":
63: for_each = local.tf_variables
The given "for_each" argument value is unsuitable: the given "for_each"
argument value is null. A map, or set of strings is allowed.
解决方案
You could replace the null
with an empty set, list or map instead.
Changing your local to this should work:
locals {
tf_variables = (var.variables == null) ? [] : jsondecode(var.variables)["variables"]
}
As an aside you shouldn't need to use jsondecode
there because var.variables
must already be a serialised HCL object.
推荐阅读
- javascript - 动态下拉,同时允许为子下拉选择多个选项
- mysql - 如何将 laravel 模型保存到 2 个数据库
- node.js - 将域用于服务器时出现套接字 IO CORS 问题
- python - 在具有多个参数和多个输出的 pandas 中应用函数
- c - 如何在 epoll 上使用具有级别触发行为的 eventfd?
- java - 如何找到离用户最近的标记?
- perl - 无法从 git pull 的输出创建数组
- kubernetes - 尝试连接jenkins和kubernetes时,Jenkins作业抛出如下错误
- javascript - 使用 jQuery 请求在 HTML 上显示 svg
- python-3.x - Qt:会话管理错误:不支持指定的身份验证协议