Configure secrets for services
Stay organized with collections
Save and categorize content based on your preferences.
Your service might require API keys, passwords, certificates, or
other sensitive information for its dependencies. For Cloud Run,
Google recommends storing this sensitive information in a secret you create in
Secret Manager.
Make a secret available to your containers in one of the following ways:
When you mount each secret as a volume, Cloud Run makes the secret
available to the container as files. When reading a volume, Cloud Run
always fetches the secret value from the Secret Manager to use
the value with the latest version. This method also works well with secret
rotation.
Pass a secret using environment variables.
Environment variables are resolved at instance startup time, so if you use
this method, Google recommends that you pin the secret to a particular version
instead of using latest as the version.
For more information, see Secret Manager
best practices.
How secrets are checked at deployment and runtime
During service deployment, Cloud Run checks all the secrets
you use. The check ensures that the service account that runs the container has
permission to access these secrets.
During runtime, when instances start up:
If the secret is an environment variable, Cloud Run retrieves the
value of the secret prior to starting the instance. If the secret retrieval
process fails, the instance doesn't start.
If you mount the secret as a volume, Cloud Run doesn't perform any
checks during instance startup. However, during runtime, if a secret is
inaccessible, attempts to read the mounted volume fail.
Volume ownership
The ownership of a Cloud Run secret volume differs by the execution
environment and deployment type.
When you mount a secret volume, the identity owning the files and directories
differs depending on the workload's execution environment and on whether the
deployment consists of one or multiple containers.
In the first generation execution environment where you are deploying a single
container, the identity you use for the container owns the secret volume.
In all other cases, root owns the volume. This includes:
First generation execution environment where you are deploying multiple
containers
The second generation environment
Before you begin
Enable the Secret Manager API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM
role (roles/serviceusage.serviceUsageAdmin), which
contains the serviceusage.services.enable permission. Learn how to grant
roles.
Any configuration change leads to the
creation of a new revision. Subsequent revisions will also automatically get
this configuration setting unless you make explicit updates to change it.
You can make a secret accessible to your service
using the Google Cloud console, the Google Cloud CLI, or a YAML file when you
deploy a new service or update an existing
service, and deploy a revision.
Click the tab of your choice:
Console
In the Google Cloud console, go to the Cloud Run Services page:
Click Deploy container to configure a
new service. Fill out the initial service
settings page, then click Containers, Networking, Security to expand the
service configuration page.
If you are configuring an existing service, click the
service, and click Edit and deploy new revision.
Follow the steps to mount the secret as a volume, or expose the secret as
an environment variable.
To expose the secret as an environment variable:
Click the Container(s) tab.
In the Variables and Secrets tab, click Reference a secret.
In the Name 1 field, enter the name of the environment variable.
From the Secret list, select the secret you want to use.
From the Version 1 list, select the version of the secret to reference.
Click Done.
Click Create or Deploy.
To mount the secret as a volume:
Click the Volumes tab.
Click Mount volume.
Click Secret.
In the Mount path field, enter the mount path for this secret.
This is the directory where all versions of your secret are placed.
From the Secret list, select the secret you want to use.
In the Path 1 field, enter the name of the file to mount.
In the Version 1 list, select the version of the secret to
reference. By default, the latest version is selected. You can select
a specific version if you want.
Click Save.
Click Create or Deploy.
gcloud
To make a secret accessible to your service, enter one of the following
commands.
To mount the secret as a volume when deploying a service:
IMAGE_URL: a reference to the container image, for
example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,
the repositoryREPO_NAME must
already be created. The URL follows the format of LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
PATH: the mount path of the volume and
filename of the secret. It must start with a leading slash—for example:
/etc/secrets/dbconfig/password, where /etc/secrets/dbconfig/ is the
mount path of the volume, and password is the filename of the secret.
SECRET_NAME: the secret name in the same
project—for example, mysecret.
VERSION: the secret version. Use latest
for latest version, or a number—for example, 2.
To expose the secret as an environment variable when deploying a
service:
IMAGE_URL: a reference to the container image, for
example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,
the repositoryREPO_NAME must
already be created. The URL follows the format of LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
.
ENV_VAR_NAME: the name of the environment
variable you want to use with the secret.
SECRET_NAME: the secret name in the same
project—for example, mysecret.
VERSION: the secret version. Use latest
for latest version, or a number—for example, 2.
You can update multiple secrets at the same time. To do this, separate the
configuration options for each secret with a comma. The following command
updates one secret mounted as a volume and another secret exposed as an
environment variable.
To update existing secrets, enter the following command:
For secrets exposed as environment variables, under env, update the
ENV_VAR, VERSION,
and/or SECRET_NAME as desired. If you have multiple secrets
mounted as environment variables, you will have multiples of these
attributes.
For secrets mounted as file paths, update the
MOUNT_PATH, VOLUME_NAME, VERSION,
FILENAME, and/or SECRET_NAME as desired. If you
have multiple secrets mounted as file paths, you will have multiples of these
attributes.
IMAGE_URL: a reference to the container image, for
example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,
the repositoryREPO_NAME must
already be created. The URL follows the format of LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
.
REVISION with a new revision name or delete it (if present). If you supply a new revision name, it must meet the following criteria:
Starts with SERVICE-
Contains only lowercase letters, numbers and -
Does not end with a -
Does not exceed 63 characters
Replace the service with its new configuration using the following
command:
gcloudrunservicesreplaceservice.yaml
Terraform
Create a secret and a secret version.
resource"google_secret_manager_secret""default"{secret_id="my-secret"replication{auto{}}}resource"google_secret_manager_secret_version""default"{secret=google_secret_manager_secret.default.namesecret_data="this is secret data"}
Create a service account and grant it access to the secret:
resource"google_service_account""default"{account_id="cloud-run-service-account"display_name="Service account for Cloud Run"}resource"google_secret_manager_secret_iam_member""default"{secret_id=google_secret_manager_secret.default.idrole="roles/secretmanager.secretAccessor" # Grant the new deployed service account access to this secret.member="serviceAccount:${google_service_account.default.email}"depends_on=[google_secret_manager_secret.default]}
Secret Manager secrets can be accessed from Cloud Run as mounted file paths or as environment variables.
For secrets mounted as file paths, reference the Secret Manager resource in the volumes parameter. The name corresponds with an entry in the volume_mounts parameter:
resource"google_cloud_run_v2_service""mounted_secret"{name="service-with-mounted-secret"location="us-central1"ingress="INGRESS_TRAFFIC_ALL"deletion_protection=false # set to "true" in productiontemplate{volumes{name="my-service-volume"secret{secret=google_secret_manager_secret.default.secret_iditems{version="latest"path="my-secret"mode=0 # use default 0444}}}containers{image="us-docker.pkg.dev/cloudrun/container/hello"volume_mounts{name="my-service-volume"mount_path="/secrets"}}service_account=google_service_account.default.email}depends_on=[google_secret_manager_secret_version.default]}
For secrets exposed as environment variables, reference the Secret Manager resource in the env parameter:
resource"google_cloud_run_v2_service""env_variable_secret"{name="service-with-env-var-secret"location="us-central1"ingress="INGRESS_TRAFFIC_ALL"deletion_protection=false # set to "true" in productiontemplate{containers{image="us-docker.pkg.dev/cloudrun/container/hello"env{name="MY_SECRET"value_source{secret_key_ref{secret=google_secret_manager_secret.default.secret_idversion="latest"}}}}service_account=google_service_account.default.email}depends_on=[google_secret_manager_secret_version.default]}
Compose
To specify secrets in your compose.yaml file, add the secrets attribute
to your service definition. Doing so creates a Secret Manager
secret to store this data based on the value in the local file.
After deployment, the Cloud Run service URL is displayed. Copy
this URL and paste it into your browser to view the running container. You can
disable the default authentication from the Google Cloud console.
Reference secrets from other projects
To reference a secret from another project, verify that your project's service
account has access to the secret.
Console
In the Google Cloud console, go to the Cloud Run Services page:
Click Deploy container to configure a
new service. Fill out the initial service
settings page, then click Containers, Networking, Security to expand the
service configuration page.
If you are configuring an existing service, click the
service, and click Edit and deploy new revision.
Follow the steps to mount the secret as a volume, or expose the secret as
an environment variable.
To expose the secret as an environment variable:
Click the Container(s) tab.
In the Variables and Secrets tab, click Reference a secret.
In the Name 1 field, enter the name of the environment variable.
From the Secret list, click Enter secret manually.
Enter the secret's resource ID in the following format:
projects/PROJECT_NUMBER/secrets/SECRET_NAME
Replace the following:
PROJECT_NUMBER with your Google Cloud project number. For
detailed instructions on how to find your project number, see
Creating and managing projects.
SECRET_NAME: The name of the secret in
Secret Manager.
From the Version 1 list, select the version of the secret to reference.
Click Done.
Click Create or Deploy.
To mount the secret as a volume:
Click the Volumes tab.
Click Mount volume.
Click Secret.
In the Mount path field, enter the mount path for this secret.
This is the directory where all versions of your secret are placed.
From the Secret list, click Enter secret manually.
Enter the secret's resource ID in the following format:
projects/PROJECT_NUMBER/secrets/SECRET_NAME
Replace the following:
PROJECT_NUMBER with your Google Cloud project number. For
detailed instructions on how to find your project number, see
Creating and managing projects.
SECRET_NAME: The name of the secret in
Secret Manager.
In the Path 1 field, enter the name of the file to mount.
In the Version 1 list, select the version of the secret to
reference. By default, the latest version is selected. You can select
a specific version if you want.
Click Save.
Click Create or Deploy.
gcloud
To mount a secret as a volume when deploying a service:
IMAGE_URL: a reference to the container image, for
example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,
the repositoryREPO_NAME must
already be created. The URL follows the format of LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
.
PATH: the mount path of the volume and
filename of the secret. It must start with a leading slash—for example:
/etc/secrets/dbconfig/password, where /etc/secrets/dbconfig/ is the
mount path of the volume, and password is the filename of the secret.
PROJECT_NUMBER: the project number for the
project the secret was created in.
SECRET_NAME: the secret name—for example,
mysecret.
VERSION: the secret version. Use latest
for latest version, or a number—for example, 2.
YAML
If you are creating a new service, skip this step.
If you are updating an existing service, download its YAML configuration:
IMAGE_URL: a reference to the container image, for
example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,
the repositoryREPO_NAME must
already be created. The URL follows the format of LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
ENV_VAR: the name of the environment variable.
PROJECT_NUMBER: the project number for the
project the secret was created in.
SECRET_NAME: the secret name—for example, mysecret.
VERSION: the secret version. Use latest
for latest version, or a number—for example, 2.
SECRET_LOOKUP_NAME: any name that has a
valid secret name syntax—for example, my-secret, it can be the same as
SECRET_NAME.
IMAGE_URL: a reference to the container image, for
example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,
the repositoryREPO_NAME must
already be created. The URL follows the format of LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
.
PATH: the mount path of the volume and
filename of the secret. It must start with a leading slash—for example:
/etc/secrets/dbconfig/password, where /etc/secrets/dbconfig/ is the
mount path of the volume, and password is the filename of the secret.
PROJECT_NUMBER: the project number for the
project the secret was created in.
SECRET_NAME: the secret name—for example, mysecret.
VERSION: the secret version. Use latest
for latest version, or a number—for example, 2.
SECRET_LOOKUP_NAME: any name that has a
valid secret name syntax—for example, my-secret, it can be the same as
SECRET_NAME.
VOLUME_NAME: any name—for example, my-volume,
it can be the same as SECRET_NAME.
REGION: the Google Cloud region. For example,
europe-west1.
IMAGE_URL: a reference to the container image, for
example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,
the repositoryREPO_NAME must
already be created. The URL follows the format of LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
.
SECRET_NAME: the secret name—for example mysecret.
PROJECT_ID: the project ID the secret was created in.
VERSION: the secret version. Use latest for latest
version, or a number—for example, 2.
REGION with the Google Cloud region. For example,
europe-west1.
IMAGE_URL: a reference to the container image, for
example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry,
the repositoryREPO_NAME must
already be created. The URL follows the format of LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
VOLUME_NAME: any name—for example, my-volume, it can be the
same as SECRET_NAME
MOUNT_PATH: the mount path of the volume and
filename of the secret. It must start with a leading slash—for example:
/etc/secrets/dbconfig/password, where /etc/secrets/dbconfig/ is the
mount path of the volume, and password is the filename of the secret.
PROJECT_ID: the project ID the secret was created in.
SECRET_NAME: the secret name—for example mysecret.
View secrets settings
To view the current secrets settings for your
Cloud Run service:
If the secret is mounted as a volume in Cloud Run, and the last
directory in the volume mount path already exists, then any files or folders in
the existing directory become inaccessible.
For example, if a secret called my-secret is mounted to path
/etc/app_data, all the contents inside the app_data directory will be
overwritten, and the only visible file is /etc/app_data/my-secret.
To avoid overwriting files in an existing directory, create a new directory for
mounting the secret, for example, /etc/app_data/secrets, so that the mount
path for the secret is /etc/app_data/secrets/my-secret.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-06-09 UTC."],[],[]]