尽管使用了依赖层,Lambda 函数包仍然很大

发布于 2025-01-09 23:03:26 字数 1410 浏览 1 评论 0原文

我有一个 Python Lambda,自从我开始使用 AWS X-Ray 以来,包大小已从 445KB 增加到 9.5MB。

并加快代码的部署,我单独打包了我的需求,并在我的模板中添加了一个层。 文档表明这种方法应该有效。

将依赖项打包在层中可以减少修改代码时上传的部署包的大小。

pip install --target ../package/python -r requirements.txt
Resources:
  ...

  ProxyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Architectures:
        - x86_64
      CodeUri: proxy/
      Handler: app.lambda_handler
      Layers:
        - !Ref ProxyFunctionLibraries
      Role: !GetAtt ProxyFunctionRole.Arn
      Runtime: python3.8
      Tracing: Active

  ProxyFunctionLibraries:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: proxy-function-lib
      Description: Dependencies for the ProxyFunction.
      ContentUri: package/.
      CompatibleRuntimes:
        - python3.8

然而,这似乎并没有阻止 Lambda 仍然将所有内容打包在顶层,每次部署包仍然是 9.5MB。由于某种原因,新层的大小为 11MB,但仅在进行更改时才会部署。

如何减小Lambda函数包的大小?

输入图片此处描述

I have a Python Lambda and since I started using AWS X-Ray the package size has ballooned from 445KB to 9.5MB.

enter image description here

To address this and speed up deployments of my code, I have packaged my requirements separately and added a layer to my template. The documentation suggests that this approach should work.

Packaging dependencies in a layer reduces the size of the deployment package that you upload when you modify your code.

pip install --target ../package/python -r requirements.txt
Resources:
  ...

  ProxyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Architectures:
        - x86_64
      CodeUri: proxy/
      Handler: app.lambda_handler
      Layers:
        - !Ref ProxyFunctionLibraries
      Role: !GetAtt ProxyFunctionRole.Arn
      Runtime: python3.8
      Tracing: Active

  ProxyFunctionLibraries:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: proxy-function-lib
      Description: Dependencies for the ProxyFunction.
      ContentUri: package/.
      CompatibleRuntimes:
        - python3.8

However, this doesn't seem to have prevented the Lambda from still packaging everything in the top layer, and every time I deploy the package is still 9.5MB. The new layer for some reason is 11MB in size, but that is only being deployed when a change is made.

How can I reduce the size of the Lambda function package?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…红帽 2025-01-16 23:03:26

实际上这里的解决方案非常简单,尽管对于非 Lambda 专家来说并不明显。

正如问题中所述,第一步是构建包库。

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

但是,当使用 sam build -u 构建 Lambda 时,会使用相同的“requirements.txt”文件,并再次安装所需的依赖项,这次是作为应用程序的一部分。

因此,我所要做的就是删除我希望打包在单独层中的需求并进行重建。这确实意味着我必须维护 2x 'requirements.txt',但这完全是可以管理的。

我已经打开了一个 问题,希望 AWS 能够更新他们的文档。

Actually the solution here was quite simple, although not obvious to non-Lambda experts.

As described in the question, the first step was to build the package library.

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

However, when building the Lambda using sam build -u the same 'requirements.txt' file is used and the required dependencies were again being installed, this time as part of the app.

So all I had to do was remove the requirements that I wish packaged in a separate layer and rebuild. It does mean that I have to maintain 2x 'requirements.txt' but that is entirely manageable.

I've opened an issue and hopefully AWS will update their documentation.

琉璃繁缕 2025-01-16 23:03:26

我正在努力解决同样的问题并以不同的方式解决它。将此答案留给未来的提问者。

我当前的 lambda 结构如下:

├── events
│   └── event.json
├── ingress
│   ├── app.py # lambda code
│   └── __init__.py
├── __init__.py
├── lib_layer # contains self written helpers and python dependencies
│   ├── helper.py
│   └── requirements.txt
├── samconfig.toml
├── template.yaml
└── tests
    ├── ...

在我的 template.yaml 中,我有以下代码片段:

SQSIngestion:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ingress/
      Handler: app.lambda_handler
      Runtime: python3.9
      Layers:
        - !Ref PythonLibLayer
      Architectures:
        - x86_64
  PythonLibLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      ContentUri: lib_layer
      CompatibleRuntimes:
        - python3.9
    Metadata:
      BuildMethod: python3.9

由于 Lambda 函数已经具有包含所有依赖项 + 我附加的帮助程序的层,因此 requirements.txt /ingress 中的 可以省略,但在 AWS 中调用该函数时仍然有效。通过致电
sam build 会自动构建依赖层并忽略构建依赖的函数文件夹。

作为提示,如果使用 VSCode,请在 settings.json 中添加以下行来修复自行编写的包的 pylances 导入错误。

{
  "python.analysis.extraPaths": ["lib_layer"]
}

I am struggling with the same problem and solved it differently. Leaving this answer as a note to future question-seekers.

My current lambda structure is the following:

├── events
│   └── event.json
├── ingress
│   ├── app.py # lambda code
│   └── __init__.py
├── __init__.py
├── lib_layer # contains self written helpers and python dependencies
│   ├── helper.py
│   └── requirements.txt
├── samconfig.toml
├── template.yaml
└── tests
    ├── ...

In my template.yaml I have the following code snippets:

SQSIngestion:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ingress/
      Handler: app.lambda_handler
      Runtime: python3.9
      Layers:
        - !Ref PythonLibLayer
      Architectures:
        - x86_64
  PythonLibLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      ContentUri: lib_layer
      CompatibleRuntimes:
        - python3.9
    Metadata:
      BuildMethod: python3.9

Since the Lambda Function already has the layer containing all dependencies + my helpers attached, the requirements.txt in /ingress can be omitted but works nevertheless when invoking the function in AWS. By calling
sam build will automatically build the dependency layer and ignore the function folder for building the dependencies.

As a tip, if working with VSCode, add in your settings.json the following line to fix pylances import error for self written packages.

{
  "python.analysis.extraPaths": ["lib_layer"]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文