Created
February 7, 2022 00:37
-
-
Save jpcaparas/c876af44ea4fa27ed59d09d641fb336d to your computer and use it in GitHub Desktop.
AWS | Publish SNS from Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { SNSClient, PublishCommand } = require("@aws-sdk/client-sns"); // This requires a Lambda Layer to be installed | |
exports.handler = async (event) => { | |
var eventText = JSON.stringify(event, null, 2); | |
var client = new SNSClient({region: process.env.REGION}); | |
var params = { | |
Message: eventText, | |
Subject: "Test SNS From Lambda", | |
TopicArn: process.env.SNS_TOPIC_ARN | |
}; | |
await client.send(new PublishCommand(params)) | |
var response = { | |
statusCode: 200, | |
body: JSON.stringify('Hello from Lambda!'), | |
}; | |
return response; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require("aws-sdk"); | |
AWS.config.region = process.env.REGION; | |
exports.handler = async (event, context) => { | |
var eventText = JSON.stringify(event, null, 2); | |
var sns = new AWS.SNS(); | |
var params = { | |
Message: eventText, | |
Subject: "Test SNS From Lambda", | |
TopicArn: process.env.SNS_TOPIC_ARN | |
}; | |
try { | |
await sns.publish(params, (err, data) => { | |
if (data) { | |
console.log('Success', data); | |
} else { | |
console.log('Error', err) | |
} | |
}).promise(); | |
} catch (e) { | |
console.log('Try...catch error', e.stack); | |
return; | |
} | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify('Hello from Lambda!'), | |
}; | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment