Desta
Desta

Reputation: 13

Are there any cdk typescript examples of a lambda publishing to an sns topic?

I'm trying to setup a lambda to receive a dynamo dB stream and publish these database changes to an sns topic. I'm not sure how I would publish a message from the lambda to my topic using the cdk. Are there any examples of this?

Upvotes: 1

Views: 2348

Answers (1)

miensol
miensol

Reputation: 41678

aws-cdk is used to create AWS resources. In other words you use it to create AWS Labmda, Dynamo DB table, SNS Topic. However, once those resources are created one rarely uses CDK to operate on them.

In order to send messages to SNS inside your Lambda refer to documentation of AWS SDK for the runtime you choose. For instance, if you decided to write your lambda in node.js you would refer to AWS SDK for node.js.

This is how your lambda code could look like:

import { SNS } from "aws-sdk";

const sns = new SNS();

export const handler = async function(event){
   const params = {
     Message: JSON.stringify({ some: 'payload' }),
     // it is easy to pass reference to the topic as environment variable using aws cdk
     TopicArn: process.env.SOME_TOPIC_ARN 
   };
   await sns.publish(params).promise()
}

Upvotes: 2

Related Questions