Using synthetics monitor CLI with .env

  • ECK version: 8.14.0
  • FLEET version: 8.14.0
  • Synthetics Monitor agent version: 8.14.1
  • @elastic/synthetics CLI version: 1.12.1
  • Arch: on premise Kubernetes

Hello everyone!
I currently have a synthetic monitor running with CLI. The stages are working fine for me:

  • Create project
  • Develop the monitor (in .js and .ts)
  • Publish in Kibana

I wanted to set security criteria using a .env file to keep sensitive information

------
.env
-----
PRIVATE_LOCATIONS=oke-svc-prod
SCHEDULE=5
GOTO=https://www.elastic.co/es/import dotenv from 'dotenv';

------
elastic-env.journey.js
-----
import dotenv from 'dotenv';
dotenv.config({path: path.resolve(__dirname, './.env') });

and I encountered two scenarios:

Scenario 1:
Inside monitor.user, I can reference the .env file, and the variables load correctly. I validated this by publishing the changes to Kibana, and the variables loaded fine. The variables I used were PRIVATE_LOCATIONS and SCHEDULE.
However, when I tried to use the GOTO variable in page.goto(process.env.GOTO) , it failed. Sometimes it loads as empty, and other times it literally takes 'process.env.GOTO' as the value.

monitor.use({
    id: 'elastic-env',
    name : 'elastic-env',
    tags : [ 'DOC-DEVOPS' ],
    privateLocations: [process.env.PRIVATE_LOCATIONS],
    schedule: parseInt(process.env.SCHEDULE), // Convertir a número
    screenshot: 'on',
    enabled: true
  });
step('Go to https://www.elastic.co/es/', async () => {
    const gotoPage = process.env.GOTO;
    console.log('pageGoto: ', gotoPage);
    await page.goto(gotoPage);
  });

Scenario 2:
Due to the error when using GOTO in Scenario 1, I updated my synthetic agent deployment by loading the GOTO environment variable into the pod (I'm using K8s). With this change, the monitor worked.

elastic-agent@syntetics-agent-test-b95d84546-f7nqg:~$ env | grep GOTO
GOTO=https://www.elastic.co/es/
elastic-agent@syntetics-agent-test-b95d84546-f7nqg:~$

image


My question is:
Is there a way to use an environment variable from a .env file that can be recognized and used when running npx @elastic/synthetics push?

Hi @Cristian_Pereyra,

You would need to pass in the ENV values as params which can then be consumed in journeys Configure Synthetics projects | Elastic Observability [8.15] | Elastic.

// synthetics.config.ts
export {
   params: {
      GOTO: "https://example.com"
   }
}

// journey.ts
journey('test', ( page, params ) => {
  step('Go to https://www.elastic.co/es/', async () => {
    console.log('pageGoto: ',  params.GOTO);
    await page.goto(params.GOTO);
  });
});

The reason for this is because the local environment is different from the remote locations where we run these monitors and these env values will not be defined.

Thanks,
Vignesh

thanks gor you intressing

i solved that using global.js file. In this solutions i have two advantages

  1. The sensitive information cannot seen in Synthetics Kibana
  2. i became independent from the environment because the env going to npm push
----
globals.js
----
const API_URL = 'https://www.elastic.co/es/';
const PRIVATE_LOCATIONS='oke-svc-prod'
const SCHEDULE=10

module.exports = {
  API_URL,
  PRIVATE_LOCATIONS,
  SCHEDULE
};
----
elastic-env.journey.js
----
const globals = require('./globals');
[..]
  console.log ("url: ", globals.API_URL)
[...]
  monitor.use({
    id: 'elastic-env',
    name : 'elastic-env',
    tags : [ 'DOC-DEVOPS' ],
    privateLocations: [globals.PRIVATE_LOCATIONS],
    schedule: parseInt(globals.SCHEDULE), // Convertir a número
    screenshot: 'on',
    enabled: true
  });
[..]

  step('Go to https://www.elastic.co/es/', async () => {
    await page.goto(globals.API_URL);
  });