Giter Club home page Giter Club logo

aws-step-functions-waitable-pattern's Introduction

AWS Step Functions Waitable Pattern

Open in Visual Studio Code

This projects contains source code with an example of how to implement an AWS Step Function waitable pattern that you can deploy using the SAM CLI. It includes the following files and folders:

  • statemachine - Definition for the state machine that orchestrates the change between status after the defined time is reached.
  • template.yaml - A template that defines the application's AWS resources.
  • waitable-stepfunction.asl.json - The file that contains the Step Function Workflow.

This sample demonstrates the power of AWS Step Functions when orchestrating time sensitive applications (e.g. send email notification to user after 2h) while keeping costs under control since you don't pay for the time that AWS Step Functions is on a wait state.

To make it reusable in real use cases, we use Amazon API Gateway as a way to trigger and send input parameters to the State Machine, and AWS DynamoDB as the persistent store for each item Status. The final workflow looks like this:

stepfunctions_graph

Deploy the sample application

The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda.

To use the SAM CLI, you need the following tools:

To build and deploy your application for the first time, run the following in your shell:

sam build
sam deploy --guided

Try on AWS console

  1. Sign in to your AWS console at https://console.aws.amazon.com

  2. Go to the Amazon API Gateway service

  3. Select your new API (e.g. aws-stepfunction-waitable-pattern)

  4. Select the POST method on the resources tab

  5. Select the Test button on the right panel

  6. Specify a valid json payload as the request body, like so:

    {
      "StartTimestamp": "2021-06-25T17:47:00Z"
    }
  7. Press the Test button. You should get a valid response.

    Job Id fe520f82-8f3e-4058-bb2d-e90f271fc3d6 started successfully. For additional information on process status use GET /status.
  8. Select the GET method under the /status resource

  9. Select the Test button on the right panel and press the Test button. You should get a valid response.

    {
    "items": [
        {
          "Id": "4cb0702d-1b62-4b09-8482-fc1182ca4240",
          "StartActionAt": "2021-07-09T13:50:00Z",
          "LastUpdated": "2021-07-09T13:35:43.151Z",
          "ProcessStatus": "STARTED"
        },
        {
          "Id": "fe520f82-8f3e-4058-bb2d-e90f271fc3d6",
          "StartActionAt": "2021-07-09T13:46:00Z",
          "LastUpdated": "2021-07-09T13:45:59.462Z",
          "ProcessStatus": "COMPLETED"
        },
        {
          "Id": "46a332a6-18d5-4c17-89d5-db1eeb78baec",
          "StartActionAt": "2021-07-09T13:40:00Z",
          "LastUpdated": "2021-07-09T13:39:59.653Z",
          "ProcessStatus": "COMPLETED"
        }
      ]
    }

Using cURL

  1. Open your prefered Terminal

  2. Make sure you have cURL installed

    MacOS

    brew install curl

    Linux

    sudo apt-get install curl

    Windows - with chocolatey

    choco install curl
  3. Send a request to API Gateway

    curl --location --request POST 'REPLACE_WITH_YOUR_API_GATEWAY_ENDPOINT' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "StartTimestamp": "2021-06-25T17:47:00Z"
    }'

    You should get a valid response.

    Job Id fe520f82-8f3e-4058-bb2d-e90f271fc3d6 started successfully. For additional information on process status use GET /status.
  4. Check process status

    curl --location --request GET 'REPLACE_WITH_YOUR_API_GATEWAY_ENDPOINT'/status

    You should get a valid response.

    {
    "items": [
        {
          "Id": "4cb0702d-1b62-4b09-8482-fc1182ca4240",
          "StartActionAt": "2021-07-09T13:50:00Z",
          "LastUpdated": "2021-07-09T13:35:43.151Z",
          "ProcessStatus": "STARTED"
        },
        {
          "Id": "fe520f82-8f3e-4058-bb2d-e90f271fc3d6",
          "StartActionAt": "2021-07-09T13:46:00Z",
          "LastUpdated": "2021-07-09T13:45:59.462Z",
          "ProcessStatus": "COMPLETED"
        },
        {
          "Id": "46a332a6-18d5-4c17-89d5-db1eeb78baec",
          "StartActionAt": "2021-07-09T13:40:00Z",
          "LastUpdated": "2021-07-09T13:39:59.653Z",
          "ProcessStatus": "COMPLETED"
        }
      ]
    }

Cleanup

To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:

aws cloudformation delete-stack --stack-name STACK_NAME

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

aws-step-functions-waitable-pattern's People

Contributors

amazon-auto avatar t1agob avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.