This page describes how to configure environment variables for your Cloud Run worker pool.
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.
Required roles
To get the permissions that you need to configure and deploy Cloud Run worker pools, ask your administrator to grant you the following IAM roles:
-
Cloud Run Developer (
roles/run.developer
) on the Cloud Run worker pool -
Service Account User (
roles/iam.serviceAccountUser
) on the service identity
For a list of IAM roles and permissions that are associated with Cloud Run, see Cloud Run IAM roles and Cloud Run IAM permissions. If your Cloud Run worker pool interfaces with Google Cloud APIs, such as Cloud Client Libraries, see the service identity configuration guide. For more information about granting roles, see deployment permissions and manage access.
Set environment variables
You can set environment variables for a Cloud Run worker pool using the Google Cloud console, the Google Cloud CLI, or Terraform:
Console
In the Google Cloud console, go to Cloud Run:
Select Worker pools from the menu, and click Deploy container to configure a new worker pool. If you are configuring an existing worker pool, click the worker pool, then click Edit and deploy new revision.
If you are configuring a new worker pool, fill out the initial worker pool page, then click Container(s), Volumes, Networking, Security to expand the worker pools configuration page.
Click the Container tab.
- In the Variables & Secrets tab, click Add Variable to add a new environment variable, then specify the name you want for the variable in the Name and Value fields. For more information on how to set multiple environment variables or escape special characters, see Set multiple environment variables.
Click Create or Deploy.
gcloud
To specify environment variables while deploying your worker pool, use the
--set-env-vars
flag:
gcloud beta run worker-pools deploy WORKER_POOL --image IMAGE_URL --set-env-vars KEY1=VALUE1,KEY2=VALUE2
Replace the following:
- WORKER_POOL: the name of your worker pool
- KEY1=VALUE1,KEY2=VALUE2: the comma-separated list of variable names and values
- IMAGE_URL: a reference to the container image that
contains the worker pool, such as
us-docker.pkg.dev/cloudrun/container/worker-pool:latest
For more information on how to set multiple environment variables or escape special characters, see Set multiple environment variables.
Terraform
To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.
resource "google_cloud_run_v2_worker_pool" "default" {
name = "WORKER_POOL"
location = "REGION"
launch_stage = "BETA"
template {
containers {
image = "IMAGE_URL"
env {
name = "KEY1"
value = "VALUE1"
}
env {
name = "KEY2"
value = "VALUE2"
}
}
}
}
Replace the following:
- WORKER_POOL: the name of the worker pool.
- REGION: the Google Cloud region—for example,
europe-west1
- IMAGE_URL: a reference to the container image that
contains the worker pool, such as
us-docker.pkg.dev/cloudrun/container/worker-pool:latest
- KEY1 and VALUE1: the environment variable and value. Optionally add more variables and values as needed.
Set default environment variables in the container
You can use the ENV
statement in a Dockerfile to set default values for environment variables:
ENV KEY1=VALUE1,KEY2=VALUE2
Order of precedence: container versus worker pool variables
If you set a default environment variable in the container and also set an environment variable with the same name on the Cloud Run worker pool, the value set on the worker pool takes precedence.
Set multiple environment variables
You can set multiple environment variables by using the .env
file or the
--set-env-vars
flag.
Set multiple environment variables using the .env
file
Console
In the Google Cloud console, go to Cloud Run:
Select Worker pools from the menu, and click Deploy container to configure a new worker pool. If you are configuring an existing worker pool, click the worker pool, then click Edit and deploy new revision.
If you are configuring a new worker pool, fill out the initial worker pool page, then click Container(s), Volumes, Networking, Security to expand the worker pools configuration page.
Click the Container tab.
- In the Variables & Secrets tab, click Add Variable and paste the contents of
your
.env
file into the Name field. Cloud Run automatically populates the Value field and creates new variables for each key-value pair you define in the.env
file.
- In the Variables & Secrets tab, click Add Variable and paste the contents of
your
Click Create or Deploy.
gcloud
To specify multiple environment variables from the .env
file, run the following
command:
gcloud beta run worker-pools deploy WORKER_POOL --image IMAGE_URL --env-vars-file=ENV_FILE_PATH
Replace the following:
- WORKER_POOL: the name of the worker pool.
- IMAGE_URL: a reference to the container image that
contains the worker pool, such as
us-docker.pkg.dev/cloudrun/container/worker-pool:latest
- ENV_FILE_PATH: path to the
.env
file.
Set multiple environment variables using the --set-env-vars
flag
If you have multiple environment variables that cannot be listed in KEY1=VALUE1,KEY2=VALUE2
format, you can repeat the --set-env-vars
flag multiple times:
[...] --set-env-vars "KEY1=VALUE1" \ --set-env-vars "KEY2=VALUE2" \ --set-env-vars "KEY3=VALUE3"
Escape comma characters
Because the comma character,
is used to split environment variables, if your
environment variable contains comma characters as values, you need to
escape those delimiters by specifying a different
delimiter character, for example, @
:
--set-env-vars "^@^KEY1=value1,value2,value3@KEY2=..."
Update environment variables
You can update runtime environment variables for existing worker pools. This is a non-destructive approach that changes or adds runtime environment variables, but doesn't delete them.
You can update environment variables in Google Cloud console, the Google Cloud CLI, or Terraform:
Console
In the Google Cloud console, go to Cloud Run:
Select Worker pools from the menu, and click the worker pool you are updating, then click Edit and deploy new revision.
Click Container(s), Volumes, Networking, Security to expand the worker pools configuration page.
Click the Variables & Secrets tab.
Locate the environment variable you want to update, then specify a different name for the variable, or a different value in the Name and Value fields.
Click Deploy.
gcloud
To update environment variables of an existing worker pool, use the
--update-env-vars
flag:
gcloud beta run worker-pools update WORKER_POOL --update-env-vars KEY1=VALUE1,KEY2=VALUE2
Replace the following:
- WORKER_POOL: the name of your worker pool
- KEY1=VALUE1,KEY2=VALUE2: the comma-separated list of variable names and values
Terraform
To update environment variables of an existing worker pool, open the main.tf
file corresponding to the worker pool and edit the name and value variables.
Then, run the command to apply the Terraform configuration.
To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.
Delete environment variables
Console
In the Google Cloud console, go to Cloud Run:
Select Worker pools from the menu, and click the worker pool you are updating, then click Edit and deploy new revision.
Click Container(s), Volumes, Networking, Security to expand the worker pools configuration page.
Click the Variables & Secrets tab.
Locate the environment variable you want to delete and click the trash icon at the right of the Value field to delete the environment variable.
Click Deploy.
gcloud
To selectively remove environment variables from an existing worker pool, use
the --remove-env-vars
flag:
gcloud beta run worker-pools update WORKER_POOL --remove-env-vars KEY1,KEY2
Replace the following:
- WORKER_POOL: the name of your worker pool
- KEY1,KEY2: the comma-separated list of variable names
Alternatively, clear all previously set environment variables
with the --clear-env-vars
flag:
gcloud beta run worker-pools update WORKER_POOL --clear-env-vars
Replace WORKER_POOL with the name of your worker pool.
View environment variable configuration for the worker pool
In the Google Cloud console, go to Cloud Run:
Click Worker pools to display the list of deployed worker pools.
Click the worker pool you want to examine to display its details pane.
Click the Containers tab to display worker pool container configuration.
Sample code
For a code sample that shows how to access environment variables in your code, refer to Handling sensitive configuration with Secret Manager in the End user authentication tutorial.
What's next
You can use environment variables to set a buildpacks configuration. For language-specific details, see the buildpacks documentation for: