DEV Community

Cover image for AWS CDK stack for building Amazon EventBridge Pipes (SQS to CloudWatch Logs)
Kohei (Max) MATSUSHITA for AWS Heroes

Posted on

AWS CDK stack for building Amazon EventBridge Pipes (SQS to CloudWatch Logs)

I have released an AWS CDK(v2) stack that builds the following Amazon EventBridge Pipes.

This stack provides a quick experience in building Amazon EventBridge Pipes.

GitHub logo ma2shita / cdk-eventbridge-pipes-simplelogger

Example an AWS CDKv2 for building the Amazon EventBridge Pipes that logs messages sent to an SQS queue to CloudWatch Log.

AWS CDKv2 example: SQS to CloudWatch Logs (w/ Enrichment by Lambda) on Amazon EventBridge Pipes

architecture

Example an AWS CDKv2 for building the Amazon EventBridge Pipes that logs messages sent to an SQS queue to CloudWatch Log.
It also includes an enrichment(but pass-throught) by a Lambda function.

(ja) SQSキューに送信されたメッセージをCloudWatch Logにログ出力するAmazon EventBridge Pipesを構築する、AWS CDKv2 のサンプル。
Lambda関数による強化(enrichment/内容は"何もしない")も含んでいます。

Created by:

  • Queue (1)
  • Log Group (1)
  • Lambda function (1)
  • Pipe (1)

Requirements

  • AWS CDKv2 env. (AWS Cloud9 (It's very easy))
    • cdk bootstrap must be done

Build & Deploy

Build:

git clone https://github.com/ma2shita/cdk-eventbridge-pipes-simplelogger.git
cd eventbridge-pipes-simplelogger/
npm install
cdk ls
# => EventBridgePipesSimpleLoggerStack

Note If you have "Newer version" in npm or aws_cdk then run to npm install -g --force npm aws-cdk

Deploy:

cdk deploy

Note The target region is set to us-west-2. This is specified in bin/event_bridge_pipes_simple_logger.ts.

Destroy:

cdk destroy

How it works

Send SQS queue message then will record toeven CloudWatch Log…

If you are on AWS Cloud9, you can cdk bootstrap immediately because the environment is ready up to the cdk command.

Contains "enrichment" Lambda functions

"Enrichment" can invoke Lambda function and Step Function, reducing the amount of code compared to a stand-alone Lambda implementation.

This CDKv2 stack contains Lambda functions that can be called from "enhancements". It is "do nothing" code, though, so please see it as a framework.
How to customize it is described below.

Customization Points

On the "Enrichment" function

The "enrichment" function allows editing of the payload. For example, you can lookup items from DynamoDB by the input then append the results to the payload.

For specifications of the Lambda function to be specified for enhancement, see here.
AWS Lambda function specifications in "Enrichment" in Amazon EventBridge Pipes

On the "Target" of EventBridge Pipes

Once you have changed the "target", remember to grant write permission to the destination service to the Pipes Execution role.
In particular, events:InvokeApiDestination is required if you specify an "API destination" that can send to an external API.

Conclusion and "CDKv2 for IoT 1-Click"

I'd love to hear your feedback!

And, an AWS CDK(v2) stack for creating AWS IoT 1-Click projects is also available. Button data can be sent to Amazon EventBridge Pipes.

GitHub logo ma2shita / cdk-iot1click-project-to-sqs

Example of an AWS CDKv2 for building the AWS IoT 1-Click project with sending to exists SQS queue Lambda function.

AWS CDKv2 example: IoT 1-Click to exists SQS queue

architecture

Example of an AWS CDKv2 for building the AWS IoT 1-Click project with sending to exists SQS queue Lambda function.

(ja) 既存のSQSキューへメッセージ送信をするIoT 1-Clickプロジェクトと、Lambda 関数を構築するAWS CDKv2の例。

Created by:

  • Lambda function (1)
  • IoT 1-Click project (1)

NOT creates:

  • SQS queue
  • IoT 1-Click placement
  • IoT 1-Click device registration

Requirements

  • AWS CDKv2 env. (AWS Cloud9 (It's very easy))
    • cdk bootstrap must be done
  • SQS queue
    • queue's ARN
  • AWS IoT 1-Click device

Build & Deploy

Build:

git clone https://github.com/ma2shita/cdk-iot1click-project-to-sqs.git
cd iot1click-project-to-sqs/
npm install
cdk ls --context destSqsQueueArn=<SQS_QUEUE_ARN>
#=> Iot1ClickProjectStack
# e.g.) cdk ls --context destSqsQueueArn=arn:aws:sqs:REGION:ACCOUNT:sqs-queue-name

Deploy:

cdk deploy --context destSqsQueueArn=<SQS_QUEUE_ARN>

Note The target region is set to us-west-2. This is specified in bin/iot1click_project.ts.

Destroy:

cdk destroy --context destSqsQueueArn=<SQS_QUEUE_ARN>

How it works

Register devices in IoT 1-Click then add the device to the project (created with this CDK).
Pressing the button sends the message…

So, It would be nice if IoT 1-Click would support sending directly to SQS. :-)

EoT

Top comments (0)