I have a list of Service Accounts that will be created, and to assign roles to them I'm using a module that Google provides, and my code is as follows:
module "service-accounts" {
source = "terraform-google-modules/service-accounts/google"
version = "4.1.1"
project_id = var.project
names = var.sa_list
project_roles = [
"${var.project}=>roles/appengine.appAdmin",
"${var.project}=>roles/artifactregistry.reader",
"${var.project}=>roles/cloudbuild.builds.builder",
"${var.project}=>roles/cloudsql.client",
"${var.project}=>roles/cloudsql.instanceUser"
]
display_name = "Google App Engine SA - Managed by Terraform"
}
This works just fine. But I don't want to make the roles in project_roles explicit, so I've tried to use the for_each meta-argument"
variables.tf:
variable "rolesList" {
type =list(string)
default = ["roles/appengine.appAdmin","roles/artifactregistry.reader", "roles/cloudbuild.builds.builder", "roles/cloudsql.client", "roles/cloudsql.instanceUser"]
}
main.tf:
module "service-accounts" {
source = "terraform-google-modules/service-accounts/google"
version = "4.1.1"
project_id = var.project
names = var.sa_list
for_each = (toset[var.rolesList])
project_roles = ["${var.project}=>${each.key}"]
display_name = "Google App Engine SA - Managed by Terraform"
}
This way won't work because Terraform will create multiple accounts with the same id. How can I remove the hard code from project_roles? Is there a way to store the roles elsewhere and then call them? Or I'll need to assign roles individually?