【发布时间】:2021-02-08 09:33:42
【问题描述】:
基本上下面是我在 aws lambda 上调用 onesignal 发送通知的完整代码。下面的代码在我的本地开发电脑上运行良好,但是当我将它上传到 lambda 时,它只会上升到76 imini。 lambda 中的代码甚至没有达到63 imini inside sendNotification。然后日志会显示Task timed out after 6.01 seconds
我已将超时设置为 30 秒。
const handler = (req, res) => {
console.log("entering test/notification")
var title = "Notification Testing";
var contents = "Ni contents utk test notification!";
var message = {
app_id: "41eb5e44-2c10-4f0d-a47f-632d717b0264",
headings: {"en": title},
contents: {"en": contents},
filters: [
{"field": "tag", "key": "parentId", "relation": "=", "value": "831113"},
{"operator": "OR"},
{"field": "tag", "key": "parentId", "relation": "=", "value": ""},
{"operator": "OR"},
{"field": "tag", "key": "studentId", "relation": "=", "value": "5c1a7986c98da061141475b4"}
]
};
sambung(message)
async function sambung (message){
var sendResult = await testSendNotification(message)
console.log("sendResult")
console.log(sendResult)
res.status(200).send({
sendResult: sendResult
});
}
function testSendNotification(data) {
console.log("entering testSendNotification with")
console.log(data)
var headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Basic SomeAuthenticationKey"
};
return new Promise((resolve, reject) => {
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
};
var https = require('https');
var req = https.request(options, function (res) {
console.log("63 imini inside sendNotification");
res.on('data', function (data) {
console.log("Response:");
console.log(JSON.parse(data));
resolve(JSON.parse(data))
});
});
req.on('error', function(e) {
console.log("ERROR:");
console.log(e);
reject(e)
});
console.log("76 imini")
req.write(JSON.stringify(data));
req.end();
});
}
};
module.exports = handler
module.exports.handler = require('./../handlerWrapper')(handler)
我正在使用serverless 框架,并将下面的行添加到我的serverless.yaml 配置文件中,因为我的代码需要访问我的vpc 中的资源。
vpc:
securityGroupIds:
- sg-00099999000000
subnetIds:
- subnet-wd111111
我刚刚测试了如果我从我的serverless.yaml 配置文件中删除了上面的代码,我上面的代码运行良好。但我需要在我的serverless.yaml 文件中包含上述配置,否则我的 lambda 将无法访问我在 vpc 中的资源。
【问题讨论】:
标签: node.js aws-lambda onesignal