Menu
Documentationbreadcrumb arrow Grafana k6breadcrumb arrow Using k6breadcrumb arrow Environment variables
Open source

Environment variables

Often, scripts need only minor tweaks to be reusable in different contexts. Rather than creating several separate scripts for these different contexts or environments, you can use environment variables to make parts of your script tweakable.

You can use environment variables for two main purposes:

  1. Passing environment variables to the k6 Script
  2. Configuring k6 Options with environment variables

Passing environment variables to the k6 script

In k6, the environment variables are exposed through a global __ENV variable, a JS object. For reference, see the script example below:

JavaScript
import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
  const res = http.get(`http://${__ENV.MY_HOSTNAME}/`);
  sleep(1);
}

The recommended option to pass environment variables to your testing script is to use one or more -e / --env CLI flags (this command works the same for all platforms):

bash
$ k6 run -e MY_HOSTNAME=test.k6.io script.js

⚠ The -e flag does not configure options

This flag just provides variables to the script, which the script can use or ignore. For example, -e K6_ITERATIONS=120 does not configure the script iterations.

Compare this behavior with K6_ITERATIONS=120 k6 run script.js, which does set iterations.

Configure k6 options with environment variables

You can also configure k6 options with environment variables. Consider this script:

JavaScript
import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
  const res = http.get('https://test.k6.io');
  sleep(1);
}

By default, a local run of this script will execute a single iteration with one virtual user(VU). To modify the default behavior, pass k6 options as environment variables. For example, this snippet configures the script to run 10 virtual users for a duration of 10 seconds:

bash
$ K6_VUS=10 K6_DURATION=10s k6 run script.js
windows
C:\k6> set "K6_VUS=10 K6_DURATION=10s" && k6 run script.js
powershell
PS C:\k6> $env:K6_VUS=10 ; $env:K6_DURATION="10s" ; k6 run script.js

As the preceding example shows, you need to prefix K6_ in the environment variable name for k6 to evaluate it as an option parameter. However, be aware that not all options are supported as environment variables. You can confirm whether one is by checking the documentation for each option.

Note that when you define options in multiple places, there’s an order of precedence that determines the option to use. To ensure you’re always working with the highest precedence, use command-line flags instead of environment variables:

bash
$ k6 run -e MY_HOSTNAME=test.k6.io --duration 10s --vus 10 script.js

Read more