Search code examples
pythonamazon-web-servicesaws-lambdafastapipython-3.12

Unable to import module 'main': No module named 'pydantic_core._pydantic_core when deploying a FastAPI app to AWS Lambda


I am working on my MacOS machine following this tutorial.

I am trying to deploy my FastAPI app to AWS Lambda.

I launched pip install fastapi uvicorn mangum

and these are the dependencies I get:

Package           Version
----------------- -------
annotated-types   0.7.0
anyio             4.4.0
click             8.1.7
fastapi           0.114.1
h11               0.14.0
idna              3.8
mangum            0.17.0
pip               24.2
pydantic          2.9.1
pydantic_core     2.23.3
sniffio           1.3.1
starlette         0.38.5
typing_extensions 4.12.2
uvicorn           0.30.6

Then leaving only fastapi and mangum, as the guy in the tutorial says:

fastapi==0.114.1
mangum==0.17.0

Then I created my app:

from fastapi import FastAPI
from mangum import Mangum

app = FastAPI()
handler = Mangum(app)

@app.get("/")
async def hello():
    return {"message": "Hello World"}

and ran:

 pip3 install -t dependencies -r requirements.txt 

and then:

(cd dependencies; zip ../aws_lambda_artifact.zip -r .)

and finally:

zip aws_lambda_artifact.zip -u main.py 

Then I uploaded the zip file into AWS Lambda but I am getting:

{
  "errorMessage": "Unable to import module 'main': No module named 'pydantic_core._pydantic_core'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "",
  "stackTrace": []
}

This is the runtime environment:

env

Can you help me? Both Lambda and my Mac are running ARM64.


Solution

  • Delete the old lambda and use a new one:

     pip install --platform manylinux2014_x86_64 --target=python --implementation cp --python-version 3.10 --only-binary=:all: --upgrade openai
    

    zip the contents as per:

    https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-native-libraries

    Here is the detail discussion: https://github.com/pydantic/pydantic/issues/6557#issuecomment-1937751778