Giter Site home page Giter Site logo

eventbridge-stepfunction-eventbridge-sqs-cdk's Introduction

Amazon EventBridge -> AWS Step Functions -> SQS

This pattern creates an EventBridge event bus, a Step Functions workflow, and subscribes the Step Functions workflow to be executed when a customEvent event is published to the event bus. The Step Functions workflow executes Lambda function for business worflow. AWS Lambda can occasionally experience transient service errors. In this case, invoking Lambda results in a 500 error, such as ServiceException, AWSLambdaException, or SdkClientException. As a best practice, proactively handle these exceptions in your state machine to Retry invoking your Lambda function, or to Catch the error and move to Failed Queue for operational needs.

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.

Architecture

Architecture diagram

StateMachine Flow

StateMachine Flow

Requirements

Deployment Instructions

  1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:

    git clone https://github.com/aws-samples/serverless-patterns
    
  2. Change directory to the pattern directory:

    cd cdk-eventbridge-stepfunction-sqs
    
  3. Inside the cdk-eventbridge-stepfunction-sqs directory run npm install to fetch the cdk project's dependencies

  4. Run cdk bootstrap

  5. From the command line, use CDK to deploy the AWS resources for the pattern. You'll be prompted to approve security related changes during the deployment.

    cdk deploy
    
  6. This command will take sometime to run. After successfully completing, it will print out a few output variables. Your output should look something like this which contain the resource names which are used for testing.

Outputs:
EventBridgeCDKStateMachineStack.EventCustomBus = EventBridgeCDKStateMachineStackcustomEventBusXXXXXXX
EventBridgeCDKStateMachineStack.StepFunction = EventBridgeCDKStateMachine
EventBridgeCDKStateMachineStack.StepfunctionFailureEventsQueue = EventBridgeCDKStateMachineStack-StepfuntionFailureQueue81C6B762-XXXXXXX
EventBridgeCDKStateMachineStack.EventBridgeFailureDLQ = EventBridgeCDKStateMachineStack-aysncdlqQueueA1EEE5D6-XXXXXXX

How it works

  • Let's create two custom events through AWS Console one for failure and another one for successfull execution. Successfull event invokes Step function and executes lambda function successfully and completes the step function state machine successfully Failure event invokes Step function and executes lambda function which throws custom exception and get retried 2 times using Step function error hanlder. After retry is exhausted, Catch handler moves the state machine Failure SQS. Stepfunction writes Failure message to the SQS using "SendMessage" task which can be handled for operational needs.

Testing

Send an event to the event bus to trigger the Step Functions workflow

Let's create two custom events for success and failure execution, To do this, log into the AWS Console at https://console.aws.amazon.com you can send a two custom messages separately to the event bus using the AWS Console.

  1. Go to the Amazon EventBridge service page. View a list of event buses by selecting the "Event buses" link on the left navigation menu.(e.g. EventBridgeCDKStateMachineStackcustomEventBusXXXXXX)

list of event buses

1.a Publish "success" event Click on the "Send Events" button at the top right. This brings up a screen that you can use to publish events to the EventBridge. publish success event create screen * Event bus: Select the CustomBus (EventBridgeCDKStateMachineStackcustomEventBusXXXXXX). * Event source: Enter CreateEvent - This is required field * Detail type: Enter CREATE - This is required field * Event detail: In the event detail, you should craft a JSON payload that sets the failure field to be false.

{
  "failure": false
}

1.b Click the "Send" button and the confirmation will be displayed:

publish success event

2.a Publish "failure" event Click on the "Send Events" button at the top right. This brings up a screen that you can use to publish events to the EventBridge. publish failure event create screen * Event bus: Select the CustomBus (EventBridgeCDKStateMachineStackcustomEventBusXXXXXX). * Event source: Enter CreateEvent - This is required field * Detail type: Enter CREATE - This is required field * Event detail: In the event detail, you should craft a JSON payload that sets the failure field to be true.

{
  "failure": true
}

2.b Click the "Send" button and the confirmation will be displayed: publish failure event

Check that the Step Functions workflow started

In the AWS Console, navigate to the Step Functions service. You should see your new state machine in the list.

View state machines

Click on your state machine, and you should see a list of executions. Click on the latest to view the result of the execution and you will notice two recent executions.

View Step function executions

Click the " Succeeded" status execution to see the state machine, and details.

View succeeded execution detail

Click the "Failed" status execution to see the state machine, and details. After the execution the failed event is send to Failed SQS queue.

View failed execution detail

Check that the Failure SQS recieved the failed messages

In the AWS Console, navigate to the SQS service. You should see your new failure sqs in the list.

View SQS HomePage

Click the "EventBridgeCDKStateMachineStack-StepfuntionFailureQueue81XXXXX", and you should see the "Send and Receive messages" on the top right side. Click the "Send and Receive messages" to navigate to "Poll message" screen. View SQS HomePage View SQS Send & Recieve Message

Click the "Poll for messages", and you should see the failure messages displayed on the bottom.

view SQS Poll Message

Making changes

You can customize the Step Functions configuration by editing the code at ./lib/eventbridge-stepfunction-sqs-stack.ts. To deploy changes, use the cdk deploy command.

Cleanup

  1. Delete the stack

    cdk destroy
    
  2. Confirm the stack has been deleted. Login to the AWS console and navigate to the AWS Cloudformation service page "EventBridgeCDKStateMachineStack" is deleted or run the below

    aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'EventBridgeCDKStateMachineStack')].StackStatus"

You should expect to see a message confirming DELETE_COMPLETE.


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

SPDX-License-Identifier: MIT-0

eventbridge-stepfunction-eventbridge-sqs-cdk's People

Contributors

shansv avatar

Watchers

 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.