본문 바로가기

개발하자

how can we add project number from variable in terraform gcp resource iam binding

반응형

how can we add project number from variable in terraform gcp resource iam binding

Below is my terraform resource. how can we add project number from variable in terraform gcp resource iam binding because if i will run same terraform for other account, i have to change it manually.

resource "google_project_iam_binding" "project" {
  project = var.projectid
  role    = "roles/container.admin"

  members = [
    "serviceAccount:service-1016545346555@gcp-sa-cloudbuild.iam.gserviceaccount.com",
  ]
}



You can use google_client_config data-source to access the configuration of the provider.

First, add the following data-source block to main.tf:

data "google_client_config" "current" {}

Then, you would be able to access the project_id as below:

output "project_id" {
  value = data.google_client_config.current.project
}

For more information, please refer to: https://www.terraform.io/docs/providers/google/d/client_config.html




The project number is found in the google_project data-source.

So when this one is added:

data "google_project" "project" {}

it should be accessible using:

data.google_project.project.number

반응형