@cumulus/aws-client
Version:
Utilities for working with AWS
56 lines • 2.3 kB
JavaScript
/**
* @module SNS
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSnsTopic = exports.publishSnsMessageWithRetry = void 0;
const p_retry_1 = __importDefault(require("p-retry"));
const logger_1 = __importDefault(require("@cumulus/logger"));
const client_sns_1 = require("@aws-sdk/client-sns");
const services_1 = require("./services");
const log = new logger_1.default({ sender: 'aws-client/sns' });
/**
* Publish a message to an SNS topic. Does not catch
* errors, to allow more specific handling by the caller.
*
* @param snsTopicArn - SNS topic ARN
* @param message - Message object
* @param retryOptions - options to control retry behavior when publishing
* a message fails. See https://github.com/tim-kos/node-retry#retryoperationoptions
* @returns {Promise<undefined>}
*/
const publishSnsMessageWithRetry = async (snsTopicArn, message, retryOptions = {}) => await (0, p_retry_1.default)(async () => {
if (!snsTopicArn) {
throw new p_retry_1.default.AbortError('Missing SNS topic ARN');
}
const publishInput = {
TopicArn: snsTopicArn,
Message: JSON.stringify(message),
};
await (0, services_1.sns)().send(new client_sns_1.PublishCommand(publishInput));
}, {
maxTimeout: 5000,
onFailedAttempt: (err) => log.debug(`publishSnsMessageWithRetry('${snsTopicArn}', '${JSON.stringify(message)}') failed with ${err.retriesLeft} retries left: ${JSON.stringify(err)}`),
...retryOptions,
});
exports.publishSnsMessageWithRetry = publishSnsMessageWithRetry;
/**
* Create an SNS topic with a given name.
*
* @param snsTopicName - Name of the SNS topic
* @returns - ARN of the created SNS topic
*/
const createSnsTopic = async (snsTopicName) => {
const createTopicInput = {
Name: snsTopicName,
KmsMasterKeyId: 'alias/aws/sns',
};
const createTopicCommand = new client_sns_1.CreateTopicCommand(createTopicInput);
const createTopicResponse = await (0, services_1.sns)().send(createTopicCommand);
return { TopicArn: createTopicResponse.TopicArn };
};
exports.createSnsTopic = createSnsTopic;
//# sourceMappingURL=SNS.js.map
;