如何将大型 nodejs 包部署到 AWS Lambda? [英] How to deploy large nodejs package to AWS Lambda?

查看:50
本文介绍了如何将大型 nodejs 包部署到 AWS Lambda?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个简单的脚本部署到 AWS Lambda,该脚本将为网站生成关键的 css.运行这个无服务器似乎很有意义(但我找不到任何工作示例).

问题在于包装尺寸.我正在尝试使用 https://github.com/pocketjoso/penthouse.当我简单地 npm install penthouse 突然包大小超过 300MB.Lambda 的大小限制仅为 250MB,并且不会上传.

有什么办法可以解决这个问题吗?也许可以即时下载顶层公寓?如果是这样,有什么例子吗?

在这种情况下,性能并不那么重要,因为自动化流程每天只会调用几次.

解决方案

查看包的包大小 (https://bundlephobia.com/result?p=penthouse),您的问题似乎不是主要与 penthouse 包有关.

虽然我不能肯定,但我认为这主要取决于您的其他依赖项的大小.

尽管如此,由于这不是一个关键系统,并且每天会通过自动化流程访问几次,因此您可以使用 CDN 减小 node_modules 文件夹的大小.

有许多服务可以让您做到这一点,我过去主要使用 UNPKGjsDelivr,因为它们看起来是可靠的- 没有停机时间.

我没有从您的问题中详细了解您具体使用哪种技术以及您可以在多大程度上达到预期结果,但您可以选择以下几个选项:

我对 penthouse 了解不多,但是使用 scriptjs,我假设您可以实现以下目标:

var penthouseScript = require("scriptjs");penthouseScript("https://cdn.jsdelivr.net/npm/penthouse@2.2.2/lib/index.min.js", () => {//阁楼相关代码顶层公寓({网址:'http://google.com',cssString: 'body { color: red }'}).then(criticalCss => {//使用关键 cssfs.writeFileSync('outfile.css', criticalCss);});});

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);
  });

});

这篇关于如何将大型 nodejs 包部署到 AWS Lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆