Environment variables reference for the Compute platform
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)
.
- FASTLY_CACHE_GENERATION
- FASTLY_CUSTOMER_ID
- FASTLY_HOSTNAME
- FASTLY_POP
- FASTLY_REGION
- FASTLY_SERVICE_ID
- FASTLY_SERVICE_VERSION
- FASTLY_TRACE_ID
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")); // productionconsole.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=productionexport DEBUG=trueexport PORT=3000js-compute-runtime --env NODE_ENV,DEBUG=false,PORT ./src/index.js ./bin/main.wasm
import { env } from 'fastly:env';console.log(env("NODE_ENV")); // productionconsole.log(env("DEBUG")); // falseconsole.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 environmentWriting 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.
- Rust
- JavaScript
- 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.