After this section, you will have a workflow that waits for the customer to provide details of their coffee order and then waits for the barista to make an order.
In this section, you add an EventBridge PutEvents state that emits an event when the callback is complete. The workflow waits here until the callback is received. This happens when the customer submits their drink order.
Go to the Step Functions console. From the AWS Management Console, select Services then select Step Functions under Application Integration. Make sure your region is correct.
From the left-hand menu, select State machine and choose OrderProcessorWorkflow from the list. Choose Edit.
On the next page, choose Workflow Studio to open the workflow in the designer.
With the Actions tab selected on the left, enter putevents
in the search bar. Drag the Amazon EventBridge PutEvents action from the list to between the Is capacity available? and Generate Order Number states in the designer.
Emit - Workflow Started TT
.{
"Entries": [
{
"Detail": {
"Message": "The workflow waits for your order to be submitted. It emits an event with a unique 'task token'. The token is stored in an Amazon DynamoDB table, along with your order ID.",
"TaskToken.$": "$$.Task.Token",
"orderId.$": "$.detail.orderId",
"userId.$": "$.detail.userId"
},
"DetailType": "OrderProcessor.WorkflowStarted",
"EventBusName": "Serverlesspresso",
"Source": "awsserverlessda.serverlesspresso"
}
]
}
Customer timed out
.900
seconds. This means that the workflow will time out if the callback is not received within 15 minutes.Customer timedout
."Customer timedout"
(including the quotes).$.cause
.In this section, you add an EventBridge PutEvents state that emits an event when the callback is complete. The workflow waits here until the callback is received. This happens when the barista makes a drink order.
putevents
in the search bar. Drag the Amazon EventBridge PutEvents action from the list to between the Generate Order Number and Pass states in the designer.{
"Entries": [
{
"Detail": {
"Message": "You pressed 'submit order'. The workflow resumes using the stored 'task token', it generates your order number. It then pauses again, emitting an event with a new 'task token'.",
"TaskToken.$": "$$.Task.Token",
"orderId.$": "$.detail.orderId",
"orderNumber.$": "$.Order.Payload.orderNumber",
"userId.$": "$.detail.userId"
},
"DetailType": "OrderProcessor.WaitingCompletion",
"EventBusName": "Serverlesspresso",
"Source": "awsserverlessda.serverlesspresso"
}
]
}
$.order
.Barista timed out
.$.comment
.On the Error handling tab, for Heartbeat, enter 900
seconds. This means that the workflow will time out if the callback is not received within 15 minutes.
With the Flow tab selected on the left, drag the Pass state action from the list to the empty Drop state here placeholder in the designer.
Barista timedout
."Barista timedout"
(including the quotes).$.cause
.Choose Apply and exit. In the Edit page, choose Save.
Choose Save anyway in the IAM popup.
In this section, you will test the changes to the workflow.
From the previous section, on the page showing the new workflow, choose Start execution.
In the Start execution pop-up, enter the following JSON payload:
{
"detail": {
"orderId": "1",
"userId": "testuser"
}
}
Choose Start execution
The console shows the Execution status of Running. The left side shows the flow of execution with the green states showing the actual path. The blue state shows when execution is suspended, pending a callback.
You will use a service called AWS CloudShell a browser-based shell terminal that makes it easy to securely manage, explore, and interact with your AWS resources to run the API command.
YOUR_TASK_TOKEN
with the token value:aws stepfunctions send-task-success --task-output '{"orderId":1}' --task-token YOUR_TASK_TOKEN
Execution continues to the next state waiting for a callback.
In the Execution event history panel, open the TaskScheduled event for Emit - Awaiting completion TT. This displays the payload for this event. Copy the TaskToken value to a scratch pad.
Use the SendTaskSuccess API callback to the workflow with the task token and continue execution. In the CloudShell terminal, enter the following command, replacing YOUR_TASK_TOKEN
with the token value:
aws stepfunctions send-task-success --task-output '{"orderId":1}' --task-token YOUR_TASK_TOKEN
The console shows that the execution is now finished for the workflow.
Next, you’ll modify the workflow to emit events when important things happen.