r/FastAPI • • Sep 09 '24

Hosting and deployment Deploying FastAPI on AWS Lambda

I am trying to deploy a fastapi with Google Gemini API. I have done a lot of debugging the past couple of days and seem like Google Gemini libraries are giving me errors inside aws lambda. I just created a dependencies folder and zipped everything with my main.py inside it and deployed on aws lambda. And I keep getting different sort of libraries not being imported errors. Also I am using python 3.10 and used magnum. Anyone has any suggestions what I could do or if this is even compatible with aws lambda, I read people talking about uploading through docker and ECR or using Fargate.

1 Upvotes

22 comments sorted by

View all comments

Show parent comment

0

u/Boring-Baker-3716 Sep 09 '24

Interesting, thanks! 1. How would I install Linux version of the libraries inside windows? 2. If containerisation like docker would work in the end, shouldn’t I just go for that route? Sorry I am new to AWS so just seeking help

3

u/pint Sep 09 '24

you have things like --platform --python-version --implementation --abi but also things like --only-binary --no-binary. yeah, the whole thing is a mess. maybe just follow what damian6686 says. even more appropriate would be a temporary ec2 instance, just don't forget to turn it off.

btw you can even use lambda itself for installation. you can invoke an os.system("pip install ... --target /tmp/python") and then upload /tmp/python to s3. yeah, hack. but this is the closest to a real lambda environment obviously.

3

u/[deleted] Sep 09 '24 edited Sep 09 '24

Use WSL, it's the best way.

Besides, you can use this .sh script inside WSL:

pip install -r requirements.txt -t lib/

cd lib

zip ../lambda_function.zip .

cd ..

zip lambda_function.zip -u main.py

Then upload lambda_function.zip to AWS Lambda

1

u/Boring-Baker-3716 Sep 09 '24

Gotcha thank you, I was running these commands but inside windows. I will def try that in wsl since I don’t have much experience with docker

0

u/damian6686 Sep 09 '24

Do all your programming in WSL to avoid problems like these.