简体   繁体   English

适用于AWS Lambda函数的Python软件包管理

[英]Python package management for AWS lambda function

I'm struggling to understand how best to manage python packages to get zipped up for an AWS lambda function. 我正在努力了解如何最好地管理python软件包以压缩AWS Lambda函数。

In my project folder I have a number of .py files. 在我的项目文件夹中,我有许多.py文件。 As part of my build process I zip these up and use the AWS APIs to create and publish my lambda function supplying the zip file as part of that call. 作为构建过程的一部分,我将它们压缩并使用AWS API创建和发布我的lambda函数,该函数在该调用中提供了zip文件。

Therefore, it is my belief that I need to have all the packages my lambda is dependant on within my project folder. 因此,我认为我需要将lambda所依赖的所有软件包都放在我的项目文件夹中。

With that in mind, I call pip as follows: 考虑到这一点,我将pip称为:

pip install -t . tzlocal

This seems to fill my project folder with lots of stuff and I'm unsure if all of it needs to get zipped up into my lambda function deployment eg 这似乎使我的项目文件夹充满了很多东西,我不确定是否需要将其全部压缩到我的lambda函数部署中,例如

.\pytz
.\pytz-2018.4.dist-info
.\tzlocal
...
...

First question - does all of this stuff need to be zipped up into my lambda? 第一个问题-是否需要将所有这些东西都压缩到我的lambda中?

If not, how do I get a package that gives me just the bits I need to go into my zip file? 如果没有,我如何获得一个打包文件,该打包文件仅提供进入zip文件所需的位?

Coming from a .Net / Node background - with the former I NuGet my package in and it goes into a nice packages folder containing just the .dll file I need which I then reference into my project. 来自.Net / Node背景-使用前一个Nu Nu获取我的包,它进入一个不错的packages文件夹,其中仅包含我需要的.dll文件,然后将其引用到我的项目中。

If I do need all of these files is there a way to "put" them somewhere more tidy - like in a packages folder? 如果我确实需要所有这些文件,是否可以将它们“整洁”地放置在更整洁的位置(例如在packages文件夹中)?

Finally, is there a way to just download the binary that I actually need? 最后,有没有办法下载我实际需要的二进制文件? I've read the problem here is that the Lambda function will need a different binary to the one I use on my desktop development environment (Windows) so not sure how to solve that problem either. 我在这里已经读到问题了,Lambda函数将需要与我在桌面开发环境(Windows)上使用的二进制文件不同的二进制文件,因此也不确定如何解决该问题。

Binary libraries used for example by numpy should be compiled on AWS Linux to work on lambda. 例如, numpy使用的二进制库应在AWS Linux上编译以在lambda上运行。 I find this tutorial useful ( https://serverlesscode.com/post/deploy-scikitlearn-on-lamba/ ). 我发现本教程很有用( https://serverlesscode.com/post/deploy-scikitlearn-on-lamba/ )。 There is even newer version of it which uses docker container so you do not need EC2 instance for compilation and you can do everything locally. 它甚至有使用Docker容器的较新版本,因此您不需要EC2实例进行编译,并且可以在本地进行所有操作。

As for the packages: the AWS docs says to install everything to the root, but you may install them all in a ./packages directory and append it to path in the beginning if the lambda handler code 至于软件包:AWS文档说将所有内容安装到根目录,但是您可以将它们全部安装在./packages目录中,如果有lambda处理程序代码,则可以将其附加到开头的路径中

import os
import sys

cwd = os.getcwd()
package_path = os.path.join(cwd, 'packages')
sys.path.append(package_path)

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

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