使用无服务器框架将 Python 代码作为 lambda 层共享
这是一个后续问题:这个问题
我正在尝试使用无服务器框架将一些共享 Python 代码作为 lambda 层上传到 AWS Lambda.
I am trying to upload some shared Python code as a lambda layer to AWS Lambda using the serverless framework.
我遵循了@msc 的解决方案(做了一些修改):
I had followed @msc's solution (with some modifications):
第 1 步:创建一个具有以下结构的无服务器项目:
Step 1: Create a serverless project with the following structure:
./
└ serverless.yml
└ common/
└ python/
└ Other packages as per requirements.txt
└ my_shared_script.py - my own package
第 2 步:设置我的 .yml
文件:
Step 2: Setup my .yml
file:
service: library
provider:
name: aws
runtime: python3.7
stage: dev
region: ap-northeast-1
layers:
Common:
path: common
resources:
Outputs:
CommonLayerExport:
Value:
Ref: CommonLambdaLayer
Export:
Name: CommonLambdaLayer
第 3 步:将我的需求安装到 common/python/
文件夹中:
Step 3: Install my requirements into the common/python/
folder:
requirements.txt:
requirements.txt:
pandas==1.0.5
安装要求到文件夹:pip install -r requirements.txt ./common/python/
步骤 1:在我的 .yml
文件中引用来自项目 1 的上传层:
Step 1: Reference the uploaded layer as from Project 1 in my .yml
file:
functions:
hello:
handler: handler.hello
layers:
- arn:aws:lambda:ap-southeast-1:182739821739:layer:Common:1
第 2 步:现在所有导入都可以使用了.来自我的 handler.py
:
Step 2: All imports now work. From my handler.py
:
import pandas
import my_shared_script
def hello(event, context):
print(my_shared_script.foo())
函数 foo()
按预期运行.
我现在的问题是 my_shared_script.py
现在与所有已安装的 Python 包位于同一个文件夹中(有很多文件和文件夹),这使得很难找到和维护我的自己的模块/脚本.
My question now is that my_shared_script.py
is now in the same folder as all the installed Python packages (there are a lot of files and folders), which makes it hard to find and maintain my own modules / scripts.
./
└ serverless.yml
└ common/
└ python/
└ Other packages as per requirements.txt
└ lib/
└ my_shared_script.py - my own package
将我自己的脚本放在单独的文件夹 /lib
中并将所有其他要求安装到 /python
文件夹中是有意义的.
It makes sense for me to put my own scripts in a separate folder /lib
and install all other requirements to the /python
folder.
但是,如果我使用上面的文件夹结构,代码就会失败.
However, if I use the above folder structure, the code fails.
我已经把它放在我在项目 2 中 handler.py
的顶部,希望脚本能够在 /lib
中找到我的代码.>
I have put this at the top of my handler.py
in Project 2, hoping that the script would be able to find my code in /lib
.
import os
import sys
CWD = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.abspath(os.path.join(CWD, "../lib")))
我正在努力理解这个无服务器框架中的 Python 路径/文件夹结构......希望有专家可以分享他们的解决方案,谢谢.
I am struggling to understand Python pathing / folder structure in this serverless framework ... Hopefully some expert can share their solution, thank you.
试试这个
import pandas
import sys
try:
sys.path.index('/opt/lib')
except ValueError:
sys.path.append('/opt/lib')
import my_shared_script
def hello(event, context):
print(my_shared_script.foo())