Using Environment Variables

Environment variables in Cloud Functions is a feature that allows you to specify arbitrary key/value pairs at the time of deployment which will be surfaced as literal environment variables accessible by your code at runtime. Environment variables are stored in the Cloud Functions backend, are bound to a single function, and adopt the same lifecycle as the function to which they are bound.

Using environment variables

Environment variables are a set of key/value pairs deployed alongside a function. These variables are scoped to the function and are not visible by other functions in your project. You can add or remove environment variables using both the command line (gcloud) and UI (GCP Console).

Setting environment variables

To set a variable using the gcloud command-line tool, use the --set-env-vars flag at deploy time:

gcloud beta functions deploy FUNCTION_NAME --set-env-vars FOO=bar FLAGS...

You can also set multiple environment variables using a comma separated list:

gcloud beta 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 beta 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, including beta features, see gcloud beta functions deploy.

You can also set environment variables during function creation in the GCP Console. To do so:

  1. Go to the Cloud Functions Overview page.

  2. Click Create function.

  3. Fill in the required fields for your function.

  4. Expand the advanced settings by clicking More.

  5. In the Environment variables section, set variables by clicking Add variable.

Updating environment variables

You can also update environment variables for existing functions. This is a non-destructive approach that changes or adds environment variables, but does not delete.

To set a variable using the gcloud command-line tool, use the --update-env-vars flag at deploy time:

gcloud beta functions deploy FUNCTION_NAME --update-env-vars FOO=bar

You can also update multiple environment variables using a comma separated list:

gcloud beta functions deploy FUNCTION_NAME --update-env-vars FOO=bar,BAZ=boo

To update environment variables using the GCP Console:

  1. Go to the Cloud Functions Overview page.

  2. Click an existing function to go to its details page.

  3. Click Edit.

  4. Expand the advanced settings by clicking More.

  5. In the Environment variables section, edit existing variables or click Add variable to set new variables.

Deleting environment variables

If you want to selectively remove environment variables you can use the --remove-env-vars flag at deploy time:

gcloud beta functions deploy FUNCTION_NAME --remove-env-vars FOO

Alternatively, you can clear all previously set environment variables with the --clear-env-vars flag:

gcloud beta functions deploy FUNCTION_NAME --clear-env-vars

To delete environment variables using the GCP Console:

  1. Go to the Cloud Functions Overview page.

  2. Click an existing function to go to its details page.

  3. Click Edit.

  4. Expand the advanced settings by clicking More.

  5. In the Environment variables section, delete a variable by clicking the X next to it.

Accessing environment variables at runtime

Node.js

At runtime, environment variables are accessible via the process.env property in Node.js.

Set the environment variable:

gcloud beta functions deploy envVar --set-env-vars FOO=bar --trigger-http --runtime nodejs6

Python (Beta)

At runtime, environment variables are accessible via os.environ.

Set the environment variable:

gcloud beta functions deploy env_vars --set-env-vars FOO=bar --trigger-http --runtime python37

Access the environment variable:

Node.js

exports.envVar = (req, res) => {
  // Sends 'bar' as response
  res.send(process.env.FOO);
};

Python (Beta)

def env_vars(request):
    return os.environ.get('FOO', 'Specified environment variable is not set.')

Variable lifecycle

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.

Limits & best practices

This section provides guidelines for using environment variables.

Size limits

The total number of bytes used by environment variable names and values for an individual function is limited to 32KiB. However, there are no specific limits on individual keys or values within this overall capacity.

Reserved keys & key validation

Certain environment variables are reserved by the environment and you cannot set variables with these keys.

The following keys are currently reserved:

Key Description
Empty ('') Keys cannot be an empty string.
= Keys cannot contain the '=' character.
X_GOOGLE_ Any key prefixed with X_GOOGLE_ will be rejected.
NODE_ENV Reserved: The current Node.js environment (default: production).
GCP_PROJECT Reserved: The current GCP project ID.
GOOGLE_CLOUD_PROJECT Reserved: Internal use.
GCLOUD_PROJECT Reserved: The current GCP project ID (legacy).
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 (e.g. us-central1).
CODE_LOCATION Reserved: The local path to the function source code.
ENTRY_POINT Reserved: The function to be executed.
SUPERVISOR_HOSTNAME Reserved: Internal use.
SUPERVISOR_INTERNAL_PORT Reserved: Internal use.
_ Reserved: Internal use.
HOME Reserved: Location of the home directory.
OLDPWD Reserved: Location of the previous working directory.
PWD Reserved: Location of the current working directory.
PORT Reserved: Internal use.
PATH Reserved: The search path for commands.
SHLVL Reserved: Internal use.

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 and/or the use of some frameworks may 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 you review the best practices for secret management.

Portability

It is possible that environment variables that currently work with your Cloud Function will not work with a different runtime environment (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 GCP Console to edit variables, GCP Console will warn you whenever you define a variable that may 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.

Getting Help & Reporting Bugs

If you run into any issues while using environment variables while in beta, post to the Cloud Functions Beta User Group.

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Functions Documentation