我有几个AWS Lambda函数。所有这些函数都使用一些常见的辅助函数。我已将这些辅助函数放在一个名为helper_functions.py
的单独文件中。我想在所有AWS Lambda函数中导入此模块。我无法找到存储此模块的地方(helper_functions.py
),因此当我在此模块中进行更改时,我不必更改Lambda函数中的任何内容。
我想到的一些选项是:
在AWS S3上上传模块,然后将其加载到每个Lambda中 功能从S3开始并使用功能。 (如果可能的话)
编写一些脚本(我还没想到)将模块与Lambda打包在一起 功能' zip文件中的Python文件并将其上载到AWS Lambda
请建议一个更好的解决方案来管理模块并以更有效的方式导入它。
答案 0 :(得分:2)
我很长时间都在努力。这是我的解决方案(可能有更好的方法):
在您的文件系统中设置辅助函数,如下所示:
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/
运行:
python setup.py sdist
我假设您已经知道如何创建和上传用于运行lambda函数的虚拟环境。如果没有,请告诉我。
现在让我们将my_helper
安装到lambda函数的虚拟环境中。我们假设您的虚拟环境名为worker_env
./worker-env/bin/pip install file://pathToSomewhere/my_helper
现在压缩worker-env
和你的实际lambda脚本并上传它。