Eri*_*ans 5 python amazon-web-services aws-lambda aws-lambda-layers
当我创建 AWS Lambda 层时,我的 zip 文件的所有内容/模块都会/opt/在 AWS Lambda 执行时转到。这很容易变得麻烦和令人沮丧,因为我必须对我所有的 lambda 使用绝对导入。例子:
import json
import os
import importlib.util
spec = importlib.util.spec_from_file_location("dynamodb_layer.customer", "/opt/dynamodb_layer/customer.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
def fetch(event, context):
CustomerManager = module.CustomerManager
customer_manager = CustomerManager()
body = customer_manager.list_customers(event["queryStringParameters"]["acquirer"])
response = {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*"
},
"body": json.dumps(body)
}
return response
Run Code Online (Sandbox Code Playgroud)
所以我想知道,是否可以通过 serverless.yml 预先将这些 /opt/paths 添加到 PATH 环境变量中?那样的话,我就可以from dynamodb_layer.customer import CustomerManager,而不是那种怪异的丑陋。
我有 Python3.6 运行时的 Lambda 层。我的 my_package.zip 结构是:
my_package.zip
- python
- lib
- python3.6
- site-packages
- customer
Run Code Online (Sandbox Code Playgroud)
所有依赖项都位于build项目根目录的文件夹中:例如build/python/lib/python3.6/site-packages/customer
我的 serverless.yml 的相关部分
layers:
my_package:
path: build
compatibleRuntimes:
- python3.6
Run Code Online (Sandbox Code Playgroud)
在我的 Lambda 中,我像导入任何其他包一样导入我的包:
import customer
| 归档时间: |
|
| 查看次数: |
1468 次 |
| 最近记录: |