Skip to content
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

Service Accounts Deleted/Recreated Lose IAM permissions #6308

Closed
BusiPlay opened this issue May 6, 2020 · 2 comments
Closed

Service Accounts Deleted/Recreated Lose IAM permissions #6308

BusiPlay opened this issue May 6, 2020 · 2 comments

Comments

@BusiPlay
Copy link

BusiPlay commented May 6, 2020

We ran into an issue when migrating a service account to a for_each construct where Terraform deleted and recreated the service account. This caused the service accounts to lose all IAM Bindings as described at https://cloud.google.com/iam/docs/understanding-service-accounts#deleting_and_recreating_service_accounts.

Example code:
Replaced:
resource "google_service_account" "service_account_a" {
account_id = "service_account_a"
display_name = "Service Account A"
}

with

variable "service_accounts" {
description="Map of Service Accounts"
default = {service_account_a = "Service Account A"}
}

resource "google_service_account" "service_account" {
for_each = var.service_accounts
account_id = each.key
display_name = each.value
}

Terraform treats this as deleting google_service_account.service_account_a and creating google_service_account.service_account["service_account_a"] and deletes the first service account, and creates a new one with the same account id.

Is there any way that the provider can recognize the account_id value in old and new state is the same, and apply an edit in place?

@venkykuberan venkykuberan self-assigned this May 6, 2020
@venkykuberan
Copy link
Contributor

Terraform does not know they are identical and treats them as 2 different resources as its unique_id is different. We can make terraform aware that they are identical and just resource is renamed by removing it from state and import with new name in 2 steps

  1. terraform rm state service_account_a
  2. terraform import google_service_account.service_account["service_account_a"] [email protected]

It should fix the issue.

@ghost
Copy link

ghost commented Jun 7, 2020

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!

@ghost ghost locked and limited conversation to collaborators Jun 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants