After this section, you will have a workflow that assigns an order number to the order.
In this section, you use a DynamoDB integration in Step Functions to increment an atomic counter and use the value as the order number.
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 updateitem
in the search bar. Drag the Amazon DynamoDB UpdateItem action from the list to between the Is capacity available? and Pass states in the designer.
Generate Order Number
.{
"TableName": "serverlesspresso-counting-table",
"Key": {
"PK": {
"S": "orderID"
}
},
"UpdateExpression": "set IDvalue = IDvalue + :val",
"ExpressionAttributeValues": {
":val": {
"N": "1"
}
},
"ReturnValues": "UPDATED_NEW"
}
{
"orderNumber.$": "$.Attributes.IDvalue.N"
}
$.Order.Payload
.{
"Comment": "A description of my state machine",
"StartAt": "DynamoDB Get Shop status",
"States": {
"DynamoDB Get Shop status": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:getItem",
"Parameters": {
"TableName": "serverlesspresso-config-table",
"Key": {
"PK": {
"S": "config"
}
}
},
"ResultPath": "$.GetStore",
"Next": "Shop Open?"
},
"Shop Open?": {
"Type": "Choice",
"Choices": [
{
"Not": {
"Variable": "$.GetStore.Item.storeOpen.BOOL",
"BooleanEquals": true
},
"Next": "EventBridge PutEvents"
}
],
"Default": "ListExecutions"
},
"ListExecutions": {
"Type": "Task",
"Next": "Is capacity available?",
"Parameters": {
"StateMachineArn": "YOUR_STATE_MACHINE_ARN",
"MaxResults": 100,
"StatusFilter": "RUNNING"
},
"Resource": "arn:aws:states:::aws-sdk:sfn:listExecutions",
"ResultPath": "$.isCapacityAvailable"
},
"Is capacity available?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.isCapacityAvailable[20]",
"IsPresent": true,
"Next": "EventBridge PutEvents"
}
],
"Default": "Generate Order Number"
},
"Generate Order Number": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:updateItem",
"Parameters": {
"TableName": "serverlesspresso-counting-table",
"Key": {
"PK": {
"S": "orderID"
}
},
"UpdateExpression": "set IDvalue = IDvalue + :val",
"ExpressionAttributeValues": {
":val": {
"N": "1"
}
},
"ReturnValues": "UPDATED_NEW"
},
"Next": "Pass",
"ResultPath": "$.Order.Payload",
"ResultSelector": {
"orderNumber.$": "$.Attributes.IDvalue.N"
}
},
"EventBridge PutEvents": {
"Type": "Task",
"Resource": "arn:aws:states:::events:putEvents.waitForTaskToken",
"Parameters": {
"Entries": [
{
"Detail": {
"Message": "Hello from Step Functions!",
"TaskToken.$": "$$.Task.Token"
},
"DetailType": "MyDetailType",
"EventBusName": "MyEventBusName",
"Source": "MySource"
}
]
},
"End": true
},
"Pass": {
"Type": "Pass",
"End": true
}
}
}
Choose Apply and exit. In the Edit page, choose Save.
In the Edit OrderProcessorWorkflow 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, choose Start execution.
After the execution is finished, the console shows a results page. The left side shows the flow of execution with the green states showing the actual path. Choose the Generate Order Number state to show the details on the right side.
Choose the Step output on the right side to see the output path for the choice state. The JSON output shows an Order attribute with a Payload containing an orderNumber of 1.
Choose Start execution again and repeat steps 2 and 3. The orderNumber is now 2. Each time you run start another execution, the order number is incremented.
Next, you’ll add a wait condition to the workflow, to wait for the customer to submit their order.