Created
December 4, 2019 15:52
-
-
Save marekkrzynowek/23745c9777f2fd35ec049e941bd49949 to your computer and use it in GitHub Desktop.
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 utils = require('@ucapp/unioncore-utils'); | |
const AWS = require('aws-sdk'); | |
module.exports.helloWorld = async (event, context) => { | |
context.callbackWaitsForEmptyEventLoop = false; | |
try { | |
const msgs = []; | |
for (const r of event.Records) { | |
const payload = _extractMessageFromPayload(r); | |
// Do stuff with the payload. | |
await utils.publishToSNS( | |
new AWS.SNS({ apiVersion: '2010-03-31' }), { | |
Message: JSON.stringify(payload), | |
TopicArn: process.env.HELLO_TOPIC, | |
}, | |
); | |
msgs.push(payload.message); | |
console.log('Payload', payload); | |
} | |
return utils.buildResponse({ | |
statusCode: 200, | |
body: { | |
message: msgs.join(' ') | |
} | |
}); | |
} catch(err) { | |
console.log('Error: ', err.message); | |
throw err; | |
} | |
}; | |
function _extractMessageFromPayload(response) { | |
const body = JSON.parse(response.body); | |
return JSON.parse(body.Message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment