terraform config_error ai_generated true

Error: Invalid index: output value is a list but no index was specified

ID: terraform/invalid-index-in-output

Also available as: JSON · Markdown · 中文
88%Fix Rate
85%Confidence
1Evidence
2023-09-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Terraform >=1.0 active
AWS Provider 5.x active

Root Cause

A module output that returns a list is referenced without an index, causing a type mismatch when Terraform expects a single value.

generic

中文

模块输出返回列表,但在引用时未指定索引,导致 Terraform 期望单个值时类型不匹配。

Official Documentation

https://developer.hashicorp.com/terraform/language/expressions/references#outputs

Workarounds

  1. 90% success Reference the output with an explicit index, e.g., `module.my_module.output_name[0]` or use `element(module.my_module.output_name, 0)`.
    Reference the output with an explicit index, e.g., `module.my_module.output_name[0]` or use `element(module.my_module.output_name, 0)`.
  2. 85% success If you need all values, use `module.my_module.output_name[*]` to preserve the list type.
    If you need all values, use `module.my_module.output_name[*]` to preserve the list type.

中文步骤

  1. 使用显式索引引用输出,例如 `module.my_module.output_name[0]` 或使用 `element(module.my_module.output_name, 0)`。
  2. 如果需要所有值,使用 `module.my_module.output_name[*]` 以保留列表类型。

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Outputs do not support count; this fix leads to a syntax error.

  2. 70% fail

    Alters the module's contract and may cause downstream errors.