AWS lambda to send SNS "succeeds" but message not actually sent

Issue

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

Solution

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

Answered By – Mark B

Answer Checked By – Pedro (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.