问题描述
基本上,以下是我在aws lambda上的完整代码,该代码调用onesignal发送通知。下面的代码在我的本地开发PC上运行良好,但是当我将其上传到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","value": ""},"key": "studentId","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中的资源。
解决方法
如果您没有要尝试从Lambda调用的API的出口路由,则可能会发生这种情况。检查Lambda上的安全组,VPC上的Internet出口等。
另外,请检查您是否需要在onesignal.com
上列出各种清单,或者它是公共API。
可以通过在google.com等上执行GET来简单检查您是否可以从AWS Lambda连接到互联网。
Here是有关为AWS Lambda提供Internet访问的文章。