AWS lambda 发送 SNS "succeeds" 但消息并未实际发送
AWS lambda to send SNS "succeeds" but message not actually sent
我编写了一个 AWS lambda 函数来在上传 S3 对象时发送文本消息。我已确认订阅,我可以接收从 SNS 控制台发送的测试消息。
当我测试 lambda 时,所有日志都说该方法成功但没有儿子消息到达。这是函数(主要只是示例模板,但为了安全起见,在此 post 中更改了我的主题 arn)。任何关于 test/try 的提示都将不胜感激。
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
不包含您期望的内容。
试试这个:
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!');
});
};
解决此问题的另一种方法是将 SNS 发布包装在 Promise 中,并等待它从 Lambda 处理程序中解决。
exports.handler = async (event) => {
await publishSNS(record, process.env.TOPIC_ARN);
}
async function publishSNS(payload, topicArn) {
await SNS.publish({
Message: JSON.stringify(payload),
TargetArn: topicArn
}).promise().then((data) => {
console.log('SNS push succeeded: ', data);
}).catch((err) => {
console.error(err);
});
}
我编写了一个 AWS lambda 函数来在上传 S3 对象时发送文本消息。我已确认订阅,我可以接收从 SNS 控制台发送的测试消息。
当我测试 lambda 时,所有日志都说该方法成功但没有儿子消息到达。这是函数(主要只是示例模板,但为了安全起见,在此 post 中更改了我的主题 arn)。任何关于 test/try 的提示都将不胜感激。
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
不包含您期望的内容。
试试这个:
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!');
});
};
解决此问题的另一种方法是将 SNS 发布包装在 Promise 中,并等待它从 Lambda 处理程序中解决。
exports.handler = async (event) => {
await publishSNS(record, process.env.TOPIC_ARN);
}
async function publishSNS(payload, topicArn) {
await SNS.publish({
Message: JSON.stringify(payload),
TargetArn: topicArn
}).promise().then((data) => {
console.log('SNS push succeeded: ', data);
}).catch((err) => {
console.error(err);
});
}