serverless-ses-sns
Version:
Serverless plugin to add a SNS destination to a SES ConfigurationSet
34 lines (26 loc) • 1.52 kB
JavaScript
const makeCreateSNSDestinationHook = (createTopic, createOrUpdateSNSDestination, logger) => async (service) => {
const snsDestination = service.custom.snsDestination;
if (!snsDestination.topicArn) {
const topic = getTopicName(service);
const { TopicArn } = await createTopic(topic);
logger.log(`Created SNS topic ${topic}`);
snsDestination.topicArn = TopicArn;
}
const { events, topicArn, configurationSet } = snsDestination;
const destination = getDestinationName(service);
await createOrUpdateSNSDestination(destination, configurationSet, events, topicArn);
logger.log(`SNS destination added to configurationSet ${configurationSet}`);
};
const makeRemoveSNSDestinationHook = (removeTopic, removeSNSDestination, logger) => async (service) => {
const snsDestination = service.custom.snsDestination;
await removeSNSDestination(getDestinationName(service), snsDestination.configurationSet);
logger.log(`SNS destination removed from configurationSet ${snsDestination.configurationSet}`);
if (!snsDestination.topicArn) {
const topic = getTopicName(service);
await removeTopic(topic);
logger.log(`SNS topic ${topic} removed`);
}
};
const getTopicName = (service) => `${service.service}-${service.provider.stage}-SES-topic`;
const getDestinationName = (service) => `${service.service}-${service.provider.stage}-SNS-event-destination`;
module.exports = { makeCreateSNSDestinationHook, makeRemoveSNSDestinationHook };