如何最小化lambda函数的权重? [英] How can I minimize the weight of my lambda functions?

查看:106
本文介绍了如何最小化lambda函数的权重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AWS(lambda + s3 + apiGateway)构建微服务API,并且我注意到我所有的lambda都具有相同的权重,因此好像我将整个项目上载到每个lambda而不是所需的一样资源.

I am trying to build a microservice API using AWS (lambda + s3 + apiGateway) and I have noticed that all my lambdas have the same weight, so it seems like I am uploading the full project to every lambda instead of the needed resources.

是否有任何方法可以仅上传每个功能所需的资源?这会减少执行时间吗?值得这样做吗?

Is there any way to upload just the resources I need for each function? Will this minimize the execution time? Is it worth to do it?

推荐答案

要分两部分来回答:

(1)强制性的您为什么在乎?"

(1) The obligatory "Why do you care?"

我问这个是因为我也很担心.但是经过测试后,似乎上传的捆绑包的大小(lambda分发捆绑包的lib文件夹中的jar)似乎并没有真正影响预期的任何内容,例如初始上传时间(或者如果您按照该路线进行,则可能是S3的使用) ).

I ask this because I was really concerned too. But after testing, it doesn't seem like the size of the bundle uploaded (the jars in the lib folder of the lambda distribution bundle) seemed to really affect anything expect maybe initial upload time (or maybe S3 usage if you are going that route).

出于理智的考虑,我没有一个纳米项目和捆绑包,而是拥有一个Java Lambda API模块,然后为每个Lambda上传相同的工件.

For the sake of sanity, rather than having a bunch of nano projects and bundles, I have a single Java Lambda API module and then I upload the same artifact for every Lambda.

在某个时候,如果出于某种原因(微服务架构,代码分离等)分离有意义,那么我打算进行分离.

At some point, if it makes sense to separate for whatever reason (micro service architecture, separation of code, etc), then I plan on splitting.

现在已经说过了,真正影响基于Java的lambda的一件事就是类加载时间.您提到您使用Spring.我建议您不要使用Spring配置加载,因为您可能最终将执行一堆真正不需要的代码.

Now having said that, the one things that REALLY seems to affect Java based lambdas is class loading time. You mentioned you use Spring. I would recommend you not use Spring configuration loading as you will probably end up executing a bunch of code you never really need.

请记住,理想情况下,您的lambda值应在100ms范围内. 我遇到的情况是,我正在使用AWS开发工具包,并且初始化AWSClient花费了13秒! (13000毫秒).当我切换到使用Node的Python时,它花了56毫秒...

Remember, ideally your lambdas should be in the 100ms range. I had a case where I was using the AWS SDK and initializing the AWSClient was taking 13 seconds! (13000 ms). When I switched to using Python of Node, it went to 56ms...

请记住,您是按时间收取费用的,并且1000倍的系数不是一件容易的事:)

Remember that you get charged by time, and a 1000x factor is no laughing matter :)

(2)如果您决定拆分,则建议将gradle分发插件与子项目一起使用,以使每个子项目和子项目zip分发轻巧".我走了这条路,但意识到我真的会很好地拆分我的组件……而且我要么会在项目之间复制配置.或者,如果我创建了项目依赖项,那么我最终将再次捆绑整个依赖项树.

(2) If you've decided on splitting, I'd recommend using the gradle distribution plugin with child projects to make each child project and child project zip distribution "light". I went down this road but realized I would really be splitting my components really fine... and I'd either be duplicating configurations across projects. Or if I made a project dependency, I would simply end up bundling up the entire dependency tree again.

如果您已经知道需要选择什么而无需依靠gradle/maven为您处理依赖项,则可以创建gradle zip任务来创建不同的Lambda分发程序包.

If you already know what you need to cherry pick without relying on gradle / maven to handle the dependencies for you, you can create gradle zip tasks to create different Lambda distribution packages.

AWS文档: http://docs.aws.amazon.com/lambda/latest/dg/create-deployment-pkg-zip-java.html

这篇关于如何最小化lambda函数的权重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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