Learning center

A deploy changed the Cloud Run revision's service account

If Cloud SQL connections fail only on a new Cloud Run revision, read spec.serviceAccountName on both revisions before you read any IAM policy. When the deploy changed the identity, the old account's roles no longer apply, and the question becomes which of them the new account lacks: Cloud SQL Client, access to the password secret, or, with IAM database login, the database user itself.

Why the identity changes#

Each revision records the service account it runs as, its service identity. If a deploy names none when the resource is created, Cloud Run uses the Compute Engine default service account, PROJECT_NUMBER-compute@developer.gserviceaccount.com; Google recommends a user-managed account instead. Service identity gcloud run deploy --service-account sets the account for the new revision. gcloud run deploy

The identity moves in both directions:

  • Default to dedicated. A team creates orders-api@... to follow that advice and grants it what it thinks the service needs. Depending on organization policy, the default account may have held the Editor role on the project, and Editor is enough for the Cloud SQL Auth Proxy. The new account starts with nothing. Default service account ยท Auth Proxy permissions
  • Dedicated to default. A YAML file or Terraform template without the account field deploys a revision that runs as whatever the template implies. In an organization created after May 3, 2024, the automatic Editor grant to default accounts is disabled by default, so the default account may hold no Cloud SQL role at all. Default service account

This is different from a role removed from an unchanged account; there, the deploy is not involved. The service account lacks Cloud SQL Client

Check 1: compare the identities#

for r in PREVIOUS_REVISION NEW_REVISION; do
  gcloud run revisions describe "$r" --project PROJECT --region REGION \
    --format='value(metadata.name, spec.serviceAccountName)'
done

spec.serviceAccountName is the revision's identity. Revision spec If the values match, this guide does not apply. If they differ, find the deploy that changed it. Cloud Run writes Admin Activity audit entries for service creation and replacement, such as google.cloud.run.v1.Services.ReplaceService and google.cloud.run.v2.Services.UpdateService. Cloud Run audit logging

gcloud logging read \
  'protoPayload.serviceName="run.googleapis.com"
   AND (protoPayload.methodName="google.cloud.run.v1.Services.ReplaceService"
     OR protoPayload.methodName="google.cloud.run.v2.Services.UpdateService")
   AND protoPayload.resourceName:"SERVICE"
   AND timestamp>="START_TIME"' \
  --project PROJECT --order=asc \
  --format='value(timestamp, protoPayload.authenticationInfo.principalEmail, protoPayload.methodName)'

The caller tells you which pipeline or person deployed. The identity itself is already settled by the two describe commands.

Check 2: compare what each identity holds#

List the roles each account holds in the project that contains the instance:

for sa in OLD_SERVICE_ACCOUNT NEW_SERVICE_ACCOUNT; do
  echo "== $sa"
  gcloud projects get-iam-policy INSTANCE_PROJECT \
    --flatten='bindings[].members' \
    --filter="bindings.members:serviceAccount:$sa" \
    --format='value(bindings.role)'
done

gcloud projects get-iam-policy

Then compare what the connection needs against what each account has. The needs depend on the path and the login method.

NeedRequired whenWhat the new account lacks looks like
A role with cloudsql.instances.connect: Cloud SQL Client, Editor or Admin, or the legacy Editor roleThe built-in socket or a language connector403 notAuthorized from the Auth Proxy. Auth Proxy troubleshooting
Access to the secret that holds the passwordThe password comes from Secret ManagerThe deploy fails, because Cloud Run checks at deployment that the service account can access every secret. Access lost later stops new instances from starting when the secret is an environment variable, and fails reads of a mounted file. How secrets are checked
Cloud SQL Instance User (cloudsql.instances.login) and a matching database userIAM database loginThe login fails at stage 3, because the IAM database user is named after the account. IAM database login fails

The third row is the one a role grant does not fix. With IAM database authentication, the PostgreSQL user for a service account is its email without .gserviceaccount.com, so a new account is a new database user, with none of the old user's grants on tables. IAM database login

To confirm a single permission, Policy Troubleshooter answers for one principal and one permission, including deny policies and conditions. troubleshoot-policy iam

gcloud policy-intelligence troubleshoot-policy iam \
  //cloudresourcemanager.googleapis.com/projects/INSTANCE_PROJECT \
  --principal-email=NEW_SERVICE_ACCOUNT \
  --permission=cloudsql.instances.connect

Check 3: does a fresh instance of the previous revision connect?#

The previous revision keeps its own service account, so a fresh instance of it tests the old identity under today's policies. Test the previous revision with a tag

Previous revision, fresh instanceReading
ConnectsThe identity change explains the failure. Route traffic back; then either grant the new account what it lacks or deploy with the old account
Fails at the same stageThe old account lost access too. Both problems need fixing; the identity change is not the only cause

A worked fixture: a new account, two gaps#

The fixture is illustrative. The fictional service orders-api in europe-west1 connects to shop-prod:europe-west1:orders-db through the built-in socket and reads its password from the secret orders-db-password as an environment variable.

Revision or deployspec.serviceAccountNameRoles in shop-prod
orders-api-00017orders-runtime@shop-prod.iam.gserviceaccount.comroles/cloudsql.client; secret accessor on orders-db-password
The new deployorders-api@shop-prod.iam.gserviceaccount.comroles/logging.logWriter

First outcome. The deploy at 09:12:30 UTC comes from a pipeline that switched to a new account, and it fails. Cloud Run checks during deployment that the revision's service account can access every secret the revision uses, and orders-api cannot read orders-db-password. orders-api-00017 keeps all traffic. Nothing is down, but the release is blocked. Grant the new account access to the secret. How secrets are checked

Second outcome, after the secret grant. The team grants secret access and redeploys at 09:40. The new revision's instance now starts, passes its startup probe, which does not touch the database, and takes traffic. Database requests then fail with 403 notAuthorized. A fresh orders-api-00017 instance started through the tag prev at 09:43 connects. The identity explains it: orders-api has no cloudsql.instances.connect. Route traffic back to orders-api-00017, then grant Cloud SQL Client to orders-api in shop-prod before deploying again.

Where the checks mislead#

gcloud projects get-iam-policy lists only the project's own bindings. An account can hold a role through a folder or organization policy, or through a group, which the filter on serviceAccount: misses. Policy Troubleshooter evaluates the effective policy; use it before deciding an account lacks a permission.

An environment variable GOOGLE_APPLICATION_CREDENTIALS makes client libraries use a key file instead of the revision's identity, and Google advises never setting it on Cloud Run. If a revision sets it, the identity that calls Google APIs is not the one spec.serviceAccountName names. Service identity

A startup probe that does not touch the database lets a revision with the wrong identity pass the deploy health check and take traffic. The deploy succeeding says nothing about the database connection.

Questions#

Which service account does my Cloud Run revision use?

The one in the revision's spec.serviceAccountName. If the service was created without one, Cloud Run uses the Compute Engine default service account, PROJECT_NUMBER-compute@developer.gserviceaccount.com.

Why did Cloud SQL stop working after I gave Cloud Run its own service account?

The new account does not inherit the old one's roles. It needs Cloud SQL Client in the instance's project, access to any secret the revision reads, and, with IAM database login, its own database user and grants.

Should I roll back or grant the role?

Roll back first when the previous revision's fresh instance connects; it restores service without an IAM change. Then grant the new account what it lacks and deploy again.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading