Hello guys, I’m trying to use Serverless to create a really small mock API to play a little bit with the tool and with AWS Lambdas.
I’m trying to create a layer so I can share a piece of code with the endpoints. I plan to have a lambda function for every major endpoint and share some code using layers, so I’ve created a small project where I have two functions and one layer in the serverless.yml file.
The problem is that when I invoke one of the two functions I get this error from AWS:
{
“errorMessage”: “Unable to import module ‘esg.esg’: No module named ‘shared’”,
“errorType”: “Runtime.ImportModuleError”,
“stackTrace”: []
}
I’m using Python and I’ve tried multiple ways to make the layer, by creating just a Python module (a .py file) or a Python package or even a full Python package that can be installed via pip.
This is my yaml file:
service: sapi-layers
frameworkVersion: ">=1.67.3"
provider:
name: aws
runtime: python3.8
lambdaHashingVersion: 20201221
region: eu-central-1
package:
exclude:
- node_modules/**
- venv/**
- .direnv/**
functions:
esg:
handler: esg.esg.handler
events:
- http:
path: /esg/{proxy+}
method: ANY
layers:
- { Ref: SharedLambdaLayer }
risk:
handler: risk.risk.handler
events:
- http:
path: /risk/{proxy+}
method: ANY
layers:
- { Ref: SharedLambdaLayer }
layers:
shared:
path: shared
compatibleRuntimes:
- python3.8
allowedAccounts:
- "*" # ALL accounts!
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
usePoetry: true
# dockerizePip: non-linux
and here’s the tree output so you can see my dir structure (I’ve omitted node_modules):
.
├── esg
│ ├── esg.py
│ ├── init.py
│ └── pycache
│ ├── esg.cpython-38.pyc
│ └── init.cpython-38.pyc
├── layers
│ └── shared
│ ├── setup.cfg
│ ├── setup.py
│ └── shared
│ ├── init.py
│ └── utils.py
├── package.json
├── package-lock.json
├── poetry.lock
├── pyproject.toml
├── risk
│ ├── init.py
│ └── risk.py
├── serverless.yml
└── shared
├── init.py
└── utils.py
In my Python files I just import the packages normally using from shared.utils import foo
Could you please tell me what’s wrong?
Thanks in advance!