Learn how to streamline infrastructure management by securely creating GCP service accounts with specific roles using Terraform.
This guide explains how to create a Google Cloud Platform (GCP) service account and assign it roles using Terraform. We'll cover defining the service account, specifying roles, and using the google_project_iam_member resource to grant the roles. The example code includes explanations and important considerations for managing service accounts and their permissions effectively.
To create a GCP service account with roles using Terraform, you'll generally follow these steps:
Define the service account:
resource "google_service_account" "default" {
account_id = "your-service-account-id"
display_name = "Your Service Account Name"
}"your-service-account-id" with a unique identifier for your service account."Your Service Account Name" with a descriptive name.Define the roles: You can either list roles directly or use variables for better organization:
variable "roles" {
type = list(string)
default = [
"roles/storage.objectViewer",
"roles/pubsub.publisher",
]
}Assign roles using google_project_iam_member:
resource "google_project_iam_member" "service_account_roles" {
for_each = toset(var.roles)
role = each.value
member = "serviceAccount:${google_service_account.default.email}"
}roles list and grants each role to the service account.Explanation:
google_service_account: This resource creates the service account itself.google_project_iam_member: This resource binds roles to members (in this case, your service account) at the project level.for_each: This Terraform construct allows you to create multiple instances of a resource based on a collection. Here, it iterates through the roles list.toset: This function converts the roles list into a set, which is required for for_each.each.value: This refers to the current element (role) being processed within the loop."serviceAccount:${google_service_account.default.email}": This dynamically constructs the member string using the service account's email.Important Considerations:
iam.serviceAccounts.create, iam.roles.list) to create service accounts and assign roles.google_organization_iam_member) accordingly.count or modules to manage roles for multiple service accounts.This setup ensures that roles are added specifically to your new service account without affecting existing role assignments.
This Terraform code defines variables for a Google Cloud service account, including its ID, display name, and roles. It then creates the service account and assigns the specified roles to it. Finally, it outputs the service account's email address.
# Define variables for service account and roles
variable "service_account_id" {
type = string
description = "Unique ID for the service account"
default = "your-service-account-id"
}
variable "service_account_display_name" {
type = string
description = "Display name for the service account"
default = "Your Service Account Name"
}
variable "roles" {
type = list(string)
description = "List of roles to assign to the service account"
default = [
"roles/storage.objectViewer",
"roles/pubsub.publisher",
]
}
# Create the service account
resource "google_service_account" "default" {
account_id = var.service_account_id
display_name = var.service_account_display_name
}
# Assign roles to the service account
resource "google_project_iam_member" "service_account_roles" {
for_each = toset(var.roles)
role = each.value
member = "serviceAccount:${google_service_account.default.email}"
}
# Output the service account email
output "service_account_email" {
value = google_service_account.default.email
}Explanation:
Variables:
service_account_id: Stores the unique ID for your service account.service_account_display_name: Stores the display name for your service account.roles: Defines a list of roles to be assigned.Service Account Creation:
google_service_account resource creates the service account using the provided ID and display name.Role Assignment:
google_project_iam_member resource iterates through the roles list using for_each and toset.google_service_account.default.email.Output:
output block displays the service account's email address, which can be useful for later reference.To use this code:
.tf file (e.g., service_account.tf).terraform init (initializes Terraform)terraform plan (preview the changes)terraform apply (create the resources)This code will create a GCP service account with the specified roles, providing a clear and organized way to manage service account permissions in your Terraform infrastructure.
Security:
Best Practices:
Troubleshooting:
Alternatives:
This table summarizes the key aspects of creating GCP service accounts and assigning them roles using Terraform:
| Feature | Description | Terraform Resource | Example |
|---|---|---|---|
| Service Account Creation | Defines the service account with a unique ID and display name. | google_service_account |
resource "google_service_account" "default" { account_id = "your-service-account-id" display_name = "Your Service Account Name" } |
| Role Definition | Specifies the roles to be assigned, either directly or using variables for better organization. | variable "roles" { type = list(string) default = [ "roles/storage.objectViewer", "roles/pubsub.publisher" ] } |
|
| Role Assignment | Binds the defined roles to the service account at the project level. | google_project_iam_member |
resource "google_project_iam_member" "service_account_roles" { for_each = toset(var.roles) role = each.value member = "serviceAccount:${google_service_account.default.email}" } |
| Dynamic Member String | Constructs the member string dynamically using the service account's email. | "serviceAccount:${google_service_account.default.email}" |
|
| Iteration through Roles | Uses for_each to iterate through the list of roles and assign each one to the service account. |
for_each = toset(var.roles) |
Key Considerations:
count or modules for managing roles across multiple service accounts.By combining the google_service_account and google_project_iam_member resources with Terraform's built-in functionalities like for_each, you can efficiently manage service accounts and their roles in your GCP projects. This approach enables infrastructure as code, ensuring that your service accounts have the correct permissions for your applications and workflows. Remember to follow security best practices, such as the principle of least privilege, and consult the Terraform Google provider documentation for the latest features and updates. By automating this process, you can minimize errors, improve consistency, and streamline your cloud infrastructure management.
Service Account in GCP with Terraform — tech.serhatteker.com | How to create Service Account with RSA key pairs in Google Cloud Platform (GCP) with Terraform
Having difficulty using two different for loops in the same resource ... | Hi there, Terraform v0.12.18 provider.google v3.4.0 provider.google-beta v3.4.0 I am trying to create several service accounts and map them to multiple roles in GCP. I’m having an issue getting my second loop to be honored (if that’s possible). variable "roles_for_admins" { default = { "iam" = "roles/resourcemanager.projectIamAdmin" "kubernetes" = "roles/container.admin" "storage" = "roles/storage.admin" "datastore" = "roles/datastore.owner" "googleappengine" = "role...
A step by step guide to set up a Terraform to work with a GCP project ... | In this guide our data engineer Edgar Ochoa will help you to set up a Terraform to work with a GCP Project using Cloud Storage as a backend.