简体   繁体   English

从 terraform 中的变量引用资源

[英]Reference resource from variable in terraform

I want to create an object through which I will iterate using for_each to create some:我想创建一个 object 通过它我将使用for_each进行迭代以创建一些:

  • google_service_account
  • google_project_iam_member

resources资源

So my object is more or less like this所以我的 object 或多或少是这样的

  service_accounts = {
    "gha_storage_admin_sa" = {
      create       = true
      project      = var.project_id
      account_id   = "id1"
      display_name = "GHA service account 1"
      role         = "roles/storage.admin"
    },
    "gha_cluster_scaling_sa" = {
      create       = false
      project      = var.project_id
      account_id   = "id2"
      display_name = "GHA service account 2"
      role         = google_organization_iam_custom_role.my_custom_role.id
    },
  }
resource "google_service_account" "service_account" {
  for_each = {
    for k, v in local.service_accounts: k => v
        if v.create
  }

  project      = each.value.project
  account_id   = each.value.account_id
  display_name = each.value.display_name
}


resource "google_project_iam_member" "member" {
  for_each = local.service_accounts

  project = var.project_id
  role    = each.value.role
  member  = "serviceAccount:${google_service_account.service_account[each.key].email}"
}

This works fine if the above is a local variable.如果上面是local变量,这很好用。

I want however to expose it as a regular variable .但是,我想将其公开为常规variable

My question is whether the referenced resource ( google_organization_iam_custom_role.my_custom_role.id ) in the second element can be somehow exposed as a variable.我的问题是第二个元素中的引用资源( google_organization_iam_custom_role.my_custom_role.id )是否可以以某种方式公开为变量。

My question is whether the referenced resource (google_organization_iam_custom_role.my_custom_role.id) in the second element can be somehow exposed as a variable.我的问题是第二个元素中的引用资源(google_organization_iam_custom_role.my_custom_role.id)是否可以以某种方式公开为变量。

Sadly its not possible .可悲的是它不可能 TF does not support dynamic variables. TF 不支持动态变量。 All variables' values must be know at plan time.在计划时必须知道所有变量的值。 You have to pass the actual value of google_organization_iam_custom_role.my_custom_role.id as input variable to your code.您必须将google_organization_iam_custom_role.my_custom_role.id的实际值作为输入变量传递给您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 terraform中for循环中的引用变量 - Reference variable in a for loop in terraform 如果变量不是 null,则创建 Terraform 资源 - Create Terraform resource if variable is not null 在双引号内使用 terraform 引用变量 - Use terraform reference variable inside double quotes Terraform 使用 count 循环变量和 if 语句来创建资源 - Terraform using count for both looping a variable and if statement to create the resource Terraform - 尽管已在同一文件中声明,但未找到变量的资源 - Terraform - Resource not found for variable despite having it declared in the same file terraform 将资源 ID 检索到变量中,以用于创建带有环境变量的 lambda - terraform retrieve resource id into variable to use in creation of lambda with environment variables Terraform:如何将 output 从一种资源传递到另一种资源? - Terraform: How to pass output from one resource to another? Terraform 计划获取错误:无法从 state 解码资源 - Terraform plan getting Error: Failed to decode resource from state Terraform 如果不是则设置资源 null - Terraform Set resource if not null 获取 Terraform 资源已存在错误,资源刚刚由 Terraform 创建 - Getting Terraform resource already exists error with resource just created by Terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM