How to pass environemnt variables to K6

Hello Everyone,

Wanted to ask how can I pass existing environment variables to my k6 test, I have the following test:

import "core-js";
import "regenerator-runtime/runtime";

import { sleep, check, group } from "k6";
import { Options } from "k6/options";
import { Rate } from "k6/metrics";
import http from "k6/http";
import { ProxyBaseURL } from "proxy-client-ts";
// import { SSMClient, GetParametersCommand } from "@aws-sdk/client-ssm";
import SSMClient from "aws-sdk/clients/ssm";

export let options: Options = {
  vus: 1,
  duration: "1s",
  thresholds: {
    // 90% of requests must finish within 3s.
    http_req_duration: ["p(95) < 3000"],
    // http errors should be less than 1% for health checks
    health_req_failed: ["rate<0.01"],
  },
};

console.log(__ENV.AWS_ACCESSKEY);

const ssmPath = "/auth/asymmetric/private-key/tenant-service";
const client = new SSMClient({
  region: "us-west-2",
  credentials: {
    accessKeyId: __ENV.AWS_ACCESSKEY,
    secretAccessKey: __ENV.AWS_SECRETKEY,
  },
});
// const command = new GetParametersCommand({
//   Names: [ssmPath],
//   WithDecryption: true,
// });

// creating a custom rate metrics
const health_failed = new Rate("health_req_failed");

export default () => {
  console.log("before");
  group("hitting myself endpoint with authentication", async function () {
    console.log("inside");
    try {
      const keyPairs = await client
        .getParameter({
          Name: ssmPath,
          WithDecryption: true,
        })
        .promise();

      console.log(keyPairs);
    } catch (error) {
      console.log(error);
    }
    console.log("after");

    const url = `${ProxyBaseURL.DEV_US}/proxy-service/rest/api/2/proxy/rest/api/2/myself`;
    const res = http.get(url);
    check(res, {
      "status is 401": () => res.status === 401,
    });
    sleep(1);
  });
};

I have tried to run this script with the following commands:

  • aws-vault exec tenant-dev -- k6 run --include-system-env-vars ./dist/proxy.test.js
  • k6 run --include-system-env-vars -e AWS_ACCESSKEY=$AWS_ACCESSKEY -e AWS_SECRETKEY=$AWS_SECRETKEY ./dist/proxy.test.js

but I alwayse get the following error:

INFO[0000] CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

when i tried to use this command k6 run --include-system-env-vars -e AWS_ACCESSKEY=$AWS_ACCESSKEY -e AWS_SECRETKEY=$AWS_SECRETKEY ./dist/proxy.test.js it seems that it assigns the text value to the env variable which is not what i want.

so my question is how do i actually pass those credentaisl down to k6?

HI @abekkai,

what you have done is how you pass environment variables to k6. But, I have no idea what @aws-sdk/* expects, as AFAIK __ENV is a k6 invention :man_shrugging: . What you have done in the code seem correct, but you seem to have lower cased the first letter of AccessKeyId and SecretAccessKey and I expect this is the problem :man_shrugging: .

You also do not need --include-system-env-vars if you are using k6 run it is true then. It’s false for k6 archive and k6 cloud so that it doesn’t just write/send all your env variables around, which will be a security issue.
If you prefer using -e (which I would recommend as it’s more explicit and will work with k6 archive/cloud) you can set --include-system-env-vars to false , but that isn’t really needed as well.

it seems that it assigns the text value to the env variable which is not what i want.

What do you want? And can you expand on what you - possibly with an example code and output and expected output.

Hope this helps you