CodexBloom - Programming Q&A Platform

Terraform how to to manage IAM policy bindings due to incorrect resource target in GCP

👀 Views: 144 💬 Answers: 1 📅 Created: 2025-06-07
terraform gcp iam HCL

I'm confused about I'm working on a personal project and This might be a silly question, but I'm having trouble with Terraform when trying to manage IAM policies for my Google Cloud Platform resources. Specifically, I'm trying to bind a service account to the `roles/storage.admin` role for a specific bucket, but it seems like Terraform isn't associating the permissions correctly. Here's the relevant portion of my configuration: ```hcl resource "google_storage_bucket" "my_bucket" { name = "my-unique-bucket-name" location = "US" } resource "google_service_account" "my_service_account" { account_id = "my-service-account" display_name = "My Service Account" } resource "google_storage_bucket_iam_member" "my_bucket_iam" { bucket = google_storage_bucket.my_bucket.name role = "roles/storage.admin" member = "serviceAccount:${google_service_account.my_service_account.email}" } ``` When I run `terraform apply`, I receive the following behavior message: ``` behavior: behavior applying IAM policy for bucket my-unique-bucket-name: googleapi: behavior 400: The member 'serviceAccount:my-service-account@my-project.iam.gserviceaccount.com' is not valid, or is not allowed to be added., invalid ``` I've checked the service account and confirmed that it exists and has the correct email format. I’ve also verified that my Terraform service account has permissions to manage IAM policies. I even tried recreating the service account and the bucket, but the same behavior continues. I've looked into the [Terraform GCP provider documentation](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket_iam_member) to ensure I'm using the resources correctly. What could I be missing here? Is there an scenario with timing or dependencies, or could it be related to the way I'm referencing the bucket name? Any insights would be appreciated! My development environment is Ubuntu. Thanks in advance! I'm working on a API that needs to handle this. Any help would be greatly appreciated! I'm on Windows 10 using the latest version of Hcl. Has anyone else encountered this?