Skip to content

Instantly share code, notes, and snippets.

@ARHEIO
Last active April 16, 2019 06:33
Show Gist options
  • Save ARHEIO/51abb821414077fa1e91bb899275757a to your computer and use it in GitHub Desktop.
Save ARHEIO/51abb821414077fa1e91bb899275757a to your computer and use it in GitHub Desktop.
Example Lambda that takes an event with a message and a message attribute and injects it into an ARN
/**
* This is an example body that you would pass in through the test function
* {
* "snsMessage": "This is a test body that will come through",
* "messageAttributes": {
* "insurance_type": {
* "DataType": "String",
* "StringValue": "car"
* }
* }
* }
*/
console.log('Loading function');
var AWS = require('aws-sdk');
AWS.config.region = 'ap-southeast-2';
exports.handler = async function(event) {
async function publishMessage() {
var params = {
MessageAttributes: event.messageAttributes,
Message: event.snsMessage, /* required */
TopicArn: 'snsTopicArn'
};
// Create promise and SNS service object
return new AWS.SNS({apiVersion: '2010-03-31'}).publish(params).promise()
.then(() => {
const response = {
statusCode: 200,
body: JSON.stringify('Publish Successful'),
};
return response;
})
.catch((err) => {
console.error(err, err.stack);
const response = {
statusCode: 400,
body: JSON.stringify('Publish Unsuccessful'),
};
return response;
});
};
const eventResponse = await publishMessage()
return eventResponse;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment