Learn how to efficiently assign multiple Google Cloud IAM roles to a service account using Terraform for streamlined infrastructure management.
Managing roles and permissions for service accounts across multiple projects is a common task in Google Cloud Platform. Terraform provides a powerful way to automate this process. This article demonstrates how to use Terraform to efficiently assign multiple roles to a service account across different Google Cloud projects.
variable "project_roles" {
type = map(list(string))
default = {
"project-id-1" = ["roles/storage.objectViewer", "roles/compute.networkViewer"]
"project-id-2" = ["roles/pubsub.subscriber"]
}
}
resource "google_project_iam_member" "project_roles" {
for_each = { for project, roles in var.project_roles : project => roles }
project = each.key
role = each.value
member = "serviceAccount:your-service-account@project-id.iam.gserviceaccount.com"
}
This code snippet iterates through the project_roles
map and creates a google_project_iam_member
resource for each role in each project.
Explanation:
project_roles
as a map, where keys are project IDs and values are lists of roles.for_each
to iterate over the project_roles
map.google_project_iam_member
resource.project
argument is set to the project ID (the key of the map).role
argument is set to the current role in the iteration.member
argument specifies the service account and is constructed using the service account email format.This approach allows you to assign multiple roles to a service account across different projects in a concise and manageable way.
This Terraform code defines a variable to store project IDs and corresponding IAM roles. It then creates a Google service account and iterates through the variable to assign the specified roles to the service account for each project. This allows you to manage IAM permissions for a service account across multiple projects using a single configuration.
# Define the project roles variable
variable "project_roles" {
type = map(list(string))
default = {
"project-id-1" = ["roles/storage.objectViewer", "roles/compute.networkViewer"]
"project-id-2" = ["roles/pubsub.subscriber"]
}
}
# Replace with your actual service account email
resource "google_service_account" "default" {
account_id = "sa-iam-example"
disabled = false
display_name = "Example Service Account"
}
# Iterate through projects and roles
resource "google_project_iam_member" "project_roles" {
for_each = { for project, roles in var.project_roles : project => roles }
project = each.key
role = each.value
member = "serviceAccount:${google_service_account.default.email}"
}
Explanation:
Variable Definition:
project_roles
variable is defined as a map
where:
Service Account:
"serviceAccount:your-service-account@project-id.iam.gserviceaccount.com"
with the actual email address of your service account.Resource Iteration:
google_project_iam_member
resource uses for_each
to iterate over the project_roles
map.each.key
represents the project ID (the key of the map).each.value
represents the list of roles associated with that project.Resource Configuration:
google_project_iam_member
resource is created.project
: Set to the current project ID (each.key
).role
: Set to the current role being iterated (each.value
).member
: Set to the service account that needs the roles assigned.How to Use:
Replace Placeholders:
project_roles
variable with your desired project IDs and roles.Deploy with Terraform:
terraform apply
to create the IAM bindings.This code will efficiently grant the specified roles to your service account across the defined projects.
Code Structure and Best Practices:
project_roles
variable and another for creating the IAM bindings.google_service_account.default.email
attribute if you're creating the service account within the same Terraform code.project_iam_roles
could be a more informative name than project_roles
.Security Considerations:
Alternative Approaches:
google_project_iam_binding
: Instead of google_project_iam_member
, you can use google_project_iam_binding
to assign roles to a group of members (users, service accounts) at once. This can be useful if you have multiple service accounts that need the same set of roles.Troubleshooting:
Additional Tips:
This code snippet demonstrates how to efficiently assign multiple IAM roles to a service account across different Google Cloud projects using Terraform.
Key Components:
project_roles
Variable: A map variable defines project IDs as keys and a list of desired roles as values. This allows for a clear and organized way to manage role assignments across projects.google_project_iam_member
Resource: This resource grants IAM roles to members (in this case, a service account) at the project level.for_each
Meta-Argument: Used within the google_project_iam_member
resource, it iterates through the project_roles
map, dynamically creating a resource for each role in each project.Functionality:
project_roles
variable.google_project_iam_member
resource.Benefits:
project_roles
variable.By using this Terraform configuration, you can streamline the process of managing IAM roles for service accounts across multiple Google Cloud projects, ensuring efficient and scalable management of permissions within your cloud environment. Remember to replace placeholder values with your specific details and follow security best practices for managing service accounts and their keys. This approach simplifies infrastructure management, improves code readability, and reduces the risk of errors compared to manual configuration.