This is a sample template for Managing Long Lived Transactions with AWS Step Functions. Below is a brief explanation of what we have created for you:
.
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── docs <-- # Workshop guide and setup instructions
├── inventory-release <-- # Lambda function code represents compensating transaction to release inventory
│ ├── ...
│ └── main.go
├── inventory-reserve <-- # Lambda function code represents compensating transaction to release inventory
│ ├── ...
│ └── main.go
├── models <-- # Models package that defines the types used by the various functions and state data
│ ├── ...
│ ├── inventory.go
│ ├── order.go
│ └── payment.go
├── order-new <-- # Lambda function code represents task to create a new order and set status to "new order"
│ ├── ...
│ └── main.go
├── order-update <-- # Lambda function code represents compensating transaction for orders.
│ ├── ... # Sets order status to "pending"
│ └── main.go
├── payment-credit <-- # Lambda function code represents the compensating transaction to refund customer order
│ ├── ...
│ └── main.go
├── payment-debit <-- # Lambda function code represents task to process financial transaction for the order
│ ├── ...
│ └── main.go
├── statemachine
│ └── llt.asl.yaml <-- # Step Functions ASL template
├── template.yaml <-- # AWS SAM template for defining and deploying serverless application resources
└── ...
A full description of the how to describe your state machine can be found on the Amazon States Language specification.
Please review the "Templates" section in the AWS Console for examples of how you can implement various states.
The Task State (identified by "Type":"Task") causes the interpreter to execute the work identified by the state's “Resource” field.
"ProcessOrder": {
"Comment": "First transaction to save the order and set the order status to new",
"Type": "Task",
"Resource": "arn:aws:lambda:[REGION]:[ACCOUNT NUMBER]:function:aws-step-functions-long-lived-tra-NewOrderFunction-121DONKVIBL5T",
"TimeoutSeconds": 10,
"Next": "ProcessPayment"
}
Any state can encounter runtime errors. Errors can arise because of state machine definition issues (e.g. the “ResultPath” problem discussed immediately above), task failures (e.g. an exception thrown by a Lambda function) or because of transient issues, such as network partition events.
"Catch": [
{
"ErrorEquals": ["ErrProcessOrder"],
"ResultPath": "$.error",
"Next": "UpdateOrderStatus"
}
]
Task States and Parallel States MAY have a field named “Retry”, whose value MUST be an array of objects, called Retriers.
When a state reports an error, the interpreter scans through the Retriers and, when the Error Name appears in the value of of a Retrier’s “ErrorEquals” field, implements the retry policy described in that Retrier.
"Retry": [{
"ErrorEquals": ["States.ALL"],
"IntervalSeconds": 1,
"MaxAttempts": 2,
"BackoffRate": 2.0
}],
"Catch": [{
"ErrorEquals": ["ErrReleaseInventory"],
"ResultPath": "$.error",
"Next": "ReleaseInventoryFailed"
}
]
The following is a list of all the custom errors thrown by the application and can be used in your state machine.
ErrProcessOrder
represents a process order errorErrUpdateOrderStatus
represents a process order errorErrProcessPayment
represents a process payment errorErrProcessRefund
represents a process payment refund errorErrReserveInventory
represents a inventory update errorErrReleaseInventory
represents a inventory update reversal error
The AWS Step Functions implementation has been configured for you to be easily test the various scenarios of the saga implementation. Modifying your order_id
with a specified prefix will trigger an error in the each Task.
The AWS CLI command will trigger a execution of your state machine. Make sure you substitute the ARN for the state machine in your account. You can find the ARN in the AWS CloudFormation Output section or in the AWS Step Functions console.
--region
must match the region you have deployed the application stack into. This is optional if you're using your default region.
aws stepfunctions start-execution \
--state-machine-arn "arn:aws:states:[REGION]:[ACCOUNT NUMBER]:stateMachine:[STATEMACHINE-NAME]" \
--input "{\"order_id\": \"40063fe3-56d9-4c51-b91f-71929834ce03\", \"order_date\": \"2018-10-19T10:50:16+08:00\", \"customer_id\": \"8d04ea6f-c6b2-4422-8550-839a16f01feb\", \"items\": [{ \"item_id\": \"567\", \"qty\": 1.0, \"description\": \"Cart item 1\", \"unit_price\": 199.99 }]}" \
--region [AWS_REGION]
DOWNLOAD SCENARIO CLI COMMANDS
Is there any other way you can think of how to break this problem down? What other features of Step Functions could be employed to implement a saga pattern?