Search code examples
node.jsaws-lambdajsdom

Load external resource to jsdom in AWS lambda


I have a problem while using jsdom with lambda functions.

Right now I have a small lambda that inject a snippet called myValue:

return new JSDOM(
  `<!DOCTYPE html><html>
      <head>
         <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
      </head>
      <body><script> console.log('hello world')</script></body>
   </html>`,
   { runScripts: "dangerously", url: `https://www.${origin}`, virtualConsole: createVirtualConsole(), resources: "usable" }

This is working as expected when I remove the external jquery resource it loads and executes myValue content, but in the moment I add the jquery injection I get an error in my lambda:

{
  "errorMessage": "2021-04-28T16:02:34.719Z 7rtre1d0-5060-4b9b-993e-5037rxzz25 Task timed out after 14.01 seconds"
}

I know it takes a while to load the resource but 14 sec are too much, so I wonder if there's a different way of doing this or even if I'm doing it right.

I also tried with:

const resourceLoader = new jsdom.ResourceLoader({
  proxy: src="https://code.jquery.com/jquery-3.5.1.min.js",
  strictSSL: false
});

But didn't work as well.

Thanks for the help!


Solution

  • If you've associated your Lambda in a VPC, make sure your Lambda is either in a public subnet (has acces to an IGW) or that there's a NAT gateway thru which it can access the internet.

    See: How do I give internet access to a Lambda function that's connected to an Amazon VPC?