Serverless - 封装的功能包含其他功能

问题描述 投票:0回答:1

在 lambda 封装大小方面遇到困难,并且找不到任何可行的解决方案。
我查看了这些包,想知道为什么一个实际上只返回事件的函数压缩后大小为 67,175 KB,并发现它还包含所有其他函数为什么??

我拿走了

package: individually: true
,它使整个包大小为 67,161 KB,但我仍然在 function2 上收到太大的错误消息。我还能做什么来减少这些功能的封装?

My_application
|-function1.py
|-function2.py
|-reallySmallFunction.py
|-sharedUtility.py
|-requirements.txt
|-serverless.yml
|-.serverless
|--function1.zip
|--function2.zip
|--reallySmallFunction.zip
|---function1.py
|---function2.py
|---reallySmallFunction.py
|---sharedUtility.py

无服务器.yml -

> service: My_application
> 
> provider:   
>   name: aws   
>   runtime: python3.8
>   stage: dev
>   region: us-east-1
>   versionFunctions: false
>   apiName: Foo
>   timeout: 10

> package:   
>   individually: true
>   patterns:
>     - "!.cache/**"
>     - "!.pytest_cache/**"
>     - "!.env/**"
>     - "!env/*"
>     - "!venv/*"
>     - "!.venv/**"
>     - "!.git/**"
>     - "!.serverless/**"
>     - "!node_modules/**"
>     - "!unit_tests/**"
>     - "!integration_tests/**"
>     - "!README.md"
>     - "!package.json"
>     - "!package-lock.json"

> functions:   
>   function1:
>     handler: function1.function1
>     iamRoleStatementsInherit: true
>     iamRoleStatements: # as needed
>     timeout: 20
> 
>   function2:
>     handler: function2.function2
>     iamRoleStatementsInherit: true
>     iamRoleStatements: # as needed
>     timeout: 20
> 
>   reallySmallFunction:
>     handler: reallySmallFunction.reallySmallFunction
>     iamRoleStatementsInherit: true
>     iamRoleStatements:
>     timeout: 20
> 
> plugins:
>   - serverless-python-requirements
>   - serverless-iam-roles-per-function
>   - serverless-prune-versions
> 
> custom:   
>   pythonRequirements:
>     zip: false
>     slim: true
>     noDeploy:
>       - pytest
>   prune:
>     automatic: true
>     includeLayers: true
>     number: 5
aws-lambda serverless-framework
1个回答
0
投票

无服务器框架不知道堆栈中的哪些函数需要哪些依赖项,除非您显式声明它。无服务器不执行任何捆绑/treeshaking,不创建抽象语法树,也不对函数代码进行任何类型的编译。

默认情况下,它为每个堆栈创建一个包并将其上传到每个函数。您还可以单独打包,然后每个函数都可以声明自己的包含/排除打包配置。

这在文档中有所介绍,并且已经在论坛上讨论了采用此策略的一些策略。

我建议特别在您的

verySmallFunction
中包含或排除依赖项。

您还可以考虑将这些函数完全分离到不同的堆栈中。这不会花费额外的钱,因为 AWS 不会根据部署的 Lambda 函数数量或 CloudFormation 堆栈的数量来定价。

© www.soinside.com 2019 - 2024. All rights reserved.