Lan*_*Shi 2 amazon-web-services amazon-sns aws-lambda
我使用了一个非常简单的代码,从 AWS 提供的示例中稍作修改:
exports.handler = async (event) => {
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'ap-southeast-2'});
// Create publish parameters
var params = {
Message: 'This is a sample message',
Subject: 'Test SNS From Lambda',
TopicArn: 'arn:aws:sns:ap-southeast-2:577913011449:TestTopic'
};
// Create promise and SNS service object
var publishTextPromise = new AWS.SNS().publish(params).promise();
let response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
// Handle promise's fulfilled/rejected states
publishTextPromise.then(
function(data) {
console.log("Message ${params.Message} send sent to the topic ${params.TopicArn}");
console.log("MessageID is " + data.MessageId);
response.result = 'Success';
}).catch(
function(err) {
console.error(err, err.stack);
response.result = 'Error';
});
return response;
};
Run Code Online (Sandbox Code Playgroud)
测试此服务时出现超时错误。3秒是极限。
由于这是一个非常简单的过程,我认为执行时间不应超过 3 秒。
我已检查我的 IAM 设置并授予我的个人资料(管理员个人资料)对 SNS 服务的完全访问权限。但错误仍然存在。我想知道这里出了什么问题,我应该如何解决这个问题?
我不确定你为什么会超时,但你的代码不应该像你期望的那样工作。
看到您在.then()代码之外返回响应,这意味着您的代码将在您的.then()代码运行之前返回(承诺是异步的)。
由于您已经在使用 Node 8,因此最好使用async/await而不是使用旧.then().catch()方法。
我已经稍微重构了你的代码,它工作得很好。为了您的方便,我保留了原始参数。看看代码如何更容易阅读和调试。
'use strict';
// Load the AWS SDK for Node.js
const AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'ap-southeast-2'});
const sns = new AWS.SNS()
module.exports.handler = async (event) => {
const params = {
Message: 'This is a sample message',
Subject: 'Test SNS From Lambda',
TopicArn: 'arn:aws:sns:ap-southeast-2:577913011449:TestTopic'
};
let response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
try {
const data = await sns.publish(params).promise();
response.messageId = data.MessageId,
response.result = 'Success'
} catch (e) {
console.log(e.stack)
response.result = 'Error'
}
return response
};
Run Code Online (Sandbox Code Playgroud)
如果出于某种原因您不想使用 async/await,那么您需要将函数的返回移动到 .then() 代码中,并在调用 Promise 后立即返回,如下所示:
'use strict';
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'ap-southeast-2''});
// Create publish parameters
var params = {
Message: 'This is a sample message',
Subject: 'Test SNS From Lambda',
TopicArn: 'arn:aws:sns:ap-southeast-2:577913011449:TestTopic'
};
module.exports.handler = async (event) => {
// Create promise and SNS service object
var publishTextPromise = new AWS.SNS().publish(params).promise();
let response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
// Handle promise's fulfilled/rejected states
return publishTextPromise.then(
function(data) {
console.log("Message ${params.Message} send sent to the topic ${params.TopicArn}");
console.log("MessageID is " + data.MessageId);
response.result = 'Success';
return response;
}).catch(
function(err) {
console.error(err, err.stack);
response.result = 'Error';
return response
});
};
Run Code Online (Sandbox Code Playgroud)
不过,我强烈建议您使用方法 #1。
| 归档时间: |
|
| 查看次数: |
2646 次 |
| 最近记录: |