Using Environment Variables
You can set arbitrary key/value pairs for Cloud Functions at deployment time. These pairs are surfaced as either literal environment variables, accessible by your code at runtime, or as configuration information for Google Cloud's buildpacks. They are stored in the Cloud Functions backend, are bound to a single function, and exist within the same lifecycle as the function to which they are bound.
Using runtime environment variables
Runtime environment variables are key/value pairs deployed alongside a function. These variables are scoped to the function and are not visible to other functions in your project. You can add or remove runtime environment variables using either the Google Cloud CLI or the Google Cloud console UI.
Setting runtime environment variables
Use these methods to establish new variables or completely replace existing variables. To make additive changes, use the update process described in the next section.
gcloud
To set a runtime environment variable using the Google Cloud CLI,
use the --set-env-vars
flag at deploy time:
gcloud functions deploy FUNCTION_NAME --set-env-vars FOO=bar FLAGS ...
You can also set multiple runtime environment variables using a comma separated list:
gcloud functions deploy FUNCTION_NAME --set-env-vars FOO=bar,BAZ=boo FLAGS...
If you want to store your configuration in a file (e.g. under source
control), you can use a YAML file together with the --env-vars-file
flag:
gcloud functions deploy FUNCTION_NAME --env-vars-file .env.yaml FLAGS...
where the contents of the .env.yaml
file are:
FOO: bar
BAZ: boo
In the above examples, FLAGS...
refers to other
options that you pass during deployment of your function. For a complete
reference for the deploy
command, see gcloud functions deploy
.
Google Cloud console UI
You can set runtime environment variables during function creation in the Google Cloud console:
Open the Functions Overview page in the Google Cloud console:
Click Create function.
Fill in the required fields for your function.
Open the Runtime, build and connections settings section.
Select the Runtime tab.
In the Runtime environment variables section, click Add variable and add the name and value.
For instructions on how to add environment variables to an existing function, see Updating runtime environment variables.
Updating runtime environment variables
You can also update runtime environment variables for existing functions. This is a non-destructive approach that changes or adds runtime environment variables, but does not delete.
gcloud
To update a variable using the Google Cloud CLI, use the
--update-env-vars
flag at deploy time:
gcloud functions deploy FUNCTION_NAME --update-env-vars FOO=bar
You can also update multiple runtime environment variables using a comma separated list:
gcloud functions deploy FUNCTION_NAME --update-env-vars FOO=bar,BAZ=boo
Google Cloud console UI
To update runtime environment variables using the Google Cloud console:
Open the Functions Overview page in the Google Cloud console:
Click an existing function to go to its details page.
Click Edit.
Open the Runtime, build and connections settings section.
Select the Runtime tab.
Make your edits in the Runtime environment variables section.
Deleting runtime environment variables
gcloud
If you want to selectively remove runtime environment variables you can use the
--remove-env-vars
flag at deploy time:
gcloud functions deploy FUNCTION_NAME --remove-env-vars FOO,BAZ
Alternatively, you can clear all previously set runtime environment variables
with the --clear-env-vars
flag:
gcloud functions deploy FUNCTION_NAME --clear-env-vars
Google Cloud console UI
To delete runtime environment variables using the Google Cloud console:
Open the Functions Overview page in the Google Cloud console:
Click an existing function to go to its details page.
Click Edit.
Open the Runtime, build and connections settings section.
Select the Runtime tab.
In the Runtime environment variables section, click the garbage icon next to the key/value pair to delete it.
Runtime environment variables set automatically
This section lists runtime environment variables that are set automatically.
Python 3.7 and Go 1.11
The following runtime environment variables are set automatically for the Python 3.7 and Go 1.11 (deprecated) runtimes. All other Cloud Functions runtimes use a more limited set of environment variables, as described in the newer runtimes section.
Key | Description |
---|---|
ENTRY_POINT |
Reserved: The function to be executed. |
GCP_PROJECT |
Reserved: The current Google Cloud project ID. |
GCLOUD_PROJECT |
Reserved: The current Google Cloud project ID (deprecated). |
GOOGLE_CLOUD_PROJECT |
Reserved: Not set but reserved for internal use. |
FUNCTION_TRIGGER_TYPE |
Reserved: The trigger type of the function. |
FUNCTION_NAME |
Reserved: The name of the function resource. |
FUNCTION_MEMORY_MB |
Reserved: The maximum memory of the function. |
FUNCTION_TIMEOUT_SEC |
Reserved: The execution timeout in seconds. |
FUNCTION_IDENTITY |
Reserved: The current identity (service account) of the function. |
FUNCTION_REGION |
Reserved: The function region (example: us-central1 ). |
Newer runtimes
Newer runtimes automatically set fewer environment variables than older ones. Any languages and runtimes not mentioned in the previous section use this more limited set of predefined environment variables.
Key | Description |
---|---|
FUNCTION_TARGET |
Reserved: The function to be executed. |
FUNCTION_SIGNATURE_TYPE |
Reserved: The type of the function: http for HTTP
functions, and event for event-driven functions.
|
K_SERVICE |
Reserved: The name of the function resource. |
K_REVISION |
Reserved: The version identifier of the function. |
PORT |
Reserved: The port over which the function is invoked. |
gcloud functions deploy envVarMemory \
--runtime nodejs10 \
--set-env-vars FUNCTION_MEMORY_MB=2Gi \
--memory 2Gi \
--trigger-http
Setting and retrieving runtime environment variables: an example
Set the runtime environment variable:
Node.js
gcloud functions deploy envVar \ --runtime nodejs18 \ --set-env-vars FOO=bar \ --trigger-httpYou can use the following values for the
--runtime
flag to specify your preferred Node.js version:
nodejs18
(recommended)nodejs20
(preview)nodejs16
nodejs14
nodejs12
nodejs10
Python
gcloud functions deploy env_vars \ --runtime python311 \ --set-env-vars FOO=bar \ --trigger-httpYou can use the following values for the
--runtime
flag to specify your preferred Python version:
python311
(recommended)python310
python39
python38
python37
Go
gcloud functions deploy EnvVar \ --runtime go120 \ --set-env-vars FOO=bar \ --trigger-httpYou can use the following values for the
--runtime
flag to specify your preferred Go version:
go120
(recommended)go119
go118
go116
go113
Java
gcloud functions deploy java-envVar-function \ --entry-point functions.EnvVars \ --runtime java17 \ --memory 512MB \ --set-env-vars FOO=bar \ --trigger-httpYou can use the following values for the
--runtime
flag to specify your preferred Java version:
java17
(recommended)java11
C#
gcloud functions deploy csharp-envVar-function \ --entry-point EnvironmentVariables.Function \ --runtime dotnet6 \ --set-env-vars FOO=bar \ --trigger-httpYou can use the following values for the
--runtime
flag to specify your preferred .NET version:
dotnet6
(recommended)dotnet3
Ruby
gcloud functions deploy env_vars --runtime ruby32 \ --set-env-vars FOO=bar \ --trigger-httpYou can use the following values for the
--runtime
flag to specify your preferred Ruby version:
ruby32
(recommended)ruby30
ruby27
ruby26
PHP
gcloud functions deploy envVar --runtime php82 \ --set-env-vars FOO=bar \ --trigger-httpYou can use the following values for the
--runtime
flag to specify your preferred PHP version:
php82
(recommended)php81
php74
At runtime access the variables programmatically:
Node.js
In Node.js use the process.env
property to access runtime environment variables:
Python
In Python use os.environ
to access runtime environment variables:
Go
In Go use os.Getenv()
to access runtime environment variables:
Java
In Java use System.getenv
to access runtime environment variables:
C#
At runtime, environment variables are accessible usingEnvironment.GetEnvironmentVariable
in C#:
Ruby
At runtime, environment variables are accessible usingENV
in Ruby:
PHP
At runtime, environment variables are accessible using PHP'sgetenv
function:
Using build environment variables
You can also set build environment variables for runtimes that support buildpacks.
Build environment variables are key/value pairs deployed alongside a function that let you pass configuration information to buildpacks. For example, you might want to customize compiler options. You can add or remove these build environment variables using either the Google Cloud CLI or the Google Cloud console UI.
Setting build environment variables
Use these methods to establish new variables or completely replace existing
variables. To make additive changes, use the update process (the
--update-build-env-vars
flag in gcloud
) described in the next section.
gcloud
To set a variable using the Google Cloud CLI, use the
--set-build-env-vars
flag at deploy time:
gcloud beta functions deploy FUNCTION_NAME --set-build-env-vars FOO=bar FLAGS...
You can also set multiple build environment variables using a comma separated list:
gcloud functions deploy FUNCTION_NAME --set-build-env-vars FOO=bar,BAZ=boo FLAGS...
If you want to store your configuration in a file (e.g. under source control),
you can use a YAML file together with the --build-env-vars-file
flag:
gcloud functions deploy FUNCTION_NAME --build-env-vars-file FILE_NAME.yaml FLAGS...
where the contents of the *.yaml
file are:
FOO: bar
BAZ: boo
In the above examples, FLAGS...
refers to other options
that you pass during deployment of your function. For a complete reference
for the deploy
command, see gcloud beta functions deploy
.
Google Cloud console UI
You can also set build environment variables during function creation in the Google Cloud console:
Open the Functions Overview page in the Google Cloud console:
Click Create function.
Fill in the required fields for your function.
Open the Runtime, build and connections settings section.
Select the Build tab.
In the Build environment variables section, click Add variable and add the name and value.
Updating build environment variables
You can also update build environment variables for existing functions. This is a non-destructive approach that changes or adds build environment variables, but does not delete.
gcloud
To set a variable using the Google Cloud CLI, use the
--update-build-env-vars
flag at deploy time:
gcloud functions deploy FUNCTION_NAME --update-build-env-vars FOO=bar
You can also update multiple build environment variables using a comma separated list:
gcloud functions deploy FUNCTION_NAME --update-build-env-vars FOO=bar,BAZ=boo
Google Cloud console UI
To update build environment variables using the Google Cloud console:
Open the Functions Overview page in the Google Cloud console:
Click an existing function to go to its details page.
Click Edit.
Open the Runtime, build and connections settings section.
Select the Build tab.
Make your edits in the Build environment variables section.
Deleting build environment variables
gcloud
If you want to selectively remove build environment variables you can use the
--remove-build-env-vars
flag at deploy time:
gcloud functions deploy FUNCTION_NAME --remove-build-env-vars FOO,BAZ
Alternatively, you can clear all previously set build environment variables with the
--clear-build-env-vars
flag:
gcloud functions deploy FUNCTION_NAME --clear-build-env-vars
Google Cloud console UI
To delete build environment variables using the Google Cloud console:
Open the Functions Overview page in the Google Cloud console:
Click an existing function to go to its details page.
Click Edit.
Open the Runtime, build and connections settings section.
Select the Build tab.
In the Build environment variables section, click the garbage icon next to the key/value pair to delete it.
Variable lifecycle
All environment variables are bound to a deployment of a Cloud Function, and can only be set or changed with a deployment. If a deployment fails for any reason, any changes to environment variables will not be applied. Environment variable changes require a successful deployment.
Best practices and reserved environment variables
Some additional environment variables are automatically set depending on the
runtime your function uses. These are based on the runtime's operating system
(for example, DEBIAN_FRONTEND
, SHLVL
, or PATH
) and the language runtime
(for example, NODE_ENV
, VIRTUAL_ENV
, or GOPATH
).
Environment variables that are provided by the environment (other than the ones listed in Environment variables set automatically) might change in future runtime versions. As a best practice, we recommend that you do not depend on or modify any environment variables that you have not set explicitly.
Modifying environment variables that are provided by the environment might lead to unpredictable outcomes. Attempts to modify such environment variables could be blocked or, worse, lead to unintended consequences such as functions that cannot start. As a best practice, consider prefixing any environment variables with a unique key to avoid conflicts.
Finally, you cannot use the following environment variables:
Key | Description |
---|---|
Empty ('') | Keys cannot be an empty string. |
= |
Keys cannot contain the '=' character. |
X_GOOGLE_ |
Keys cannot contain the prefix X_GOOGLE_ . |
Size limits
The total number of bytes used by runtime environment variable names and values for an individual function is limited to 32KiB. There are no specific limits on individual keys or values within this overall capacity.
For build environment variables, up to 100 variables can be defined, with the
definition string foo=bar
limited to 64KiB.
Managing secrets
Environment variables can be used for function configuration, but are not recommended as a way to store secrets such as database credentials or API keys. These more sensitive values should be stored outside both your source code and outside environment variables. Some execution environments or the use of some frameworks can result in the contents of environment variables being sent to logs, and storing sensitive credentials in YAML files, deployment scripts or under source control is not recommended.
For storing secrets, we recommend that you review the best practices for secret management. Note that there is no Cloud Functions-specific integration with Cloud KMS.
Portability
It is possible that environment variables that currently work with your Cloud Function will not work with a different runtime environment, such as in a different language or with certain tools or libraries. It is also possible that they will not be accepted by a different platform.
You can avoid such issues by following the
POSIX standard for environment variables.
If you use the Google Cloud console to edit variables,
Google Cloud console will warn you whenever you define a variable that might
have portability issues (but will not prevent deployment). As a general rule, we
recommend that environment variable keys consist solely of uppercase letters,
digits, and <underscore>
(_
), as defined in the
Portable Character Set,
and that they do not begin with a digit.