Search code examples
google-cloud-platformterraform

Terraform - GCP - Import project IAM member


I am using this code to set permissions for projects to service accounts:

resource "google_project_iam_member" "name_of_sa_project_prod" {
    for_each = toset([
        "roles/bigquery.dataOwner",
        "roles/bigquery.jobUser"
    ])
    role = each.key
    project = google_project.test_prod.project_id
    member = "serviceAccount:${google_service_account.sa_account.email}"
}

But since I changed laptops, I need to import all the states back before running terraform apply. I have tried the following:

terraform import module.module_name.google_project_iam_member.name_bigquery_dev.owner["user:name@project_name.iam.gserviceaccount.comr"] "project_name roles/owner user:[email protected]"

But I get the following errors:

Index brackets must contain either a literal number or a literal string.

This character is not used within the language.

I also tried this:

terraform import module.module_name.google_project_iam_member.name_bigquery_dev["roles/bigquery.dataOwner"] "project_name roles/owner user:sa_name@project_name.iam.gserviceaccount.com"

But this one gives me the following error:

│ Index brackets must contain either a literal number or a literal string.

The documentation only gives this example:

terraform import google_project_iam_binding.default "{{project_id}} roles/viewer"

But I'm not sure how to apply it in my case of "for_each" situation?


Solution

  • When executing the import command in a shell the full resource namespace must be cast as a literal string for the shell to interpret it correctly:

    terraform import 'module.module_name.google_project_iam_member.name_bigquery_dev["roles/bigquery.dataOwner"]' "project_name roles/owner user:sa_name@project_name.iam.gserviceaccount.com"