错误:无效的属性值:给定的值不适用于属性 "tags":字符串值不能为 null
Error: Invalid attribute value: the given value is not suitable for attribute "tags": string values must not be null
ID: terraform/invalid-nullable-attribute
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Terraform 1.3.0 | active | — | — | — |
| Terraform 1.4.0 | active | — | — | — |
| Terraform 1.5.0 | active | — | — | — |
根因分析
期望字符串或字符串映射的资源属性接收到了 null 值,通常来自条件表达式或求值为 null 的变量。
English
A resource attribute that expects a string or map of strings is receiving a null value, often from a conditional expression or variable that evaluates to null.
官方文档
https://developer.hashicorp.com/terraform/language/expressions/type-constraints解决方案
-
使用 coalesce() 函数在变量为 null 时提供默认的非 null 值。
-
使用条件逻辑和 try() 结合类型约束确保 null 值被替换。或者,为变量定义默认的非 null 值。
-
如果属性是可选的并且可以省略,使用动态块或条件逻辑在值为 null 时完全跳过它。
无效尝试
常见但无效的做法:
-
60% 失败
Some AWS services reject empty strings for required tags. The provider may still throw an error if the service requires non-empty values.
-
85% 失败
If the attribute is required, Terraform will error with 'Required attribute is not set'. Removing it doesn't fix the null issue.
-
75% 失败
try() only catches errors, not null values. If the expression evaluates to null without error, try() returns null.