AWS Lambda发送SNS“成功"消息但实际上没有发送消息 [英] AWS lambda to send SNS "succeeds" but not message actually sent
问题描述
我编写了一个AWS lambda函数,以在上载S3对象时发送文本消息.我已经确认订阅,并且可以接收从SNS控制台发送的测试消息.
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.
当我测试lambda时,所有日志都说该方法成功了,但是没有儿子消息到达.这是函数(大多数只是示例模板,但是出于安全性考虑,在本文中更改了我的主题arn).任何有关要测试/下一步尝试的提示均表示赞赏.
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!');
};
推荐答案
您正在调用publish()
之后立即调用context.done()
. publish()函数是一个异步调用,您不必等待它完成.另外,我认为您的变量pubResult
不包含您期望的变量.
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.
尝试一下:
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!');
});
};
这篇关于AWS Lambda发送SNS“成功"消息但实际上没有发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!