# How to overwrite values from config file dynamically

**URL:** <https://community.grafana.com/t/how-to-overwrite-values-from-config-file-dynamically/99495>\
**Category:** OSS Support\
**Created:** [July 27, 2022, 4:29am UTC](https://community.grafana.com/t/how-to-overwrite-values-from-config-file-dynamically/99495 "2022-07-27T04:29:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jac](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jac](https://community.grafana.com/u/Jac)\
**Post date:** [July 27, 2022, 4:29am UTC](https://community.grafana.com/t/how-to-overwrite-values-from-config-file-dynamically/99495/1 "2022-07-27T04:29:26Z")

</div>

I want to config my scenario using config file. However, some parameters in certain scenarios, I would like to set dynamically. For example in the config below for scenario 1, I would like to set start rate and target dynamically by env variables or from CLI when running the script. How can I to achieve this? Is there an example somewhere?

```auto
 "scenarios": {
    "scenario1": {
      "executor": "ramping-arrival-rate",
      "exec": "exec1",
      "preAllocatedVUs": 50,
      "maxVUs": 100,
      "startRate": 10,
      "timeUnit": "1s",
      "stages": [
        {
          "target": 10, "duration": "10s"
        }
      ]
    },

```

---

<div class="post-metadata">

**Author:** ![olegbespalov](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/olegbespalov/32/36739_2.png) [@olegbespalov](https://community.grafana.com/u/olegbespalov)\
**Post date:** [July 27, 2022, 7:51am UTC](https://community.grafana.com/t/how-to-overwrite-values-from-config-file-dynamically/99495/2 "2022-07-27T07:51:29Z")

</div>

Hi @Jac !

Config files are static JSON files, and there you can’t use environment variables. However, you can easily achieve that migration to the `options` inside your scripts.

```auto
import { sleep } from "k6";

export let options = {
   "scenarios": {
      "scenario1": {
        "executor": "ramping-arrival-rate",
        "preAllocatedVUs": 50,
        "maxVUs": 100,
        "startRate": __ENV.MY_RATE,
        "timeUnit": "1s",
        "stages": [
          {
            "target": 10, "duration": "1m"
          }
        ]
      },
   }
}

export default function () {
   console.log("hey");

   sleep(1);
}

```

```auto
 MY_RATE=15 k6 run script.js

```

Let me know if that answers,  
Cheers!

---

<div class="post-metadata">

**Author:** ![Jac](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jac](https://community.grafana.com/u/Jac)\
**Post date:** [July 28, 2022, 1:29am UTC](https://community.grafana.com/t/how-to-overwrite-values-from-config-file-dynamically/99495/3 "2022-07-28T01:29:28Z")

</div>

Hey @olegbespalov  
I need to use config file, because I want to be able to run same code with different configuration.  
I only need to overwrite certain values in the code if they are provided in env variables. Is it possible?

I think what I need is here: [Options reference](https://k6.io/docs/using-k6/k6-options/reference/#stages)  
Because if I provide env variables or CLI options it will overwrite the config file. Right? However, I do not know how to overwrite certain stages for particular scenario. Is there any example?

---

<div class="post-metadata">

**Author:** ![mstoykov](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/mstoykov/32/35309_2.png) [@mstoykov](https://community.grafana.com/u/mstoykov)\
**Post date:** [July 28, 2022, 7:39am UTC](https://community.grafana.com/t/how-to-overwrite-values-from-config-file-dynamically/99495/4 "2022-07-28T07:39:07Z")

</div>

Hi @Jac,

As shown in [Scenario config file - #2 by nedyalko](https://community.grafana.com/t/scenario-config-file/99081/2) you can load the config file as a normal file, parse it from JSON and then set it to `export const options`. You can also just before that change w/e options you want.

This also (as shown there) can be done only for _some_ option keys - you don’t need to write the whole configuration, as needed for `--config`.

And you can use env variables to choose the “configuration file” an then what the replace for your needs.

Hope this helps you!

---

<div class="post-metadata">

**Author:** ![Jac](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jac](https://community.grafana.com/u/Jac)\
**Post date:** [July 29, 2022, 12:29am UTC](https://community.grafana.com/t/how-to-overwrite-values-from-config-file-dynamically/99495/5 "2022-07-29T00:29:38Z")

</div>

Thanks @mstoykov ! It is helpful.
