繁体   English   中英

为 Python 构建自定义 AWS Lambda 层

[英]Building a custom AWS Lambda Layer for Python

我有一个名为custom_module.py的模块,它具有我想在几个不同的 Lambda 中使用的函数和方法。 代码如下所示:

def test():
    return 'Custom module'

我试图通过压缩这个文件并使用表格创建一个层来将它转换成一个 Lambda 层。 一切似乎都运行良好,我将模块导入我的 Lambda 以供使用,如下所示:

import json
from custom_module import test

print('Loading function')

def lambda_handler(event, context):
    return test()

不幸的是,运行它会返回"Unable to import module 'lambda_function': No module named 'custom_module'"错误。 我在这里到底做错了什么? 我也指定了正确的运行时和架构。

编辑

根据评论,我尝试了这个文件结构:

layer
|
+--- python
     |
     +--- custom_module
          |
          +--- __init__.py (contains the test() method)

我将layer文件夹压缩为layer.zip并上传。 不幸的是,仍然遇到同样的问题。

As per the docs , for Python runtimes, the Lambda layer should only have 1 subfolder called python so that the Lambda can access the layer content without the need to specify the path (or it needs to be a site package within python/lib/python3.9/site-packages )。

您有 2 个子文件夹 - layer ,然后python

更改您的文件结构,使您的custom_module仅位于python文件夹中。

layer.zip
|
+--- python
     |
     +--- custom_module
          |
          +--- __init__.py (contains the test() method)

暂无
暂无

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

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