如何在 AWS Lambda 中加载 npm 模块? [英] How to load npm modules in AWS Lambda?

查看:23
本文介绍了如何在 AWS Lambda 中加载 npm 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用基于 Web 的编辑器创建了多个 Lambda 函数.到现在为止还挺好.我现在想开始用模块来扩展它们(例如 Q 表示承诺).我不知道如何将模块导出到 Lambda 以便我的函数使用它们.

I've created several Lambda functions using the web based editor. So far so good. I'd now like to start extending those with modules (such as Q for promises). I can't figure out how to get the modules out to Lambda so they can be consumed by my functions.

我已经阅读了this,但它似乎涉及设置一个EC2 并从那里运行 Lambda 函数.创建函数时有一种上传 zip 的机制,但这似乎涉及发送本地开发的函数.由于我在基于 Web 的编辑器中工作,这似乎是一个奇怪的工作流程.

I've read through this but it seems to involve setting up an EC2 and running Lambda functions from there. There is a mechanism to upload a zip when creating a function but that seems to involve sending up functions developed locally. Since I'm working in the web based editor that seems like a strange workflow.

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

How can I simply deploy some modules for use in my Lambda functions?

推荐答案

如果不上传 .zip 文件,您将无法加载 NPM 模块,但实际上您可以将此过程简化为两个快速命令行.

You cannot load NPM modules without uploading a .zip file, but you can actually get this process down to two quick command lines.

方法如下:

  1. 将您的 Lambda 函数文件放在单独的目录中.这是因为您在本地为 Lambda 安装了 npm 包,并且您希望能够隔离和测试您将上传到 Lambda 的内容.

  1. 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.

使用 npm install packageName 在本地安装 NPM 包,同时您在步骤 1 中创建的单独 Lambda 目录中.

Install your NPM packages locally with npm install packageName while you're in your separate Lambda directory you created in step #1.

确保您的函数在本地运行时有效:node lambdaFunc.js(您可以简单地注释掉代码中的两行 export.handler 以适应您的代码在本地与 Node 一起运行).

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).

转到 Lambda 的目录并压缩内容,确保包含目录本身.

Go to the Lambda's directory and compress the contents, make sure not to include the directory itself.

zip -r lambdaFunc.zip .

  • 如果你安装了 aws-cli,如果你想让你的生活更轻松,我建议你安装它,你现在可以输入这个命令:

  • 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
    

    (上面的 lambdaFunc 部分没有引号,以防你像我一样想知道)

    (no quotes around the lambdaFunc part above in case you wonder as I did)

    现在您可以在 Lambda 控制台中单击测试.

    Now you can click test in the Lambda console.

    我建议为上述两个命令添加一个简短的别名.这是我为更长的 Lambda 更新命令所拥有的:

    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"
    

  • 这篇关于如何在 AWS Lambda 中加载 npm 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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