簡體   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