错误:无效的动态块迭代器:动态块的迭代器必须是单个单词,不能是复杂表达式
Error: Invalid dynamic block iterator: The iterator for a dynamic block must be a single word, not a complex expression
ID: terraform/dynamic-block-iterator-invalid
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Terraform 1.4.0 | active | — | — | — |
| Terraform 1.5.0 | active | — | — | — |
| Terraform 1.6.0 | active | — | — | — |
根因分析
动态块的 iterator 参数被设置为复杂表达式(如函数调用或属性引用),而不是简单的变量名。
English
The dynamic block's iterator argument is set to a complex expression (like a function call or attribute reference) instead of a simple variable name.
官方文档
https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks解决方案
-
使用一个简单的、单词的标识符作为迭代器,且该标识符在当前作用域中未被使用。例如,如果遍历标签列表,使用 'tag' 作为迭代器。
-
如果有嵌套的动态块,为每一层使用不同的迭代器名称,例如 'outer' 和 'inner'。
无效尝试
常见但无效的做法:
-
90% 失败
The iterator must be a new variable name, not an existing expression. Terraform uses it to create a temporary variable within the block.
-
60% 失败
The default iterator 'each' works only if not already used by another dynamic block in the same resource. If 'each' is used elsewhere, it causes conflicts.
-
95% 失败
The iterator must be an unquoted identifier, not a string. Quoting makes it a string literal, which is not a valid variable name.