0

I have a pretty basic code snippet to test publishing of messages to SNS from Node Lambda:

exports.handler = async () => {
    const AWS = require('aws-sdk');
    AWS.config.update({region: 'us-east-2'});
    let result;
    try {
        result = await new AWS.SNS({apiVersion: '2010-03-31'}).publish({
            TopicArn: 'arn:aws:sns:us-east-2:99999999999:MyTopic',
            Message: 'Body of Message 1',
            Subject: 'Message 1'
        });
    } catch (err) {
        console.error('xxxxxxxx', err, err.stack);
        throw err;
    }
    console.info('>>>>>> ' + result.MessageId);
}

However, all I repeatedly get in the logs is >>>>>> undefined, and of course, the messages are not being published (because the queue subscribed to this is always empty). I can confirm that the Lambda function has the relevant permissions. What am I doing wrong?

ankush981
  • 5,159
  • 8
  • 51
  • 96

1 Answers1

2

You are not converting publish to a promise. This means that result = await will not work as expected.

Read my old answer for more understanding: How to use Async and Await with AWS SDK Javascript

TLDR;

result = await new AWS.SNS({apiVersion: '2010-03-31'}).publish({
            TopicArn: 'arn:aws:sns:us-east-2:99999999999:MyTopic',
            Message: 'Body of Message 1',
            Subject: 'Message 1'
        }).promise(); // !!!
tpetert
  • 39
  • 2
hoangdv
  • 15,138
  • 4
  • 27
  • 48
  • <> I don't know what to say . . . thanks! :( :( :'( – ankush981 Nov 04 '19 at 01:07
  • Looks like this is no longer necessary, as the sdk has a promise version in `"@aws-sdk/client-sns": "^3.7.0"`, but I'm still getting this error. – ps2goat Feb 26 '21 at 22:19
  • Looks like my issue was due to one service (STS) returning credentials with PascalCasedProperties, whereas SNS expected a credentials object with camelCasedProperties. – ps2goat Feb 26 '21 at 22:30