terraform type_error ai_generated true

Error: Invalid value for variable: value must be a list of strings, got a string

ID: terraform/variable-type-constraint-violation

Also available as: JSON · Markdown · 中文
90%Fix Rate
87%Confidence
1Evidence
2023-12-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Terraform v1.5.0 active
Terraform v1.6.0 active
Terraform v1.7.0 active

Root Cause

A variable defined with a list type constraint was provided with a single string value instead of a list.

generic

中文

定义为列表类型约束的变量被提供了单个字符串值而不是列表。

Official Documentation

https://developer.hashicorp.com/terraform/language/values/variables#type-constraints

Workarounds

  1. 95% success In .tfvars file, use list syntax: 'variable_name = ["value1", "value2"]'.
    In .tfvars file, use list syntax: 'variable_name = ["value1", "value2"]'.
  2. 90% success For environment variables, use JSON encoding: 'export TF_VAR_variable_name='["value1","value2"]''.
    For environment variables, use JSON encoding: 'export TF_VAR_variable_name='["value1","value2"]''.
  3. 85% success If using -var flag: 'terraform apply -var='variable_name=["value"]''.
    If using -var flag: 'terraform apply -var='variable_name=["value"]''.

中文步骤

  1. 在 .tfvars 文件中,使用列表语法:'variable_name = ["value1", "value2"]'。
  2. 对于环境变量,使用 JSON 编码:'export TF_VAR_variable_name='["value1","value2"]''。
  3. 如果使用 -var 标志:'terraform apply -var='variable_name=["value"]''。

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The single string does not satisfy the list(type = list(string)) constraint.

  2. 80% fail

    Environment variables are always interpreted as strings, not lists.

  3. 70% fail

    Other parts of the configuration likely use for_each or other list-specific functions.