我已经使用基于web的编辑器创建了几个Lambda函数。到目前为止一切顺利。现在我想开始用模块扩展这些模块(比如Q表示承诺)。我不知道如何将模块转换为Lambda,以便它们可以被我的函数使用。

我已经读完了这篇文章,但它似乎涉及到设置一个EC2并从那里运行Lambda函数。在创建函数时,有一种机制可以上传压缩文件,但这似乎涉及到发送本地开发的函数。因为我在基于网络的编辑器中工作,这似乎是一个奇怪的工作流程。

我如何简单地部署一些模块在我的Lambda函数中使用?


当前回答

希望这对你有所帮助,使用无服务器框架你可以做这样的事情:

在无服务器中添加这些东西。yml文件:

    plugins:
      - serverless-webpack
    custom:
      webpackIncludeModules:
        forceInclude:
          - <your package name> (for example: node-fetch)

然后创建你的Lambda函数,通过无服务器部署来部署它,即无服务器中包含的包。Yml会在你身边。

有关无服务器的更多信息:https://serverless.com/framework/docs/providers/aws/guide/quick-start/

其他回答

你不能在不上传.zip文件的情况下加载NPM模块,但实际上你可以将这个过程简化为两个快速命令行。

方法如下:

Put your Lambda function file(s) in a separate directory. This is because you install npm packages locally for Lambda and you want to be able to isolate and test what you will upload to Lambda. Install your NPM packages locally with npm install packageName while you're in your separate Lambda directory you created in step #1. Make sure your function works when running locally: node lambdaFunc.js (you can simply comment out the two export.handler lines in your code to adapt your code to run with Node locally). Go to the Lambda's directory and compress the contents, make sure not to include the directory itself. zip -r lambdaFunc.zip . If you have the aws-cli installed, which I suggest having if you want to make your life easier, you can now enter this command: aws lambda update-function-code --function-name lambdaFunc \ --zip-file fileb://~/path/to/your/lambdaFunc.zip (no quotes around the lambdaFunc part above in case you wonder as I did) Now you can click test in the Lambda console. I suggest adding a short alias for both of the above commands. Here's what I have in mine for the much longer Lambda update command: alias up="aws lambda update-function-code --function-name lambdaFunc \ --zip-file fileb://~/path/to/your/lambdaFunc.zip"

现在您可以使用Lambda Layers来处理这些问题。只需添加一个包含您需要的包的层,它就会完美地运行。

关注这篇文章: https://medium.com/@anjanava.biswas/nodejs-runtime-environment-with-aws-lambda-layers-f3914613e20e

希望这对你有所帮助,使用无服务器框架你可以做这样的事情:

在无服务器中添加这些东西。yml文件:

    plugins:
      - serverless-webpack
    custom:
      webpackIncludeModules:
        forceInclude:
          - <your package name> (for example: node-fetch)

然后创建你的Lambda函数,通过无服务器部署来部署它,即无服务器中包含的包。Yml会在你身边。

有关无服务器的更多信息:https://serverless.com/framework/docs/providers/aws/guide/quick-start/

npm模块必须捆绑在你的nodejs包中,并以zip的形式上传到AWS Lambda Layers,然后你需要如下所示引用你的模块/js,并使用其中的可用方法。 const mymodule = require('/opt/nodejs/MyLogger');

在摆弄了几个小时的包之后,我发现它似乎对在浏览器中运行做了一些假设(即使我告诉它使用engine: node)。

而不是:


esbuild

esbuild更简单,也更快!

只需运行npm add——save-dev esbuild,并将这些脚本添加到你的package.json:

{
  ...
  "scripts": {
    "build": "esbuild --bundle --minify --platform=node --target=node12 --outdir=build main.js",
    "export": "cd build && zip main.js.zip main.js"
  },
  ...
  "devDependencies": {
    "esbuild": "^0.11.19",
    ...
  }
}

这允许我在使用aws-sdk的同时仍然进行树摇和缩小,同时仍然能够安装其他依赖项,如jest和eslint,而不必打包整个node_modules文件夹。

要在CI中构建一个包,只需: NPM ci && NPM运行构建&& NPM运行导出

build/main.js.zip文件将包含您需要的所有内容!