Skip to content

Latest commit

 

History

History
87 lines (69 loc) · 3.54 KB

File metadata and controls

87 lines (69 loc) · 3.54 KB

Step Function with Lambda

step-functions-lambda

Invoking the first Lambda function with an AWS API Gateway. Once the first Lambda function receives the incoming request from the API Gateway, it will validate the request and start the Step Function State Machine. The AWS Step Function State Machine will trigger the second Lambda Function if the transaction_type is either PURCHASE or REJECTED.

The second Lambda Function will receive the event as JSON text as the input and will process the received event (json.RawMessage). It is because the individual states receive JSON as input and usually pass JSON as output to the next state.

State Machine Definition

{
  "StartAt": "processTransactionTask",
  "States": {
    "processTransactionTask": {
      "End": true,
      "Type": "Task",
      "InputPath": "$",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "arn:aws:lambda:region:account_id:function:processTransaction",
        "Payload.$": "$"
      }
    }
  }
}

API Specification

When the API received an incoming request, it will validate if the required fields are present, start the Step Function State Machine, and will return an HTTP Status OK.

Method: POST

Endpoint: https://{api-id}.execute.api.{region}.amazonaws.com/prod/

Request Body

{
  "customerId": "84378220428",
  "card_number": "4444 4444 8888 8888",
  "transaction_type": "PURCHASE"
}

AWS CDK API / Developer Reference

AWS SDK v2 API / Developer Reference

AWS Documentation Developer Guide

Useful commands

  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template

Deploy

Using make command

  1. Install all the dependencies, bootstrap your project, and synthesized CloudFormation template.

    # Without passing "profile" parameter
    dev@dev:~:aws-cdk-samples/step-functions/step-functions-lambda$ make init
    
    # With "profile" parameter
    dev@dev:~:aws-cdk-samples/step-functions/step-functions-lambda$ make init profile=[profile_name]
  2. Deploy the project.

    # Without passing "profile" parameter
    dev@dev:~:aws-cdk-samples/step-functions/step-functions-lambda$ make deploy
    
    # With "profile" parameter
    dev@dev:~:aws-cdk-samples/step-functions/step-functions-lambda$ make deploy profile=[profile_name]