问题
This is a follow up question to: This Question
I am trying to upload some shared Python code as a lambda layer to AWS Lambda using the serverless framework.
I had followed @msc's solution (with some modifications):
Project 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
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
Step 3: Install my requirements into the common/python/
folder:
requirements.txt:
pandas==1.0.5
Install requirements to folder:
pip install -r requirements.txt ./common/python/
Project 2
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
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())
Function foo()
runs as expected.
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
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.
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")))
I am struggling to understand Python pathing / folder structure in this serverless framework ... Hopefully some expert can share their solution, thank you.
回答1:
Try this
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())
来源:https://stackoverflow.com/questions/63107494/sharing-python-code-as-a-lambda-layer-using-the-serverless-framework