AWS lambda to send SNS “succeeds” but not message actually sent

拥有回忆 提交于 2019-12-11 00:04:15

问题


I've written an AWS lambda function to send a text message when an S3 object is uploaded. I've confirmed the subscription and I can receive test messages sent from the SNS console.

When I test the lambda all the logs say the method succeeds but no sons message ever arrives. Here is the function (mostly just the sample template but changed my topic arn in this post for security). Any hints of things to test/try next are appreciated.

console.log('Loading function');
var aws = require('aws-sdk');
var s3 = new aws.S3({ apiVersion: '2006-03-01' });
exports.handler = function(event, context) {
    var bucket = event.Records[0].s3.bucket.name;
    var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
    var params = {
        Bucket: bucket,
        Key: key
    };
    var sns = new aws.SNS();
    console.log('start of brians sns function')
    var pubResult = sns.publish({
        Message: 'Test publish to SNS from Lambda',
        TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:lambdatop'
    }, function(err, data) {
        if (err) {
            console.log(err.stack);
            return;
        }
        console.log('push sent');
        console.log(data);
    });
    console.log('after sns publish:')
    console.log(pubResult)
    context.done(null, 'Brians Function Finished!');  
};

回答1:


You are calling context.done() right after calling publish(). The publish() function is an asynchronous call, and you aren't waiting for it to finish. Also, I don't think your variable pubResult contains what you expect it to.

Try this:

console.log('Loading function');
var aws = require('aws-sdk');
exports.handler = function(event, context) {
    var sns = new aws.SNS();
    console.log('start of brians sns function')
    sns.publish({
        Message: 'Test publish to SNS from Lambda',
        TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:lambdatop'
    }, function(err, data) {
        if (err) {
            console.log(err.stack);

            // Notify Lambda that we are finished, but with errors
            context.done(err, 'Brians Function Finished with Errors!');  
            return;
        }
        console.log('push sent');
        console.log(data);

        // Notify Lambda that we are finished
        context.done(null, 'Brians Function Finished!');  
    });
};


来源:https://stackoverflow.com/questions/34205738/aws-lambda-to-send-sns-succeeds-but-not-message-actually-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!