简体   繁体   中英

https.request to onesignal returns timeout on aws lambda

Basically below is my full code on aws lambda that calls onesignal to send notification. The code below works perfectly well in my local development pc, but when I upload it to lambda, it will only go up to 76 imini . The code in lambda doesn't even reach 63 imini inside sendNotification . Then the log will display Task timed out after 6.01 seconds

I've set the timeout to 30 seconds.

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)

I'm using the serverless framework, and I added the line below to my serverless.yaml config file, because my code need to access a resource in my vpc.

vpc:
    securityGroupIds:
      - sg-00099999000000
    subnetIds:
      - subnet-wd111111

I just tested if I removed the code above from my serverless.yaml config file, my code above runs good. But I need to have the config above in my serverless.yaml file, else my lambda won't be able to access my resources in vpc.

This might happen if you do not have an egress route to the API you are trying to invoke from Lambda. Check the security group on the Lambda, Internet Egress on the VPC etc.. Also, check if you need while-listing of any sorts on onesignal.com or it is a public API. A simple check to see if you can connect to internet from AWS Lambda can be done by doing a GET on google.com etc..

Here is an article about giving internet access to AWS Lambda.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM