简体   繁体   English

如何在使用 AWS CDK 构建 cdk 时安装 lambda 函数的依赖项

[英]How to install dependencies of lambda functions upon cdk build with AWS CDK

When using AWS SAM I used to run build command which would go through all of my Lambda function packages and install their dependencies (run npm install on them).使用 AWS SAM 时,我曾经运行build命令,该命令将 go 通过我所有的 Lambda function 包并安装它们的依赖项(运行npm install它们)。

How can I achieve the same behavior with AWS CDK?如何使用 AWS CDK 实现相同的行为? It doesn't seem to do it automatically, or am I missing something?它似乎不会自动执行,还是我遗漏了什么?

This functionality really is missing.确实缺少此功能。 You'll need to write your own packaging.您需要编写自己的包装。 Keep in mind that lambda dependencies must be built on a system with the same architecture as the target system in AWS (Linux) if any of the dependencies (such as Numpy) uses a shared library with native C code.请记住,如果任何依赖项(例如 Numpy)使用具有本机 C 代码的共享库,则必须在与 AWS (Linux) 中的目标系统具有相同架构的系统上构建 lambda 依赖项。

There's a Docker image available which aims to provide an environment as close to AWS as possible: lambci/lambda:build-python3.7有一个 Docker 映像可用,旨在提供尽可能接近 AWS 的环境: lambci/lambda:build-python3.7

So if you're building on any non-Linux architecture, you might need this for some more complex lambda functions.因此,如果您在任何非 Linux 架构上构建,您可能需要它来处理一些更复杂的 lambda 函数。

EDIT: I opensourced my Python code for lambda packaging: https://gitlab.com/josef.stach/aws-cdk-lambda-asset编辑:我为 lambda 打包开源了我的 Python 代码: https : //gitlab.com/josef.stach/aws-cdk-lambda-asset

You can do this pretty easily with a local build script like this:您可以使用这样的本地构建脚本轻松完成此操作:

    const websiteRedirectFunction = new lambda.Function(
      this,
      "RedirectFunction",
      {
        code: lambda.Code.fromAsset(path.resolve(__dirname, "../../redirect"), {
          bundling: {
            command: [
              "bash",
              "-c",
              "npm install && npm run build && cp -rT /asset-input/dist/ /asset-output/",
            ],
            image: lambda.Runtime.NODEJS_12_X.bundlingDockerImage,
            user: "root",
          },
        }),
        handler: "index.redirect",
        tracing: lambda.Tracing.ACTIVE,
        runtime: lambda.Runtime.NODEJS_12_X,
      }
    );

Assuming you have a folder that you want to build and upload the handler and node_modules for Lambda.假设您有一个要为 Lambda 构建和上传处理程序和 node_modules 的文件夹。

From the docs :文档

When using lambda.Code.fromAsset(path) it is possible to bundle the code by running a command in a Docker container.使用 lambda.Code.fromAsset(path) 时,可以通过在 Docker 容器中运行命令来捆绑代码。 The asset path will be mounted at /asset-input.资产路径将安装在 /asset-input。 The Docker container is responsible for putting content at /asset-output. Docker 容器负责将内容放在 /asset-output 中。 The content at /asset-output will be zipped and used as Lambda code. /asset-output 中的内容将被压缩并用作 Lambda 代码。

It does not do it automatically You'll need to package those.它不会自动执行您需要将它们打包。 Then you'll be able to use fromAsset or fromBucket to get connect the code to the function然后你就可以使用fromAsset或 fromBucket 将代码连接到函数

There is (currently experimental) module inside aws-cdk which solves the problem for Python. aws-cdk有(目前是实验性的)模块可以解决 Python 的问题。

See more here . 在此处查看更多信息

I created a separate project using SAM put all the requirements in requirements.txt alongside your app.py我使用 SAM 创建了一个单独的项目,将 requirements.txt 中的所有要求与您的 app.py 放在一起

then run sam build --build-dir packaged The packaged directory will have the packaged artifact with dependencies.然后运行sam build --build-dir packaged packaged目录将包含打包后的工件和依赖项。

Then all that you have to do in your cdk is ` from aws_cdk import ( core, aws_lambda as lambda_)然后你在你的 cdk 中要做的就是` from aws_cdk import ( core, aws_lambda as lambda_)

..... .....

    lambdaFn = lambda_.Function(
        self, "DocManAuth",
        handler="app.lambda_handler",
        code=lambda_.Code.asset("../auth/packaged/DocManAuthFunction"),
        timeout=core.Duration.seconds(30),
        runtime=lambda_.Runtime.PYTHON_3_7,
    )

    core.CfnOutput(self, 'Authorizer Function',
                   value=lambdaFn.function_name)

` `

for a complete project visit docman完整的项目访问文档

For anyone using Python. (until aws-lambda-python lib is ready )对于使用 Python 的任何人。(直到 aws-lambda-python 库准备就绪

Install the function dependencies directly into the lambda function folder of your CDK project.将 function 依赖项直接安装到您的 CDK 项目的 lambda function 文件夹中。

pip install --target ./ -r ./requirements.txt

Requirements text is just a list of dependencies:需求文本只是依赖项列表:

requests==2.27.1

Then run:然后运行:

cdk deploy

Everything in the lambda function folder will be deployed:将部署 lambda function 文件夹中的所有内容:

  • Dependencies,依赖关系,
  • Function code, Function代码,
  • Requirements.txt, etc. Requirements.txt等

Some more details here:这里有更多细节:

https://docs.aws.amazon.com/lambda/latest/dg/python-package.html https://docs.aws.amazon.com/lambda/latest/dg/python-package.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM