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 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 environment variables using both the gcloud command-line tool and GCP Console UI.

Setting environment variables

To set a variable using the gcloud command-line tool, 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 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.

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

  1. Open the Functions Overview page in the GCP Console:

    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 functions deploy FUNCTION_NAME --update-env-vars FOO=bar

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

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

To update environment variables using the GCP Console:

  1. Open the Functions Overview page in the GCP Console:

    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 functions deploy FUNCTION_NAME --remove-env-vars FOO

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

gcloud functions deploy FUNCTION_NAME --clear-env-vars

To delete environment variables using the GCP Console:

  1. Open the Functions Overview page in the GCP Console:

    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

Set the environment variable during deployment:

Node.js 8

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

Node.js 10 (Beta)

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

Node.js 6 (Deprecated)

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

Python

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

Go

gcloud functions deploy EnvVar --runtime go111 --set-env-vars FOO=bar --trigger-http

Access the environment variable:

Node.js

At runtime, environment variables are accessible via the process.env property in Node.js:
exports.envVar = (req, res) => {
  // Sends 'bar' as response
  res.send(process.env.FOO);
};

Python

At runtime, environment variables are accessible via os.environ in Python:
import os


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

Go

At runtime, environment variables are accessible using os.Getenv() in Go:
// Package tips contains tips for writing Cloud Functions in Go.
package tips

import (
	"fmt"
	"net/http"
	"os"
)

// EnvVar is an example of getting an environment variable in a Go function.
func EnvVar(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "FOO: %q", os.Getenv("FOO"))
}

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.

Environment variables set automatically

This section lists environment variables that are set automatically.

Node.js 6, Node.js 8, Python 3.7 and Go 1.11

The following environment variables are set automatically for the Node.js 6, Node.js 8, Python 3.7 and Go 1.11 runtimes. Subsequent Cloud Functions runtimes use a more limited set of environment variables, as described in Node.js 10 and subsequent runtimes.

Key Description
ENTRY_POINT Reserved: The function to be executed.
GCP_PROJECT Reserved: The current GCP project ID.
GCLOUD_PROJECT Reserved: The current GCP 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).

Node.js 10 and subsequent runtimes

The Node.js 10 runtime automatically sets fewer environment variables than previous runtimes supported by Cloud Functions. All future runtimes on Cloud Functions will 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 background 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.

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 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.

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 GCP Console to edit variables, GCP 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.

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

Send feedback about...

Cloud Functions Documentation