terraform config_error ai_generated true

错误:无效的资源实例键:资源实例键必须是正整数或字符串

Error: Invalid resource instance key: resource instance key must be a positive integer or a string

ID: terraform/resource-config-key-too-deep

其他格式: JSON · Markdown 中文 · English
88%修复率
82%置信度
1证据数
2023-10-20首次发现

版本兼容性

版本状态引入弃用备注
1.5.0 active
1.6.0 active

根因分析

在 Terraform 期望简单整数或字符串时,使用了复杂表达式(如列表或映射)作为资源实例键。

English

Using a complex expression (like a list or map) as the key in a resource instance when Terraform expects a simple integer or string.

generic

官方文档

https://developer.hashicorp.com/terraform/language/resources/syntax

解决方案

  1. 将键表达式简化为单个变量或字面值。例如,如果键应为字符串,使用 'var.environment' 而不是 'var.environment[0]'。
  2. 重构配置,使用具有简单键的映射的 for_each,或使用 count 与列表。

无效尝试

常见但无效的做法:

  1. 90% 失败

    Wrapping the expression in tostring() or tolist() does not change the fundamental type mismatch.

  2. 85% 失败

    Using count.index or for_each.key in a nested resource block still fails because the key must be a literal.