Skip to content

Latest commit

 

History

History
116 lines (103 loc) · 4.58 KB

File metadata and controls

116 lines (103 loc) · 4.58 KB

Step Functions with SQS

step-functions-sqs

Starting the execution of the Step Function State Machine via AWS Console to call the SQS SendMessage API from a Task state. The State Machine accepts values in a JSON format (please see sample payload) and will execute the SQS SendMessage API to send a message to the configured SQS Queue, and the Lambda Function will process the received event from the Amazon SQS event message.

State Machine Definition

{
  "StartAt": "transaction-definition",
  "States": {
    "transaction-definition": {
      "End": true,
      "Type": "Task",
      "Resource": "arn:aws:states:::sqs:sendMessage",
      "Parameters": {
        "QueueUrl": "https://sqs.us-east-1.amazonaws.com/123456789101/transaction-queue.fifo",
        "MessageBody": {
          "id.$": "$.id",
          "order_line_id.$": "$.line_id",
          "total.$": "$.total",
          "change.$": "$.change",
          "customer.$": "$.customer"
        },
        "MessageGroupId": "process.transaction"
      }
    }
  }
}

Sample Payload

{
	"id": "transaction-12345",
	"line_id": "line-12345",
	"total": 250.50,
	"change": 249.50,
	"customer": {
		"id": "customer-12345",
		"name": "John Doe"
	}
}

Sample CloudWatch Log

{
  "log_code": "SQSMessage",
  "log_msg": "recieved event from Amazon SQS",
  "log_level": "INFO",
  "log_keys": {
    "transaction": {
      "id": "transaction-12345",
      "order_line_id": "line-12345",
      "customer": {
        "id": "customer-12345",
        "name": "John Doe"
      },
      "total": 250.5,
      "change": 249.5
    }
  },
  "log_timestamp": "2023-05-17 05:51:23"
}

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-sqs$ make init
    
    # With "profile" parameter
    dev@dev:~:aws-cdk-samples/step-functions/step-functions-sqs$ make init profile=[profile_name]
  2. Deploy the project.

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