Learning center

A first-generation Cloud Run revision and the instance's server CA mode

If a first-generation Cloud Run revision cannot connect through the built-in Cloud SQL connection, check the instance's server CA mode. First generation embeds Cloud SQL Auth Proxy v1, which connects only to instances that use the per-instance CA. Then find which side changed: the revision's execution environment or the instance's serverCaMode.

The rule and where it applies#

Cloud SQL lets an instance's server certificate come from one of three CA hierarchies, set in serverCaMode: GOOGLE_MANAGED_INTERNAL_CA (per-instance CA), GOOGLE_MANAGED_CAS_CA (shared CA) or CUSTOMER_MANAGED_CAS_CA (customer-managed CA). An unspecified value behaves like GOOGLE_MANAGED_INTERNAL_CA. Instance resource The per-instance CA is the default. Manage server CA certificates

Cloud Run's first-generation execution environment embeds Cloud SQL Auth Proxy v1 and can connect only to instances with the per-instance CA. An instance with the shared or the customer-managed CA needs the second-generation environment, because both modes require Auth Proxy v2. Connect from Cloud Run The standalone Auth Proxy states the same requirement as versions: 2.13.0 or later for a shared CA, 2.14.3 or later for a customer-managed CA. Auth Proxy requirements

The rule concerns the proxy that Cloud Run embeds behind the /cloudsql/ socket. A language connector inside the container is the application's own dependency, and its version is what matters there. Confirm the path first: which path the revision uses.

The failure sits at stage 2 of the four stages: the socket exists, and the attempt fails before PostgreSQL sees a login. A revision whose service account lacks the Cloud SQL Client role fails at the same stage, so rule that out as well. The revision's service account lacks Cloud SQL Client

Check 1: read both settings#

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

gcloud sql instances describe INSTANCE --project INSTANCE_PROJECT \
  --format='value(settings.ipConfiguration.serverCaMode)'

The revision's execution environment is in the run.googleapis.com/execution-environment annotation, gen1 or gen2. Revision annotations · --execution-environment

A missing annotation does not mean second generation. Cloud Run services have no execution environment specified by default, and Cloud Run then selects first or second generation based on the features used. Execution environments

Revision environmentInstance serverCaModeReading
gen1GOOGLE_MANAGED_INTERNAL_CA or emptyCompatible. Look elsewhere at stage 2
gen1GOOGLE_MANAGED_CAS_CA or CUSTOMER_MANAGED_CAS_CAIncompatible. The built-in connection cannot reach this instance
Not setShared or customer-managed CACloud Run may choose either. Set gen2 explicitly
gen2AnyCompatible. Look elsewhere at stage 2

Check 2: which side changed?#

An incompatible pair that used to work means one side moved. Each side keeps a record.

The revision. Compare the annotation across revisions, as above. A new gen1 value comes from --execution-environment gen1, from a YAML file that sets the annotation, or from Terraform's execution_environment; every new revision copies the service's setting unless the deploy changes it. Configure execution environments

The instance. The server CA mode is set when an instance is created or edited. Shared CA An edit is an UPDATE in the instance's operations, and an Admin Activity audit entry with the method cloudsql.instances.update. Operation types · Cloud SQL audit logging

gcloud sql operations list --instance INSTANCE \
  --project INSTANCE_PROJECT --limit 20

gcloud logging read \
  'protoPayload.serviceName="cloudsql.googleapis.com"
   AND protoPayload.methodName="cloudsql.instances.update"
   AND timestamp>="START_TIME"' \
  --project INSTANCE_PROJECT --order=asc \
  --format='value(timestamp, protoPayload.authenticationInfo.principalEmail, protoPayload.resourceName)'

gcloud sql operations list

What changedFresh instance of the previous revisionReading
The new revision is gen1; the instance has not been updatedConnects (if it is gen2)The deploy caused it. Route traffic back, then remove gen1 from the deploy
The instance moved to a shared or customer-managed CA; the revisions did not changeFails too, if it is gen1 or unset and running in first generationNot the deploy. Every first-generation revision lost the connection when the instance changed

Fixing it#

Each fix is a change; know what it touches.

Deploying with --execution-environment gen2 creates a new revision in the second-generation environment. Second generation requires at least 512 MiB of memory and can have longer cold starts than first generation for some services. Execution environments

Moving the instance back to the per-instance CA is an instance edit, and it changes which CA signs the server certificate for every client of the instance. Check the other clients before you choose it.

A worked fixture: the revision or the instance#

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.

Outcome A: the deploy changed the environment. orders-db has used GOOGLE_MANAGED_CAS_CA for four months. orders-api-00017 carries run.googleapis.com/execution-environment: gen2. orders-api-00018, deployed at 09:12:30 UTC with --execution-environment gen1 --memory 256Mi to cut cold starts, fails every database request from 09:12:41. A fresh orders-api-00017 instance started through a tag at 09:15:02 connects. The instance's operations list has no UPDATE in the past month. The deploy caused it. Route traffic back to orders-api-00017.

Outcome B: the instance changed. Neither revision carries an execution-environment annotation, and orders-api-00017 has connected for weeks. At 08:40 UTC an UPDATE operation on orders-db completes; the audit entry for cloudsql.instances.update shows a database administrator, and serverCaMode now reads GOOGLE_MANAGED_CAS_CA. orders-api-00018 is deployed at 09:12:30, and its new instances fail; a fresh orders-api-00017 instance at 09:15:02 fails the same way. The deploy only started new instances. Set gen2 on the service, which is a new deploy; reverting the CA mode is the other option, if nothing else depends on the shared CA.

Where the checks mislead#

When neither revision sets an environment, the annotation cannot tell you which environment an instance actually ran in, because Cloud Run chose it. Instances started before the CA change may hold open connections for a while, which can hide the change until new instances start; set the environment explicitly so the record answers the question next time.

For a language connector, whether it supports these CA modes depends on the connector and its version, not on the execution environment. A connector-based service that fails after a CA change needs its dependency versions read against the connector's own documentation.

Questions#

Can a first-generation Cloud Run service connect to a Cloud SQL instance with a shared CA?

Not through the built-in Cloud SQL connection. First generation embeds Auth Proxy v1, which supports only the per-instance CA; shared and customer-managed CA modes require the second-generation environment.

How do I find a Cloud SQL instance's server CA mode?

Run gcloud sql instances describe INSTANCE --format='value(settings.ipConfiguration.serverCaMode)'. An empty or unspecified value behaves like GOOGLE_MANAGED_INTERNAL_CA, the per-instance CA.

Which execution environment does my Cloud Run service use?

If the revision's run.googleapis.com/execution-environment annotation is gen1 or gen2, that one. If it is absent, Cloud Run selects first or second generation based on the features the service uses.

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