无法发布到SNS主题,使MessageId为未定义

时间:2019-11-04 00:55:49

标签: node.js aws-lambda amazon-sns

我有一个非常基本的代码段,用于测试从Node Lambda向SNS消息的发布:

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);
}

但是,我反复得到的日志都是>>>>>> undefined,当然,这些消息也没有发布(因为预订的队列始终为空)。我可以确认Lambda函数具有相关权限。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您没有将publish转换为承诺。这意味着result = await将无法正常工作。

请阅读我的旧答案以进一步了解: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(); // !!!