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 the buildpack system. 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 gcloud command-line tool or the 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 (the --update-env-vars flag in gcloud) described in the next section.

gcloud

To set a a runtime environment 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 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.

Cloud Console UI

You can set runtime environment variables during function creation in the Cloud Console:

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

    Go to the Cloud Functions Overview page.

  2. Click Create function.

  3. Fill in the required fields for your function.

  4. Open the Variables, networking, and advanced settings section.

  5. Select the Environment variables tab.

  6. In the Runtime environment variables section, click Add variable and add the name and value.

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 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 runtime environment variables using a comma separated list:

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

Cloud Console UI

To update runtime environment variables using the Cloud Console:

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

    Go to the Cloud Functions Overview page.

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

  3. Click Edit.

  4. Open the Variables, networking, and advanced settings section.

  5. Select the Environment variables tab.

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

Cloud Console UI

To delete runtime environment variables using the Cloud Console:

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

    Go to the Cloud Functions Overview page.

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

  3. Click Edit.

  4. Open the Variables, networking, and advanced settings section.

  5. Select the Environment variables tab.

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

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

The following runtime environment variables are set automatically for the Node.js 6, Node.js 8, Python 3.7 and Go 1.11 runtimes. All other 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 languages and runtimes other than those mentioned in the previous section 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.

Setting and retrieving runtime environment variables: an example

Set the runtime environment variable:

Node.js

gcloud functions deploy envVar \
--runtime nodejs10 \
--set-env-vars FOO=bar \
--trigger-http
You can use the following values for the --runtime flag to specify your preferred Node.js version:
  • nodejs10
  • nodejs12

Python

gcloud functions deploy env_vars \
--runtime python37 \
--set-env-vars FOO=bar \
--trigger-http
You can use the following values for the --runtime flag to specify your preferred Python version:
  • python37
  • python38

Go

gcloud functions deploy EnvVar \
--runtime go111 \
--set-env-vars FOO=bar \
--trigger-http
You can use the following values for the --runtime flag to specify your preferred Go version:
  • go111
  • go113

Java

gcloud functions deploy java-envVar-function \
--entry-point functions.EnvVars \
--runtime java11 \
--memory 512MB \
--set-env-vars FOO=bar \
--trigger-http

At runtime access the variables programmatically:

Node.js

In Node.js use the process.env property to access runtime environment variables:

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

Python

In Python use os.environ to access runtime environment variables:

import os


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

Go

In Go use os.Getenv() to access runtime environment variables:


// 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"))
}

Java

In Java use System.getenv to access runtime environment variables:


import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.BufferedWriter;
import java.io.IOException;

public class EnvVars implements HttpFunction {

  // Returns the environment variable "foo" set during function deployment.
  @Override
  public void service(HttpRequest request, HttpResponse response)
      throws IOException {
    BufferedWriter writer = response.getWriter();
    String foo = System.getenv("FOO");
    if (foo == null) {
      foo = "Specified environment variable is not set.";
    }
    writer.write(foo);
  }
}

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 gcloud command-line tool or the Cloud Console UI.

Using gcloud

Because the build environment variables capability is in preview, to use gcloud you must have the gcloud beta component installed.

If you need to install the beta component, or you want to check whether you have it, from the command line run the following command:

gcloud components install beta

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 gcloud command-line tool, 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.

Cloud Console UI

You can also set build environment variables during function creation in the Cloud Console:

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

    Go to the Cloud Functions Overview page.

  2. Click Create function.

  3. Fill in the required fields for your function.

  4. Open the Variables, networking, and advanced settings section.

  5. Select the Environment variables tab.

  6. 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 gcloud command-line tool, 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-env-vars FOO=bar,BAZ=boo

Cloud Console UI

To update build environment variables using the Cloud Console:

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

    Go to the Cloud Functions Overview page.

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

  3. Click Edit.

  4. Open the Variables, networking, and advanced settings section.

  5. Select the Environment variables tab.

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

Cloud Console UI

To delete build environment variables using the Cloud Console:

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

    Go to the Cloud Functions Overview page.

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

  3. Click Edit.

  4. Open the Variables, networking, and advanced settings section.

  5. Select the Environment variables tab.

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