I am trying to deploy a python lambda with the serverless framework which uses the pydantic module (among other things). I am also using the serverless-python-requirements plugin. I am getting this error:
Running “serverless” from node_modules
{
“errorMessage”: “No module named ‘pydantic_core._pydantic_core’”,
“errorType”: “ModuleNotFoundError”,
“requestId”: “44cde922-0d35-406b-97be-2d02a4e1fc8d”,
“stackTrace”: [
" File "/opt/python/serverless_aws_lambda_sdk/instrument/init.py", line 598, in stub\n return self._handler(user_handler, event, context)\n",
" File "/opt/python/serverless_aws_lambda_sdk/instrument/init.py", line 580, in _handler\n result = user_handler(event, context)\n",
" File "/var/task/serverless_sdk/init.py", line 144, in wrapped_handler\n return user_handler(event, context)\n",
" File "/var/task/s_post_rental_app_from_airtable.py", line 25, in error_handler\n raise e\n",
" File "/var/task/s_post_rental_app_from_airtable.py", line 20, in \n user_handler = serverless_sdk.get_user_handler(‘functions.post_rental_app_from_airtable.handler.lambda_handler’)\n",
" File "/var/task/serverless_sdk/init.py", line 56, in get_user_handler\n user_module = import_module(user_module_name)\n",
" File "/var/lang/lib/python3.10/importlib/init.py", line 126, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n",
" File "", line 1050, in _gcd_import\n",
" File "", line 1027, in _find_and_load\n",
" File "", line 1006, in _find_and_load_unlocked\n",
" File "", line 688, in _load_unlocked\n",
" File "", line 883, in exec_module\n",
" File "", line 241, in _call_with_frames_removed\n",
" File "/var/task/functions/post_rental_app_from_airtable/handler.py", line 9, in \n from pydantic import BaseModel, ValidationError\n",
" File "/var/task/pydantic/init.py", line 3, in \n import pydantic_core\n",
" File "/var/task/pydantic_core/init.py", line 6, in \n from ._pydantic_core import (\n"
]
}
This is my serverless.yml:
[skipping header]
provider:
name: aws
latest supported python by lambda + serverless as of 2023-10-23
runtime: python3.10
NOTE: arm64 may offer better price/performance
architecture: ‘arm64’
stackTags:
Project: reffie
functions:
post_rental_app_from_airtable:
handler: functions.post_rental_app_from_airtable.handler.lambda_handler
events:
- http:
path: /manual-trigger
method: post
plugins:
- serverless-python-requirements
package:
patterns:
# virtualenv
- ‘!env310/’
# unit tests and their data
- '!tests/’
# local node_modules
- ‘!node_modules/’
# python cache
- '!pycache/’
- ‘!**/*.pyc’
# Mac files
- ‘!.DS_Store’
And my requirements.txt:
annotated-types==0.6.0
certifi==2023.7.22
charset-normalizer==3.3.1
greenlet==3.0.0
idna==3.4
inflection==0.5.1
pyairtable==2.1.0.post1
pydantic==2.4.2
pydantic_core==2.10.1
python-dotenv==1.0.0
requests==2.31.0
SQLAlchemy==2.0.22
typing_extensions==4.8.0
urllib3==2.0.7
I tried both with architecture as x86_64 and as arm64. I read online that the error sometimes happens due to a mismatch between the architecture of the lambda and the architecture of the docker container used to package the python modules. Any indication where to start?