Learning center

Cloud Run and a Cloud SQL instance in another project

With the Cloud SQL instance in another project, Cloud Run needs the role and the API in the right projects and the full connection name on the revision. Cloud SQL Client goes to the service account in the instance's project, the Cloud SQL Admin API is enabled in both projects, and the revision names the instance as PROJECT:REGION:INSTANCE. Read IAM in the instance's project; reading the service's project is the usual mistake.

What each project holds#

A cross-project connection has two projects:

ProjectHoldsNeeds
The service's projectThe Cloud Run service, its revisions and usually the service accountThe Cloud SQL Admin API enabled
The instance's projectThe Cloud SQL instanceThe Cloud SQL Admin API enabled; Cloud SQL Client on the service account

Cloud SQL's Cloud Run guide states both requirements: when the authorizing service account belongs to a different project than the instance, enable the Cloud SQL Admin API in both projects and add the IAM permissions for the service account in the project that contains the instance. Connect from Cloud Run The role belongs there because a service account authorizes access to all Cloud SQL in a specific project, and the project is the lowest level where Cloud SQL Client can be granted. Roles with serverless options · Cloud SQL IAM roles

The failure from a missing piece sits at stage 2 of the four stages: the Auth Proxy or connector is refused by the Admin API before PostgreSQL sees a login.

Check 1: the connection name is complete#

gcloud run revisions describe REVISION --project SERVICE_PROJECT --region REGION \
  --format='yaml(metadata.annotations, spec.serviceAccountName)'

gcloud sql instances describe INSTANCE --project INSTANCE_PROJECT \
  --format='value(connectionName)'

gcloud run deploy accepts a bare instance name only when the instance is in the same project and region as the service; any other instance needs <project>:<region>:<instance>. gcloud run deploy The console asks for a custom connection string in the form PROJECT-ID:REGION:INSTANCE-ID for an instance in another project. Console steps

run.googleapis.com/cloudsql-instances on the revisionReading
Matches connectionName exactlyThe name is right. Go to check 2
Names the service's project instead of the instance'sA bare name or a template default resolved to the service's project. The socket mounted is not the one the code opens, or the proxy is asked for an instance that does not exist there
Names the right project, another region or instanceA copy error. Fix the name

The code's socket path must carry the same full name, since the socket appears at /cloudsql/INSTANCE_CONNECTION_NAME. The /cloudsql socket is missing

Check 2: the role is in the instance's project#

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

Policy Troubleshooter evaluates the effective allow and deny policies on the resource you name, so name the instance's project. troubleshoot-policy iam

Result on the instance's projectResult on the service's projectReading
GrantedAnyIAM is in order for the connection. Go to check 3
Not grantedGrantedThe role was granted in the wrong project. It gives access to instances in the service's project only
Not grantedNot grantedNever granted, or removed. Read the instance project's audit log. The service account lacks Cloud SQL Client

A deny policy attached to the instance's project, folder or organization can block the service account even with the grant in place; Policy Troubleshooter includes the deny policies you can read.

Check 3: the Admin API in both projects#

for p in SERVICE_PROJECT INSTANCE_PROJECT; do
  echo "== $p"
  gcloud services list --enabled --project "$p" | grep sqladmin.googleapis.com
done

gcloud services list A project with no output lacks the API. The Auth Proxy logs Error 403: Access Not Configured when the API is not enabled. Auth Proxy troubleshooting · The Cloud SQL Admin API is disabled

Private IP across projects#

A direct private IP connection does not use the proxy, so the role and the Admin API do not apply to the path. The network does: the revision's egress must reach the VPC network the instance uses. Direct VPC egress and Serverless VPC Access support VPC networks connected through Cloud VPN and VPC Network Peering, and a connector must share the project and region of the resource that uses it unless you use Shared VPC. Private IP from Cloud Run Private services access accepts connections from one VPC network; Private Service Connect accepts them from several networks, projects or organizations. Private IP options For timeouts on that path, see private IP connections from Cloud Run time out.

A worked fixture: a grant in the wrong project#

The fixture is illustrative. The fictional service orders-api runs in project shop-prod as orders-runtime@shop-prod.iam.gserviceaccount.com. Its database moves to a new instance in the project shop-data, connection name shop-data:europe-west1:orders-db. orders-api-00018, deployed at 09:12:30 UTC, is the first revision that names it; orders-api-00017 still names shop-prod:europe-west1:orders-db.

Outcome A: the grant is in shop-prod.

Checkshop-prodshop-data
sqladmin.googleapis.com enabledYesYes
Troubleshooter, cloudsql.instances.connectGrantedNot granted
Annotation on orders-api-00018shop-data:europe-west1:orders-db

From 09:12:41, orders-api-00018 logs 403 notAuthorized. The migration ticket granted Cloud SQL Client in shop-prod, where it already existed. Routing traffic back to orders-api-00017 restores service on the old instance, as long as it still accepts writes. Grant roles/cloudsql.client to orders-runtime in shop-data; this is an IAM change in that project, made by whoever owns its policy.

Outcome B: a bare name. The grant and both APIs are in place. The deploy used --set-cloudsql-instances orders-db, and a bare name means an instance in the service's own project and region, so the revision mounts the socket for shop-prod:europe-west1:orders-db, the old instance. The code, updated for the move, opens /cloudsql/shop-data:europe-west1:orders-db/.s.PGSQL.5432, and from 09:12:41 the driver reports that the socket file does not exist. The name was the problem, not the access. Deploy with --set-cloudsql-instances shop-data:europe-west1:orders-db.

Where the checks mislead#

gcloud projects get-iam-policy SERVICE_PROJECT shows the role, and it looks correct. Cross-project problems hide there because the role is real; it just applies to the wrong instances.

An instance whose connectionName you cannot read, because you lack access to the instance's project, cannot be checked from the service's side. Ask the owner of that project for the name, the API state and the Troubleshooter result.

A connection that worked before a project move may have used the old instance through a bare name. A successful connection proves which instance answered only when you check the name it used.

Questions#

Where do I grant Cloud SQL Client when the instance is in another project?

In the project that contains the Cloud SQL instance, to the service account the Cloud Run revision runs as. A grant in the Cloud Run service's project covers only instances in that project.

Do both projects need the Cloud SQL Admin API?

Yes, when the service account and the instance are in different projects, Google's Cloud Run guide asks for the Cloud SQL Admin API in both.

How do I connect Cloud Run to a Cloud SQL instance in another project?

Give the revision the full connection name, PROJECT:REGION:INSTANCE, with --add-cloudsql-instances or --set-cloudsql-instances, grant Cloud SQL Client in the instance's project, and enable the Admin API in both projects.

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