This workflow uses AWS Step Functions to build a saga pattern to book flights, book car rentals, and process payments for a vacation. The saga pattern is a failure management pattern that coordinates transactions between multiple microservices to maintain data consistency.
Learn more about this workflow at Step Functions workflows collection: << Add the live URL here >>
For more info about the saga pattern, please read Implement the serverless saga pattern by using AWS Step Functions
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS CDK Installed
-
If this is your first time using AWS CDK, bootstrap your environment.
cdk bootstrap aws://{your-aws-account-number}/{your-aws-region}
-
Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/step-functions-workflows-collection
-
Change directory to the pattern directory:
cd saga-pattern-cdk
-
From the command line, use npm to install dependencies and run the build process for the Lambda functions.
npm install npm run build
-
From the command line, use CDK to deploy the AWS resources for the workflow as specified in the
lib/cdk-serverless-saga-stack.ts
file:cdk deploy
-
During the prompts:
Do you wish to deploy these changes (y/n)? Y
The following workflow diagram illustrates the typical flow of the travel reservation system. The workflow consists of reserving air travel, reserving a car, processing payments, confirming flight reservations, and confirming car rentals followed by a success notification when these steps are complete. However, if the system encounters any errors in running any of these transactions, it starts to fail backward. For example, an error with payment processing triggers a refund, which then triggers a cancellation of the rental car and flight, which ends the entire transaction with a failure message.
For testing purposes, this pattern deploys API Gateway and a test Lambda function that triggers the Step Functions state machine. With Step Functions, you can control the functionality of the travel reservation system by passing a run_type parameter to mimic failures in “ReserveFlight,” “ReserveCarRental,” “ProcessPayment,” “ConfirmFlight,” and “ConfirmCarRental.”
The saga Lambda function (sagaLambda.ts) takes input from the query parameters in the API Gateway URL, creates the following JSON object, and passes it to Step Functions for execution:
```
let input = {
"trip_id": tripID, // value taken from query parameter, default is AWS request ID
"depart_city": "Detroit",
"depart_time": "2021-07-07T06:00:00.000Z",
"arrive_city": "Frankfurt",
"arrive_time": "2021-07-09T08:00:00.000Z",
"rental": "BMW",
"rental_from": "2021-07-09T00:00:00.000Z",
"rental_to": "2021-07-17T00:00:00.000Z",
"run_type": runType // value taken from query parameter, default is "success"
};
```
You can experiment with different flows of the Step Functions state machine by passing the following URL parameters:
Successful Execution ─ https://{api gateway url}
Reserve Flight Fail ─ https://{api gateway url}?runType=failFlightsReservation
Confirm Flight Fail ─ https://{api gateway url}?runType=failFlightsConfirmation
Reserve Car Rental Fail ─ https://{api gateway url}?runType=failCarRentalReservation
Confirm Car Rental Fail ─ https://{api gateway url}?runType=failCarRentalConfirmation
Process Payment Fail ─ https://{api gateway url}?runType=failPayment
Pass a Trip ID ─ https://{api gateway url}?tripID={by default, trip ID will be the AWS request ID}
- Delete the stack
cdk destroy
Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0