Secrets & environment variables
Cloud Run passes configuration to your container in two ways: environment variables for non-sensitive settings, and secrets (backed by Secret Manager) for sensitive values like API keys and passwords.
Environment variables
Environment variables are key-value pairs injected into your application container and read by your code at runtime to control functionality. They are set when you create or update a service or job, or deploy a new revision - through the Google Cloud console, the gcloud CLI, a YAML file, or Terraform.
You read them with the standard library for your language:
| Language | Access |
|---|---|
| Python | os.environ.get("key") |
| Node.js | process.env.key |
| Java | System.getenv("key") |
You can set default environment variables in the image with the ENV statement in a Dockerfile. A variable set with the same name on the Cloud Run service or job overrides the Dockerfile default.
Some environment variables are reserved and cannot be set - they are listed in the container runtime contract.
Secrets
When your service needs sensitive configuration - API keys, passwords - store it in a secret in Secret Manager, the Google Cloud service for storing, managing, and accessing secrets.
- A secret is an object holding a collection of metadata (replication locations, labels, permissions) plus one or more secret versions.
- A secret version stores the actual secret data - an API key or password - as a text string or binary blob.
- Secret Manager is the service that lets you store, manage, and access secrets.
Accessing a secret
Make the secret available to the service, then deploy or update with the specified secret. There are two ways to expose it:
- Volume: the secret appears to the container as a file. Reading the volume always fetches the value from Secret Manager, so it works well with the
latestversion. - Environment variable: resolved once at instance startup, so pin the secret to a specific version rather than
latest.
Any configuration change, including updating secrets, creates a new service revision. Subsequent revisions automatically inherit the setting. You can do this in the console, with the gcloud CLI, or a YAML file.
Allowing access to secrets
A Cloud Run service runs as a service account - that is its identity. To let it read a secret, grant that service account the Secret Manager Secret Accessor role on the secret. The grant is a policy binding in the secret's IAM policy.
Recap
- Environment variables are key-value pairs injected into the container and read by your code; set them on the service/job or a revision.
- A Dockerfile
ENVdefault is overridden by a same-named variable set on the Cloud Run service or job. - Use secrets (Secret Manager) to store and access sensitive information.
- Expose a secret by mounting it as a volume (fetches latest on read) or passing it as an environment variable (pin to a version).
- Grant the service account the Secret Manager Secret Accessor role to authorize access.