While
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. Serverless interfaces and procedures might differ from classic Elastic Stack deployments. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability This functionality is generally available and ready for production usage. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless
Generally available
Stack
9.4+
Generally available since 9.4
The while step runs its nested steps repeatedly as long as a Kibana Query Language (KQL) condition evaluates to true. The condition is re-evaluated at the end of every iteration.
Use while for polling patterns: checking a status until it reaches ready, retrying an operation until it succeeds, or waiting for an external job to complete. For iterating over a known collection, use foreach instead.
Schema convention. In each schema table on this page, the Location column indicates where each parameter sits in the YAML:
top level: Alongsidetypeandname, at the top of the step or trigger definition.`with`: Inside the step'swith:block.`on`: Inside the trigger'son:block.
| Parameter | Location | Type | Required | Description |
|---|---|---|---|---|
name |
top level | string | Yes | Unique step identifier. |
type |
top level | string | Yes | Must be while. |
condition |
top level | string | Yes | KQL expression evaluated each iteration. The loop continues while it is true. |
steps |
top level | array | Yes | Loop body. |
if |
top level | string | No | KQL expression that skips the entire loop when it evaluates to false. |
max-iterations |
top level | number or object | No | Limit for number of iterations. Default is 2000. Bare number is treated as { limit: N, on-limit: continue }. Use the object form to opt into on-limit: fail. |
timeout |
top level | duration | No | Timeout for the entire loop. |
on-failure |
top level | object | No | Loop-level error-handling policy. Supports the same continue, retry, and fallback options as other steps. |
iteration-timeout |
top level | duration | No | Per-iteration timeout. |
iteration-on-failure |
top level | object | No | Per-iteration error-handling policy. Supports continue, retry, and fallback without failing the whole loop. |
while defaults to max-iterations: 2000 with on-limit: continue, which means the step succeeds quietly when the cap is reached. If your loop needs to fail the workflow on hitting the cap, set on-limit: fail explicitly. Always think about the cap on loops that depend on external state to avoid runaway executions or silently truncated work.
Use loop-level guardrails to control the while step as a whole:
max-iterationscaps the number of iterations to prevent runaway loops.timeoutlimits the total time spent in the loop, across all iterations.on-failuredefines loop-level failure handling with the samecontinue,retry, andfallbackoptions used by other steps.ifskips the entire loop when the condition evaluates to false.
Use iteration-level guardrails to control each pass through the loop:
iteration-timeoutlimits how long one iteration can run.iteration-on-failurehandles failures for one iteration withcontinue,retry, orfallbackwithout failing the whole loop.
# Bare number: default `on-limit` is `continue` (the step succeeds when the limit is reached)
max-iterations: 60
# Object form: opt into `on-limit: fail` to fail the workflow when the limit is reached
max-iterations:
limit: 60
on-limit: fail
Inside the steps block of a while, the following variables are available:
| Variable | Contains |
|---|---|
while.iteration |
Zero-based iteration counter. |
- name: poll_job
type: while
if: "inputs.poll : true"
condition: "steps.check.output.status : pending"
max-iterations:
limit: 60
on-limit: fail
timeout: "10m"
iteration-timeout: "30s"
iteration-on-failure:
retry:
max-attempts: 2
delay: "1s"
steps:
- name: check
type: elasticsearch.search
with:
index: "jobs"
query:
term:
id: "{{ inputs.job_id }}"
size: 1
- name: log_progress
type: console
with:
message: "Attempt {{ while.iteration }}: status {{ steps.check.output.hits.hits[0]._source.status }}"
- name: wait
type: wait
with:
duration: "5s"
If the job hasn't left pending after 60 iterations (five minutes), the loop exits with failure.
- Flow control steps: Overview of all flow-control types.
- Foreach step: For iterating over a known array.
- Loop break and Loop continue: Control loop flow from inside the body.