AWS Step Functions: Using the Wait State Type
Tags: programming, aws, tutorial, productivity
Published: 2023-12-01 04:58:13

Before checking out the post, I recommend first looking at: [Introduction to AWS Step Functions using Terraform as an infrastructure as code tool.](https://dev.to/aws-builders/introduction-to-aws-step-functions-using-terraform-as-infrastructure-as-code-tool-33il)

---


The "Wait" state in AWS Step Functions is a powerful and flexible tool for managing workflows in the cloud. This state is essentially a pause mechanism, allowing workflows to wait for a specified period of time or until a specific event occurs before proceeding to the next step. It is crucial in scenarios where timing and temporal coordination are important.

## Operation of the "Wait" State:

**Fixed Wait**: You can configure the "Wait" state so that your workflow pauses for a specific amount of time, expressed in seconds or until an exact hour and date. This option is useful for predictable delays, such as waiting between attempts of an operation or to allow time for external processes to complete.

### Example:

```
"wait_ten_seconds" : {
  "Type" : "Wait",
  "Seconds" : 10,
  "Next": "NextState"
}
```

**Dynamic Wait:** It is also possible to configure the "Wait" state to wait until a certain event occurs, such as the arrival of a message to an SQS queue or the update of a specific resource. In this mode, the workflow resumes immediately after the condition is met, making it ideal for unpredictable events.

### Example:

```
"wait_until" : {
  "Type": "Wait",
  "TimestampPath": "$.expirydate",
  "Next": "NextState"
}
```

As we can see in the previous example, the waiting time is determined by an input variable `$.expirydate`, which should come from the previous state.

## Common Use Cases for the Use of the Wait State:

**Approval Processes:** In workflows that require human approvals, the "Wait" state can pause the process until a response is received.

**Coordination of Asynchronous Tasks:** In scenarios where multiple tasks are executed in parallel and one of them must wait for the others to complete.

**Scheduled Delays:** Useful in cases where a delay between steps is needed, such as in batch processing or in the staggered sending of notifications.

## Example:

We have a workflow that is divided into 3 stages or actions:

**Create: **creation of the process. The process will have an ID and a state, and the state must advance to the effective state before an action of completion or reversal can be taken. It is important to clarify that the process's effective state does not occur immediately; it can take from 10 to 20 seconds.

**Execute Action:** At this point, the process must be in an effective state to execute the action. The allowed actions are finish or rollback, depending on the case. After this, the state of the process will be finishing or rollbacking.

**End:** this stage will validate that the process has successfully finished or has been successfully reverted.

![AWS Step function definition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivaw3sr7avmdvvnheaz3.png)

## Example states:

**Create**: This state calls a lambda function that will initialize the process.

```
"Create": {
      "Type": "Task",
      "Resource": "${aws_lambda_function.number_validator_lambda.arn}",
      "Parameters": {
        "input.$": "$",
        "action": "create"
      },
      "Next": "WaitForExecuteAction"
}
```

**WaitForExecuteAction:** After the process is initiated, a waiting period is required before validating the current state and executing the action (finish / rollback).

```
"WaitForExecuteAction": {
      "Type": "Wait",
      "Seconds": 10,
      "Next": "ExecuteAction"
}
```

**ExecuteAction**: After the waiting period, a lambda function must be executed that validates the current state of the process. If it is effective, the action should be executed; otherwise, the flow should fail.

```
"ExecuteAction": {
      "Type": "Task",
      "Resource": "${aws_lambda_function.number_validator_lambda.arn}",
      "Parameters": {
        "number.$": "$.number",
        "update.$": "$.update",
        "status.$": "$.status",
        "action": "execute_action"
      },
      "Next": "WaitForEnd"
}
```

**WaitForEnd**: After this, an additional waiting period is required for the process to reach a finished or rollbacked state.
```
"WaitForEnd": {
      "Type": "Wait",
      "Seconds": 10,
      "Next": "EndDeploy"
}
```

EndDeploy: Finally, it must be validated that the process has indeed finished; if not, the flow should fail.

```
"EndDeploy": {
      "Type": "Task",
      "Resource": "${aws_lambda_function.number_validator_lambda.arn}",
      "Parameters": {
        "number.$": "$.number",
        "update.$": "$.update",
        "status.$": "$.status",
        "action": "end"
      },
      "Next": "Ended"
}
```

**Ended**: Final step of the workflow.

```
"Ended": {
      "Type": "Succeed"
}
```

In summary, the "Wait" state in AWS Step Functions is a powerful tool for efficient workflow management. Its ability to handle both fixed and event-based pauses allows developers to build smarter and more responsive applications that can effectively react to changing conditions and business needs.

Complete example: https://github.com/jjoc007/poc_step_function_validator/tree/main
If you like it, give a 👍 to the post and a ⭐ to the repo.

## References:

- [introduction to aws step functions](https://dev.to/aws-builders/introduction-to-aws-step-functions-using-terraform-as-infrastructure-as-code-tool-33il) 
- [introduction to aws step functions Spanish version ](https://jjoc007.com/introducción-a-aws-step-functions-usando-terraform-como-herramienta-de-infrastructura-como-código-e2add2930269) 
- [Repository](https://github.com/jjoc007/poc_step_function_validator/tree/main) 
- [Aws documentation](https://states-language.net/spec.html#wait-state)