Search code examples
google-cloud-platformgoogle-bigqueryterraformterraform-provider-gcpterraform0.12+

Multiple resource creation using terraform


I am trying to create multiple GCP projects using terraform, In each project i am trying to create multiple SA's and custom roles.

As as initial step i am able to create multiple projects but i am unable to create resources in those projects.

Eg:

resource "google_project" "project_creation" {
  count = 5
  name       = "terraform-testing${count.index + 1}"
  project_id = "terraform-testing${count.index + 1}"
}

output "gcp_projects" {
  value = ["${google_project.project_creation[*].id}"]
}

resource "google_project_iam_custom_role" "role" {
  project = google_project.project_creation[*].id 
  role_id     = "myCustomRole"
  title       = "Bigquery Role"
  description = "Bigquery Role"
  permissions = ["roles/bigquery.admin"]
}

Error: │ google_project.project_creation is tuple with 2 elements │ Inappropriate value for attribute "project": string required.


Solution

  • the project attribute expects a string. however you are passing it a tuple of all the project ids. If you want to create a resource for each project then iterate over the projects with something like

    resource "google_project" "project_creation" {
      count = 5
      name       = "terraform-testing${count.index + 1}"
      project_id = "terraform-testing${count.index + 1}"
    }
    
    output "gcp_projects" {
      value = ["${google_project.project_creation[*].id}"]
    }
    
    resource "google_project_iam_custom_role" "role" {
      count = length(google_project.project_creation)
      project = google_project.project_creation[count.index].id
      role_id     = "myCustomRole"
      title       = "Bigquery Role"
      description = "Bigquery Role"
      permissions = ["roles/bigquery.admin"]
    }