【问题标题】:Computed attributes cannot be set : org_id无法设置计算属性:org_id
【发布时间】:2021-11-21 08:12:04
【问题描述】:

我正在学习如何使用 Terraform。我的目标是在 GCP 上部署架构,所以到目前为止,这是我的 main.tf

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "3.85.0"
    }
  }
}
provider "google" {
  credentials = file(var.credentials_file)
  region      = var.region
  zone        = var.zone
}

data "google_organization" "org" {
  domain                = var.organization.display_name
  org_id                = var.organization.id
  directory_customer_id = var.organization.directory_customer_id
}
resource "google_folder" "shared" {
  display_name = "Shared"
  parent       = google_organization.org_id
}

resource "google_folder" "ddm" {
  display_name = "Data and Digital Marketing"
  parent       = google_folder.shared.name
}

resource "google_folder" "dtl" {
  display_name = "DTL"
  parent       = google_folder.ddm.name
}

根据documentationorg_id属性参考内

但我收到以下错误:

╷
│ Error: Computed attributes cannot be set
│
│   with data.google_organization.org,
│   on main.tf line 17, in data "google_organization" "org":
│   17:   org_id                = var.organization.id
│
│ Computed attributes cannot be set, but a value was set for "org_id".
╵
╷
│ Error: Computed attributes cannot be set
│
│   with data.google_organization.org,
│   on main.tf line 18, in data "google_organization" "org":
│   18:   directory_customer_id = var.organization.directory_customer_id
│
│ Computed attributes cannot be set, but a value was set for "directory_customer_id".
╵
╷
│ Error: Reference to undeclared resource
│
│   on main.tf line 22, in resource "google_folder" "shared":
│   22:   parent       = google_organization.org_id
│
│ A managed resource "google_organization" "org_id" has not been declared in the root module.

我做错了什么?

【问题讨论】:

  • 我猜你可能是在导入一个现有的 org id 到 Terraform 之后?您是在尝试创建一个新组织还是拥有一个现有组织并希望在 Terraform 中创建它?
  • 我有一个 terraform 中的现有组织,但我想概述它,以及 main.tf 中的文件夹层次结构
  • @GrzegorzOledzki,老实说,我不确定我是否可以使用服务帐户来做到这一点。也许我需要一个通过超级管理员帐户应用的“引导”terraform,它可以导入组织并创建文件夹结构。另外,我还有另一个通过服务帐户在项目中创建项目和资源...你怎么看?

标签: terraform terraform-provider-gcp


【解决方案1】:

组织被设置为data 源,但在前面的代码中,它被用作resource 块。

引用组织需要做的是:

data "google_organization" "org" {
  organization          = var.organization.id
}

org_id 是输出,而不是输入。唯一可接受的输入是organizationdomain;它们是相互排斥的。

并像这样使用它的输出:

resource "google_folder" "shared" {
  display_name = "Shared"
  parent       = data.google_organization.org.org_id
}

编辑:虽然语法正确,但它可能不起作用,因为使用的帐户必须是组织级别的组织管理员。我不建议使用 google_organization 数据源来获取 ID 和其他信息,我最终将它们写入变量并以这种方式调用它:

resource "google_folder" "shared" {
  display_name = "Shared"
  parent       = "organizations/${var.organization.id}"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多