在多个AWS Lambda中导入python模块 [英] Import a python module in multiple AWS Lambdas

查看:302
本文介绍了在多个AWS Lambda中导入python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个AWS Lambda函数.所有这些功能都使用一些常用的辅助功能.我已将这些帮助程序功能放置在名为helper_functions.py的单独文件中.我想在我的所有AWS Lambda函数中导入此模块.我找不到存储该模块(helper_functions.py)的位置,因此,当我在此模块中进行更改时,不必在Lambda函数中进行任何更改.

I have a couple of AWS Lambda functions. All of those functions use some common helper functions. I have placed these helper functions in a separate file called helper_functions.py. I want to import this module in all of my AWS Lambda functions. I am unable to find a place to store this module (helper_functions.py), so when I make a change in this module I don't have to change anything in my Lambda functions.

我想到的一些选择是:

  1. 在AWS S3上加载模块,然后在每个Lambda中加载它 从S3开始使用该功能. (如果可能的话)

  1. Uploading the module on AWS S3 and then loading it in each Lambda function in the start from S3 and using the functions. (if possible)

编写一些脚本(我还没有弄清楚),将模块和Lambda打包在一起 函数的zip文件中并将其上传到AWS Lambda

Writing some script (which I haven't figured out yet) that packages the module along with the Lambda functions' Python file in a zip and uploads it on AWS Lambda

请提出一个更好的解决方案来管理模块并以更有效的方式导入它.

Please suggest a better solution to manage the module and import it in a much more efficient way.

推荐答案

我为此苦苦挣扎了很长时间.这是我的解决方案(可能会有更好的方法):

I struggled with this for a long time. Here's my solution (there might be a better way):

像这样在文件系统中设置您的助手功能:

setup your helper function in your file system like this:

pathToSomewhere/my_helper/helper_functions.py pathToSomewhere/my_helper/__init__.py pathToSomewhere/setup.py

pathToSomewhere/my_helper/helper_functions.py pathToSomewhere/my_helper/__init__.py pathToSomewhere/setup.py

__init__.py所在的位置:

from .helper_functions import *

setup.py

from setuptools import setup

setup(name='my_helper',
      version='0.10000',
      description='My helper functions',
      url='http://github.com/user/example',
      license='Proprietary',
      author='Null',
      author_email='null@example.com',
      packages=['my_helper'],
      install_requires=['boto3'],
      zip_safe=False)

现在让我们打包my_helper.从pathToSomewhere/运行:

Now let's package up my_helper. From pathToSomewhere/ run:

python setup.py sdist

我假设您已经知道如何创建和上传用于运行lambda函数的虚拟环境.如果没有,让我知道.

I'm assuming you already know how to create and upload a virtual environment for running your lambda function. If not, let me know.

现在让我们将my_helper安装到lambda函数的虚拟环境中.假设您的虚拟环境称为worker_env

Now let's install my_helper into the virtual env of your lambda function. Let's assume your virtual environment is called worker_env

./worker-env/bin/pip install file://pathToSomewhere/my_helper

现在压缩worker-env和您实际的lambda脚本并上传.

Now zip up worker-env and your actual lambda script and upload that.

这篇关于在多个AWS Lambda中导入python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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