SNS.publish requires a callback while using promise() and sends 2 events for each invocation
I recently ran into this issue which took me longer than I would have liked to figure out.
I was using the promise()
version of the SNS.publish
method. Typescript was complaining that it required 2 arguments(params and callback) but I was only supplying 1 argument(params). So I added the callback to make typescript happy.
const sns = new SNS();
await sns
.publish(
{
Message: 'Message',
TopicArn: <Arn>,
},
(error) => {
if (error) {
console.log('error happened');
console.error(error);
}
},
)
.promise();
This results in the event being published twice.
According to this post on stackoverflow, sns is publishing the event once for the callback, and again for the promise.
The key to get around this and make typescript happy is specifying the API version in the SNS constructor
const sns = new SNS({ apiVersion: '2010-03-31' });
await sns
.publish(
{
Message: 'Message',
TopicArn: <Arn>,
},
)
.promise();