简体   繁体   中英

How to deploy large nodejs package to AWS Lambda?

I am trying to deploy a simple script to AWS Lambda that would generate critical css for a website. Running this serverless seems to make sense (but I cannot find any working examples).

The problem is with package size. I am trying to use https://github.com/pocketjoso/penthouse . When I simply npm install penthouse suddenly the package size is over 300MB. Size limit on Lambda is only 250MB and it will not upload.

Is there any way to solve this? Perhaps download penthouse on the fly? If so, is there any example?

Performance is not so critical in this case as it would be called only a few times a day by an automated process.

Looking at the bundle size of the package ( https://bundlephobia.com/result?p=penthouse ), it doesn't appear that your issue is primarily with the penthouse package.

Although I cannot say for certain, I think it's mainly down to the size of your other dependencies.

Nevertheless, seen as this isn't a critical system and will be accessed a few times a day via automation processes, you can reduce the size of your node_modules folder by using a CDN.

There are a number of services which allow you to do this, I have primarily used UNPKG and jsDelivr in the past as they appear to be reliable with minimal-to-no downtime.

I lack the required detail from your question regarding which technology you're specifically using and the extent you can go to in order to achieve your desired result, but there are a few options you can choose:

I don't know much about penthouse but with scriptjs , I assume you can achieve something like this:

var penthouseScript = require("scriptjs");

penthouseScript("https://cdn.jsdelivr.net/npm/penthouse@2.2.2/lib/index.min.js", () => {

  // penthouse related code

  penthouse({
    url: 'http://google.com',
    cssString: 'body { color: red }'
  })
  .then(criticalCss => {
    // use the critical css
    fs.writeFileSync('outfile.css', criticalCss);
  });

});

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