You are viewing the docs for v1.3, which is not the latest release.
View latest

Share Data Across Flows with Subflows, KV Store, and Variables

For the complete documentation index, see llms.txt. For a full content snapshot, see llms-full.txt. Append .md to any kestra.io/docs/* URL for plain Markdown.

How to access values across different flows.

Sometimes it's useful to store values so they can be used across multiple flows. Whether that's configuration or state generated by similar flows, accessing values between flows has many benefits. There are multiple ways to do that in Kestra, each with different advantages depending on the use case.

There are three different ways you can access values across different flows:

  1. Subflows
  2. KV Store
  3. Namespace Variables

Subflows

Using Subflows, you can execute one flow from another flow. As part of that, you can pass inputs from the parent flow to the subflow and retrieve outputs generated from it. This is useful if you want multiple flows to execute together and interact directly with one another. However, this doesn't work if you want one flow to generate a value and another flow to use it later when it executes.

In this example, our parent flow is passing the variable debug into the subflow as an input. On top of that, the subflow returns an output subflow_output too.

yamlid: parent_flow
namespace: company.team

variables:
  debug: true

tasks:
  - id: subflow
    type: io.kestra.plugin.core.flow.Subflow
    flowId: subflow
    namespace: company.team
    inputs:
      debug: "{{ vars.debug }}"

  - id: log
    type: io.kestra.plugin.core.log.Log
    message: "{{ outputs.subflow.outputs.subflow_output }}"

In the subflow, the output is explicitly defined to make it accessible in the parent flow. This example uses the input to generate the output sent to the parent flow.

yamlid: subflow
namespace: company.team

inputs:
  - id: debug
    type: BOOLEAN

tasks:
  - id: return
    type: io.kestra.plugin.core.debug.Return
    format: "Subflow: {{ inputs.debug }}"

outputs:
  - id: subflow_output
    type: STRING
    value: "{{ outputs.return.value }}"

KV Store

Using the KV Store, you can set and get values across different flows. This is good if you want to be able to store values without flows directly interacting with one another, like they do with Subflows. Flows can use the Get and Set tasks to make themselves stateful, allowing one flow to store the state, and another to access it when it wants. However, this approach isn't ideal if you don't want these values to be modified by the flows directly.

For example, you can use io.kestra.plugin.core.kv.Set task as well as use the UI interface to manage the values in the KV Store. To access them, you can use the io.kestra.plugin.core.kv.Get task which will return them as an output.

yamlid: kv_store
namespace: company.team

variables:
  debug: true

tasks:
  - id: set
    type: io.kestra.plugin.core.kv.Set
    key: debug
    value: "{{ vars.debug }}"
    namespace: "{{ flow.namespace }}"

  - id: get
    type: io.kestra.plugin.core.kv.Get
    key: debug

  - id: log
    type: io.kestra.plugin.core.log.Log
    message: "{{ outputs.get.value }}"

Namespace Variables

Using Namespace Variables, you can define values that can be accessed between flows in a namespace, similar to the KV Store. However, these can only be set in the Namespace page. This is useful if you want to access values across flows but do not want to update them dynamically inside your flows at the same time.

For example, we can define our variables as a key-value pair in our Namespace:

yamldebug: true
state: failed
hello: world

We can access them using the {{ namespace.var_key }} expression where var_key is the key of our key-value pair.

yamlid: global_variables
namespace: company.team

variables:
  debug: true

tasks:
  - id: debug_1
    type: io.kestra.plugin.core.log.Log
    message: "Namespace: {{ namespace.state }}"

  - id: debug_2
    type: io.kestra.plugin.core.log.Log
    message: "Local: {{ vars.debug }}"