Nov 18, 20265 min read

Google Cloud Service Accounts: One Per Service With Least Privilege

Stop running Cloud Run and GKE workloads as the default compute service account: create one service account per service, grant resource-level roles, attach it at deploy time, and audit what is left.

Illustration for Google Cloud Service Accounts: One Per Service With Least Privilege

Every Cloud Run service and GKE workload runs as a service account, and if you never chose one, it is the Compute Engine default service account. In many older projects that account holds the Editor role on the whole project, which means a template rendering bug in your marketing API can delete your production database.

The lasting fix is one service account per service, with roles bound on the specific resources that service uses, and it gets harder the longer you wait. This guide walks through the setup for Cloud Run, the bindings for common resources, and how to find what is still over-granted.

Default vs per-service accounts at a glance

Default compute accountOne account per service
ScopeShared by every workload in the projectOne workload
Typical rolesEditor on older projectsA few resource-level roles
Blast radiusThe whole projectThat service's resources
Audit logsEvery action looks the sameEach action names the service
Setup effortNoneOne account and a few bindings per service

Google's service account best practices recommend single-purpose service accounts and warn against relying on the default ones. New organizations created after May 2024 enforce the iam.automaticIamGrantsForDefaultServiceAccounts constraint, which stops the Editor grant, but projects created earlier keep it until someone removes it.

Create the account and attach it

Name accounts after the service so audit logs read naturally.

~
$ gcloud iam service-accounts create orders-api \ --project=my-project --display-name="orders API runtime"$ gcloud run deploy orders-api --image=$IMAGE --region=us-central1 \ --service-account=orders-api@my-project.iam.gserviceaccount.com

On GKE, the equivalent is a Kubernetes service account bound to the Google service account through Workload Identity Federation for GKE, so pods get the Google identity without a key file.

Whoever deploys needs roles/iam.serviceAccountUser on that specific account, the actAs permission. Grant it on the account, not the project, or any deployer can run code as any service account in the project.

Bind roles on resources, not the project

Most Google Cloud resources accept IAM bindings directly. Use those instead of project-level grants:

ResourceRoleCommand
Pub/Sub topicroles/pubsub.publishergcloud pubsub topics add-iam-policy-binding
Pub/Sub subscriptionroles/pubsub.subscribergcloud pubsub subscriptions add-iam-policy-binding
Cloud Storage bucketroles/storage.objectUser or objectViewergcloud storage buckets add-iam-policy-binding
Secret Manager secretroles/secretmanager.secretAccessorgcloud secrets add-iam-policy-binding
Cloud SQLroles/cloudsql.client (project level) plus a database usergcloud projects add-iam-policy-binding
~
$ SA=serviceAccount:orders-api@my-project.iam.gserviceaccount.com$ gcloud pubsub topics add-iam-policy-binding order-created \ --member=$SA --role=roles/pubsub.publisher$ gcloud storage buckets add-iam-policy-binding gs://my-project-invoices \ --member=$SA --role=roles/storage.objectUser$ gcloud secrets add-iam-policy-binding stripe-key \ --member=$SA --role=roles/secretmanager.secretAccessor

Cloud SQL is the exception: cloudsql.client only allows connecting through the connector or Auth Proxy, and what the service can do inside Postgres depends on the database user. With Cloud SQL IAM authentication, the service account itself becomes a database user you can grant per-table privileges.

Find what is still over-granted

IAM Recommender compares granted permissions with the permissions each principal used over the last 90 days and suggests smaller roles. Start with the default compute account:

~
$ gcloud projects get-iam-policy my-project \ --flatten="bindings[].members" \ --filter="bindings.members:serviceAccount:123456789-compute@developer.gserviceaccount.com" \ --format="table(bindings.role)"$ gcloud recommender recommendations list --project=my-project \ --location=global --recommender=google.iam.policy.Recommender

Once no workload uses the default account, remove its Editor binding, then consider disabling it. Do this in a staging project first, since forgotten workloads fail loudly when their roles disappear.

Best fit: every project with more than one service. Create the account when you create the service, and the bindings stay small.

How does Encore assign service accounts?

Encore

Encore brings infrastructure from code, which gives it the information this guide asks you to track by hand: which service uses which bucket, topic, database and secret. The GCP infrastructure docs describe a dedicated service account per Cloud Run deployment, or per service on GKE, with permissions derived from the resources the code uses, and updated as the code changes.

package orders import ( "context" "encore.dev/pubsub" "encore.dev/storage/objects" ) type OrderCreated struct { OrderID string `json:"order_id"` } // Publishing here is what gives this service publish access to the topic. var OrderEvents = pubsub.NewTopic[*OrderCreated]("order-created", pubsub.TopicConfig{ DeliveryGuarantee: pubsub.AtLeastOnce, }) // Using this bucket from the service is what grants storage access to it. var Invoices = objects.NewBucket("invoices", objects.BucketConfig{}) var secrets struct { StripeKey string // the service can read this secret and no others } func publishCreated(ctx context.Context, id string) error { _, err := OrderEvents.Publish(ctx, &OrderCreated{OrderID: id}) return err }

Static analysis of topics, buckets and secrets is what drives the bindings. When code needs a reference it can pass around, Encore asks you to declare the permission up front with a typed reference such as pubsub.TopicRef[pubsub.Publisher[*OrderCreated]], so the granted access stays visible in the code. Encore provisions a service account for the service, and you never pick roles or write bindings.

A coding agent that adds a bucket write to a service changes the permissions in the same diff, visibly, and cannot grant itself access to a resource the code does not use.

Best fit: teams on GCP who want least-privilege identities per service without maintaining the binding list by hand as services change.

How to choose

If you deploy to GCP yourself, create one service account per service now, attach it at deploy time, bind roles on resources, grant actAs per account, and use IAM Recommender to trim the default account until you can remove its Editor role. If you deploy with Encore, the per-service accounts and bindings follow from the code, and your review is of the code that uses each resource.

Frequently asked questions

Why should each Cloud Run service have its own service account?

Every service that shares a service account can do everything that account can do. With one account per service, a bug or compromised dependency in one service can only reach that service's own database, topics and secrets, and audit logs show which service performed each action.

What is wrong with the default compute service account?

Cloud Run and GKE use the Compute Engine default service account unless you choose another. In projects created before Google started enforcing the automatic-grants organization policy in 2024, that account often holds the project-wide Editor role, which gives every workload broad write access to the project.

How do I grant a service account access to one Pub/Sub topic or bucket?

Bind the role on the resource rather than the project, for example gcloud pubsub topics add-iam-policy-binding orders --member=serviceAccount:orders-api@PROJECT.iam.gserviceaccount.com --role=roles/pubsub.publisher. Resource-level bindings keep the account from reaching other topics or buckets in the same project.

Does Encore create a service account for each service on GCP?

Yes. Encore's GCP documentation describes a dedicated service account for each deployment, with permissions derived from the resources your code uses, such as the buckets, topics, databases and secrets it accesses. The bindings change as your code changes.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders