尽管使用了依赖层,Lambda 函数包仍然很大
我有一个 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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上这里的解决方案非常简单,尽管对于非 Lambda 专家来说并不明显。
正如问题中所述,第一步是构建包库。
但是,当使用 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.
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.
我正在努力解决同样的问题并以不同的方式解决它。将此答案留给未来的提问者。
我当前的 lambda 结构如下:
在我的
template.yaml
中,我有以下代码片段:由于 Lambda 函数已经具有包含所有依赖项 + 我附加的帮助程序的层,因此
requirements.txt
可以省略,但在 AWS 中调用该函数时仍然有效。通过致电/ingress
中的sam build
会自动构建依赖层并忽略构建依赖的函数文件夹。作为提示,如果使用 VSCode,请在
settings.json
中添加以下行来修复自行编写的包的 pylances 导入错误。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:
In my
template.yaml
I have the following code snippets: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 callingsam 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.