terraform type_error ai_generated true

错误:无效的属性值:给定的值不适用于属性 "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

其他格式: JSON · Markdown 中文 · English
85%修复率
85%置信度
1证据数
2024-02-14首次发现

版本兼容性

版本状态引入弃用备注
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.

generic

官方文档

https://developer.hashicorp.com/terraform/language/expressions/type-constraints

解决方案

  1. 使用 coalesce() 函数在变量为 null 时提供默认的非 null 值。
  2. 使用条件逻辑和 try() 结合类型约束确保 null 值被替换。或者,为变量定义默认的非 null 值。
  3. 如果属性是可选的并且可以省略,使用动态块或条件逻辑在值为 null 时完全跳过它。

无效尝试

常见但无效的做法:

  1. 60% 失败

    Some AWS services reject empty strings for required tags. The provider may still throw an error if the service requires non-empty values.

  2. 85% 失败

    If the attribute is required, Terraform will error with 'Required attribute is not set'. Removing it doesn't fix the null issue.

  3. 75% 失败

    try() only catches errors, not null values. If the expression evaluates to null without error, try() returns null.