# Service accounts and IAM grants One service account per workload identity, and one `_iam_member` resource per grant. - `account_id` is 6 to 30 characters: lower-case letters, digits and hyphens. - Use the narrowest resource the role can be granted on: the bucket, topic, secret or service, in preference to the project. - A grant to a service account is `member = "serviceAccount:${...email}"`. The same value is the `member` attribute of the service account resource. ```hcl variable "project_id" { type = string description = "Project to deploy into." } variable "region" { type = string description = "Region for regional resources." } resource "google_service_account" "worker" { project = var.project_id account_id = "event-worker" display_name = "Event worker" description = "Identity of the worker service." } # A project-level role, only when no narrower resource can take the grant. resource "google_project_iam_member" "worker_metrics" { project = var.project_id role = "roles/monitoring.metricWriter" member = google_service_account.worker.member } # Who may act as the service account, for example a deployment pipeline. resource "google_service_account_iam_member" "deployer_acts_as_worker" { service_account_id = google_service_account.worker.name role = "roles/iam.serviceAccountUser" member = "serviceAccount:deployer@${var.project_id}.iam.gserviceaccount.com" } ```