简体   繁体   English

为 Python 构建自定义 AWS Lambda 层

[英]Building a custom AWS Lambda Layer for Python

I have a module called custom_module.py which has functions and methods that I would like to use across a few different Lambdas.我有一个名为custom_module.py的模块,它具有我想在几个不同的 Lambda 中使用的函数和方法。 The code looks like this:代码如下所示:

def test():
    return 'Custom module'

I tried to convert this into a Lambda Layer by zipping up this file and using the form to create a layer.我试图通过压缩这个文件并使用表格创建一个层来将它转换成一个 Lambda 层。 Everything seemed to work nicely, and I imported the module into my Lambda for use, like so:一切似乎都运行良好,我将模块导入我的 Lambda 以供使用,如下所示:

import json
from custom_module import test

print('Loading function')

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

Unfortunately, running this returns a "Unable to import module 'lambda_function': No module named 'custom_module'" error.不幸的是,运行它会返回"Unable to import module 'lambda_function': No module named 'custom_module'"错误。 What exactly am I doing wrong here?我在这里到底做错了什么? I have the correct runtimes and architecture specified as well.我也指定了正确的运行时和架构。

Edit编辑

Going by the comment, I tried this file structure:根据评论,我尝试了这个文件结构:

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

I zipped up the layer folder as layer.zip and uploaded it.我将layer文件夹压缩为layer.zip并上传。 Still get the same issue unfortunately.不幸的是,仍然遇到同样的问题。

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 ). 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 )。

You have 2 subfolders - layer and then python .您有 2 个子文件夹 - layer ,然后python

Change your file structure to have your custom_module inside the python folder only.更改您的文件结构,使您的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