Environment variables reference for the Compute platform

IMPORTANT: The content on this page uses the following versions of Compute SDKs: JavaScript SDK: 3.29.2 (current is 3.32.1, see changes), Rust SDK: 0.11.2 (current), Go SDK: 1.3.3 (current)

Compute services can access environment variables provided by the system. All environment variables are strings, though if a variable does not exist, the SDK will behave appropriately. For example, Rust's std::env::var will return a Result(Err).

Setting custom environment variables at build time

IMPORTANT: This feature is available in the JavaScript SDK.

In a Compute service written in JavaScript, custom values can be added into (and override) the service's environment at build time. To do this, provide the names and values of the environment variables on the js-compute-runtime command used to build your application (usually the "build" script of package.json):

js-compute-runtime --env NODE_ENV=production,FASTLY_HOSTNAME=example.com ./src/index.js ./bin/main.wasm
import { env } from 'fastly:env';
console.log(env("NODE_ENV")); // production
console.log(env("FASTLY_HOSTNAME")); // example.com

It is also possible to insert values from the current process environment into the service's environment by specifying a name without a value:

export NODE_ENV=production
export DEBUG=true
export PORT=3000
js-compute-runtime --env NODE_ENV,DEBUG=false,PORT ./src/index.js ./bin/main.wasm
import { env } from 'fastly:env';
console.log(env("NODE_ENV")); // production
console.log(env("DEBUG")); // false
console.log(env("PORT")); // 3000

NOTE:

  • For environment variables inserted from the current process, the build process will display the following informational message:
    Writing NODE_ENV environment variable into the runtime from the current process environment
    Writing PORT environment variable into the runtime from the current process environment
  • For security reasons, there is no option to include every environment variable from the outer environment.

IMPORTANT: Custom environment variables, whether specified on the --env parameter or obtained from the current process environment, are inserted into the Wasm binary at build time. Making changes to values set this way requires re-building and re-deploying the service. Consider using Config Store or Secret Store for your dynamic configuration needs instead.

IMPORTANT: Avoid setting secrets or sensitive information using this mechanism, as the values will be discoverable by examining the Wasm binary. Consider using Secret Store to store sensitive information instead.

Reading and setting environment variables

The values of environment variables are set when a Compute instance starts running, and do not change during execution. However, environment variables are writable, and will retain any change written to them through the end of the instance lifecycle. Environment variables are scoped to the instance and are destroyed when the instance ends.

  1. Rust
  2. JavaScript
  3. Go

Environment variables in non-production environments

For more information on the availability and values of environment variables in non-production environment, see Testing and debugging.