You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在.tf.json资源中使用for_each时出现JSON值类型错误

Fixing Terraform JSON for_each Value Type Error

Let's break down what's going wrong and fix your gsuite_user resource configuration step by step.

Why You're Seeing the Error

The error message points to an incorrect JSON structure for your resource block. In Terraform's JSON syntax:

  • The resource.<type> object expects instance names mapped to their configurations (a valid JSON object).
  • You tried placing for_each directly at the top level of the gsuite_user object, which doesn't fit Terraform's JSON schema. Additionally, your primary_email expression was incorrectly referencing non-existent var.first_name/var.last_name variables instead of using the each.value from your employee_map.

Corrected Configuration

Here's the fixed main.tf.json with proper structure and corrected variable references:

{
  "resource": {
    "gsuite_user": {
      "*": {
        "for_each": "${var.employee_map}",
        "change_password_next_login": true,
        "name": {
          "family_name": "${each.value.last_name}",
          "given_name": "${each.value.first_name}"
        },
        "password": "password",
        "primary_email": "${each.value.first_name}.${each.value.last_name}@email",
        "lifecycle": {
          "ignore_changes": ["password", "change_password_next_login"]
        }
      }
    }
  }
}

Key Changes Made:

  • Added the "*" wildcard key: In Terraform JSON, this tells Terraform we're defining a dynamic resource template to be repeated via for_each, rather than a single named resource.
  • Moved for_each into the wildcard block: This places for_each in the correct location as a parameter for the dynamic resource template.
  • Fixed primary_email expression: Changed ${var.first_name}.${var.last_name}@email to ${each.value.first_name}.${each.value.last_name}@email to reference the first/last name from each entry in your employee_map.
  • Properly formatted JSON: Split the code into multiple lines for readability (minified works too, but this helps catch syntax errors).

Verify Your Variables File

Your variables file is mostly correct, but adding an explicit type definition improves validation and clarity:

{
  "variable": {
    "employee_map": {
      "type": "map(object({
        first_name = string
        last_name = string
      }))",
      "default": {
        "john.doe": { "first_name": "john", "last_name": "doe" },
        "jane.doey": { "first_name": "jane", "last_name": "doey" }
      }
    }
  }
}

Test the Fix

Run these commands to validate and apply your configuration:

terraform init
terraform validate
terraform plan

You should see Terraform plan to create two gsuite_user instances, one for each entry in your employee_map.

内容的提问来源于stack exchange,提问作者Benjamin Waters

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:23:37