-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
google_service_account_iam_policy not working as expected #1751
Comments
In Arguably, we should probably support both, or at least have a better error for a common case like that. |
@paddycarver thank you. I've tried it and here's what I got: Error: Error running plan: 1 error(s) occurred:
* module.service_account_for_storage.google_service_account_iam_policy.default: 1 error(s) occurred:
* module.service_account_for_storage.google_service_account_iam_policy.default: Resource 'google_service_account.default' does not have attribute 'self_link' for variable 'google_service_account.default.self_link Also, when I use service_account_id = "projects/${data.google_client_config.default.project}/serviceAccounts/${google_service_account.default.email}" I get Error: Error applying plan: 1 error(s) occurred:
* module.service_account_for_storage.google_service_account_iam_policy.default: 1 error(s) occurred:
* google_service_account_iam_policy.default: Error setting IAM policy for service account 'projects/anakatech/serviceAccounts/[email protected]': googleapi: Error 400: Role roles/storage.objectViewer is not supported for this resource., badRequest And when I use - service_account_id = "${google_service_account.default.email}" I get - Error: Error running plan: 1 error(s) occurred:
* module.service_account_for_storage.google_service_account_iam_policy.default: "service_account_id" ("[ACCOUNT_ID]@[GCP_PROJECT_ID].iam.gserviceaccount.com") doesn't match regexp "projects/(?:(?:[-a-z0-9]{1,63}\\.)*(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?):)?(?:[0-9]{1,19}|(?:[a-z0-9](?:[-a-z0-9]{0,61}[a-z0-9])?))/serviceAccounts/((?:(?:[-a-z0-9]{1,63}\\.)*(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?):)?(?:[0-9]{1,19}|(?:[a-z0-9](?:[-a-z0-9]{0,61}[a-z0-9])?))@appspot.gserviceaccount.com|[0-9]{1,20}[email protected]|[a-z](?:[-a-z0-9]{4,28}[a-z0-9])@[-a-z0-9\\.]{1,63}\\.iam\\.gserviceaccount\\.com$)" |
That's what I get for not checking the docs before replying. Sorry about that! You want |
@paddycarver thank you again. Actually I think that the docs are wrong - In google_service_account_key the
But, in the "Example Usage" section above it none of the above is used and instead the data "google_service_account" "myaccount" {
account_id = "myaccount"
}
data "google_service_account_key" "mykey" {
service_account_id = "${data.google_service_account.myaccount.name}"
public_key_type = "TYPE_X509_PEM_FILE"
}
output "mykey_public_key" {
value = "${data.google_service_account_key.mykey.public_key}"
} Moreover, in IAM policy for service account
and all the examples use - service_account_id = "your-service-account-id" IMHO when someone reads I hope that the good people at behind Terraform would fix the docs and moreover fix the kind of value used in |
Adding to the comment above I should note that I haven't been able to add resource "google_service_account" "default" {
account_id = "${var.account_id}"
display_name = "${var.display_name}"
}
data "google_iam_policy" "default" {
binding {
role = "roles/storage.objectViewer"
members = ["serviceAccount:${google_service_account.default.email}"]
}
}
resource "google_service_account_iam_policy" "default" {
service_account_id = "${google_service_account.default.name}"
policy_data = "${data.google_iam_policy.default.policy_data}"
} ... I received * module.service_account_for_storage.google_service_account_iam_policy.default: 1 error(s) occurred:
* google_service_account_iam_policy.default: Error setting IAM policy for service account 'projects/[PROJECT_ID]/serviceAccounts/[ACCOUNT_ID]@[PROJECT_ID].iam.gserviceaccount.com': googleapi: Error 400: Role roles/storage.objectViewer is not supported for this resource., badRequest
* google_container_node_pool.default: 1 error(s) occurred: On the other hand I could add resource "google_storage_bucket_iam_member" "member" {
bucket = "${google_storage_bucket.terraform_state.name}"
role = "roles/storage.objectViewer"
member = "serviceAccount:${module.service_account_for_storage.email}"
} Any insights? |
Possibly related to #2180 |
Hi @tsadoklevi, I suggest you resort to using the I suspect all the https://github.com/terraform-providers/terraform-provider-google/issues/ |
Hi @walterdolce - thank you for you comments. I used resource "google_service_account" "cloudsql-sa" {
account_id = "cloudsql-sa"
}
resource "google_project_iam_binding" "cloudsql-sa-cloudsql-admin-role" {
role = "roles/cloudsql.admin"
members = [
"serviceAccount:${google_service_account.cloudsql-sa.email}"
]
} This is equivalent to - export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
export SA_EMAIL= $(google_service_account.cloudsql-sa.email)
export ROLE="roles/cloudsql.admin"
gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:$SA_EMAIL --role $ROLE But as commented in #1225 the "side effect" of using this resource is that upon So, the right resource for adding a service account (or other types of members) is resource "google_service_account" "cloudsql-sa" {
account_id = "cloudsql-sa"
}
resource "google_project_iam_member" "cloudsql-sa-cloudsql-admin-role" {
role = "roles/cloudsql.admin"
member = "serviceAccount:${google_service_account.cloudsql-sa.email}"
} |
More information on the iam resources possible for the project is available at https://www.terraform.io/docs/providers/google/r/google_project_iam.html. We're always happy to field suggestions on how to improve this to make it clearer. In this case, the original problem (#1751 (comment)) is because the service_account_iam_policy is to determine who is allowed to use the service account, and what permissions they have on it (https://cloud.google.com/iam/docs/service-accounts#service_account_permissions), so you can't set the storage.objectViewer permission on it, because that's not a permission that makes sense in that context. You wanted to use the google_project_iam_member/google_project_iam_policy/google_project_iam_binding resource with the service account being the member, and storage.objectViewer being the role. It looks like we got to a solution, however, so I'm going to close this out. Feel free to comment if you feel there's more to do here. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks! |
Terraform Version
Terraform v0.11.7
Affected Resource(s)
google_service_account_iam_policy
Terraform Configuration Files
Debug Output
Expected Behavior
Service account IAM policy should be created
Actual Behavior
Service account IAM policy is not created
The text was updated successfully, but these errors were encountered: