-
-
Save ikbelkirasan/61d1fe6a8a85867f36a817133a63e12c to your computer and use it in GitHub Desktop.
SNS util methods for easier publishing with promises
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 AWS = require('aws-sdk'); | |
const sns = new AWS.SNS(); | |
function snsEventToJson(sns) { | |
return JSON.parse(sns.Records[0].Sns.Message); | |
} | |
function jsonToSnsParams(jsonObject) { | |
return { | |
Message: JSON.stringify(jsonObject) | |
}; | |
} | |
function publish(params) { | |
// sns topic from env | |
params.TargetArn = process.env.SNS_TOPIC; | |
return new Promise((resolve, reject) => { | |
sns.publish(params, (err, data) => { | |
if (err) { | |
return reject(err); | |
} else { | |
return resolve(data); | |
} | |
}); | |
}); | |
} | |
// usage: | |
function publishThisThing(param, paramtwo) { | |
return publish(jsonToSnsParams({ | |
'keyOne':'blah', | |
'paramOne': param, | |
'paramTwo': paramtwo | |
})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment