Skip to content

Latest commit

 

History

History
 
 

saga-pattern-cdk

Saga Pattern

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.

Requirements

Deployment Instructions

  1. If this is your first time using AWS CDK, bootstrap your environment.

    cdk bootstrap aws://{your-aws-account-number}/{your-aws-region}
    
  2. 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
    
  3. Change directory to the pattern directory:

    cd saga-pattern-cdk
    
  4. From the command line, use npm to install dependencies and run the build process for the Lambda functions.

    npm install
    npm run build
    
  5. 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
    
  6. During the prompts:

    • Do you wish to deploy these changes (y/n)? Y

How it works

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.

Image

image

Testing

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}

Cleanup

  1. Delete the stack
    cdk destroy

Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0