Create a State Machine with a Callback Pattern
Learn about the callback pattern in AWS Step Functions.
We'll cover the following
In this lesson, we will focus on creating a state machine with the callback pattern.
The callback pattern
The callback pattern in AWS Step Functions is like a delivery service that requires signature confirmation. When a delivery arrives, the recipient is notified and must provide their signature to confirm receipt. Similarly, in Step Functions, the callback pattern is a way for a state to wait for an external process to complete and provide a token to confirm that it has finished.
Let's break it down using an analogy and then provide an advanced example.
Analogy: Imagine we are running an art event, and we have several tasks to complete before the event can begin:
Set up the venue.
Arrange catering.
Hire a photographer.
Send out invitations.
For task three (hiring a photographer), we need to wait for a friend's recommendation before we can proceed. So, we ask our friend to call us back when they have the information. While we await our friend's recommendation, we don't remain idle; we proceed with other tasks.
In this analogy, the callback pattern in AWS Step Functions is like waiting for our friend's call. We don't know exactly when the call will come, but we know that when it does, we'll have the information we need to move forward with hiring a photographer.
The waitForTaskToken
field
In AWS Step Functions workflows, we implement the callback pattern using the waitForTaskToken
field. This is a special feature that allows a state machine to halt its execution temporarily, pending the completion of a task by an external service or through manual intervention. While it might seem that the execution pauses, this pattern actually enables the workflow to continue with other tasks in the meantime, efficiently handling the wait time. Once the task is completed, the external service or user sends back a task token to the state machine, which then continues with its next steps. It is generated by the Step Functions service. The waitForTaskToken
feature is used with AWS service integrations like SQS, SNS, Lambda, or any custom integration that can receive and process the Task Token. To use waitForTaskToken
, we need to include a special parameter called TaskToken
in our state definition. This parameter is passed to the external service or user, who then needs to return it to the state machine when the task is done.
Get hands-on with 1200+ tech skills courses.