The current way I am able to impersonate service accounts via terraform is by using lengthy declarations like these with multiple provider blocks. Not to mention I have to copy/paste this every time for each user/s.a/project...
provider "google" {
alias = "network_admin_impersonation"
scopes = var.impersonation_info.of_network_admin.tier_1_scopes
}
data "google_service_account_access_token" "network-admin" {
provider = google.network_admin_impersonation
target_service_account = google_service_account.network-admin.email
scopes = var.impersonation_info.of_network_admin.tier_2_scopes
lifetime = "1200s"
}
provider "google" {
alias = "as_network_admin"
access_token = data.google_service_account_access_token.network-admin.access_token
region = var.region
zone = var.zone
}
And to grant users the right to use this service account :
resource "google_service_account_iam_member" "network-admin-impersonators" {
for_each = toset([
for account in var.user_accs_impersonators_info.as_network_admin :
"${account.acc_type}:${account.acc_details.email}"
])
service_account_id = google_service_account.network-admin.name
role = "roles/iam.serviceAccountTokenCreator"
member = each.value
}
There must be a better way to do this that I am not seeing. Maybe via a module? But I read somewhere that it is generally a bad idea to use provider within modules... I would appreciate some guidance on this.