Deploy FastAPI on AWS Lambda | In 9 MINUTES
ฝัง
- เผยแพร่เมื่อ 8 ก.ค. 2023
- FastAPI has taken the Python community by storm with its high performance, ease of use, and automatic validation. In this super-condensed tutorial, we’re marrying FastAPI with the mighty AWS Lambda for serverless bliss!
🛠️ What You'll Learn 🛠️
✅ How to create a simple FastAPI application
✅ How to set up and configure AWS Lambda
✅ Deploying FastAPI on AWS Lambda
✅ Best practices and troubleshooting tips
⚡️Commands from the video:
1. pip3 install -t dependencies -r requirements.txt
2. (cd dependencies; zip ../aws_lambda_artifact.zip -r .)
3. zip aws_lambda_artifact.zip -u main.py
🎥 React + FastAPI: • How to build a React +...
🎥 MongoDB + FastAPI: • Unlocking the Power of...
🎥 MySQL + FastAPI: • How to build a FastAPI...
🎥 PostgreSQL + FastAPI: • How to build a FastAPI...
===========================
🎓 Courses can be found:
FastAPI Beginner & Advanced: www.udemy.com/course/fastapi-...
Learn Chatbot Development: www.udemy.com/course/the-comp...
===========================
🛒 GEAR & RECOMMENDED BOOKS: kit.co/codingwithroby
👨💻 Hi, I am Eric!👨💻
Hey! I’m a seasoned developer and cloud enthusiast. I love sharing my knowledge and helping others to discover the exciting world of programming and cloud technologies. For more tutorials, tips, and tech magic, don’t forget to hit that subscribe button and ring the bell for notifications!
🙏 Support The Channel 🙏
If you found this tutorial helpful, please give it a thumbs up 👍, comment, and share it with your network 🌍
#fastapi #lambda #python
Really great tutorial. Thank you so much for sharing. FYI - to people who may be reading the comments - if you setup your FastApi with .env locally and are running with dotenv, you will need to change all those os.getenv() to ENV_VAR = os.environ['NAME'] in lambda.
Thank you! Also I appreciate you dropping this to help other students 🙂
Thanks, it is a great tutorial. In a short video, you explained the process nicely.
Thank you!
Fantastic tutorial! I really appreciate your work.
Woot Woot!
Thanks Eric for this great tutorial ;
I have a question though; I could not install zip " × python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [9 lines of output]" I tried every possible solution proposed online;
anyone have faced this error while installing this package ?
Thanks
nice video, man, really helpful
Awesome! You are welcome
The Lambda function worked, but when I uploaded the zip file, I got an "internal server error" which I traced to "errorMessage": "Unable to import module 'main': No module named 'pydantic_core._pydantic_core' ". Any ideas?
had the same issue. I retried the video and this time I removed all dependencies in the requirements.txt file besides just fasapi and mangum after pip freeze > requirements.txt and it worked. video mark 1:04
package issue
great video , very high quality also
Much appreciated! I am glad you were able to get value 🙂
This is amazing! Was wondering, what if my FastAPI app has multiple folders and files? How should I create the artifact in that case?
You should still be able to do the exact same thing, but you will want to add the other files along with main.
This actually worked, Gemini was giving me wrong information.
Eric > AGI
Thanks. I appreciate this.
Glad it was helpful!
I am getting "errorMessage": "Unable to import module 'main': No module named 'pydantic_core._pydantic_core'",
pip3 install -r requirements.txt --platform manylinux2014_x86_64 --target=dependencies --implementation cp --python-version 3.10 --only-binary=:all: --upgrade openai
try to use this command 🙂
Hey I'm getting the same problem how you resolved it?
@@deveshmishra6119 how can i resolve
Include architecture in your pip install commands. e.g: pip install \
--platform manylinux2014_x86_64 \
--target=my-lambda-function \
--implementation cp \
--python-version 3.9 \
--only-binary=:all: --upgrade \
pandas
Great Video
I'd recommend using the FastAPI version that is shown in this video, else things might not work, didn't work in my case.
How do I set restrictions so that nobody can hit my api too many times and I get a bill sent by AWS.
If you want to add it in the code you can implement Rate Limiting. One of the most popular libraries for this is: pypi.org/project/ratelimiter/
What if you have multiple routers added to the main app and each router has several endpoints. Does this still work?
as far as I was able to check - yes works without any problem
what if we have another folder . Do i need add folder using this "zip aws_lambda_artifact.zip -u folder_name"
Yup!
That's great!
Thanks! Glad you were able to find value :-)
Wow 🤩
Nice video. Is this something that will interact with a database as well?
Of course! You can do this exact same thing if you are using SQLite but if you are using a different type of database you just need to make sure all the connections are setup beforehand. If you need to see how to connect FastAPI to databases, I have multiple videos for different database you may find helpful 🙂
did you mean to delete `uvicorn` from the generated `requirements.txt` file? that came w/ the install of fastapi?
around 1'30" into the video
@@user-ni5tt8wh2b Below are my requirements
fastapi==0.110.0
mangum==0.17.0
Great video!, quick question: will I be able to use all the endpoints without setting them up in the api gateway?
You can proxy all routes/methods to the lambda using {proxy+}
this is dope but is there any way i could get the windows variation of some of these console lines?
Do the commands not work for Windows?
they should work in Powershell
Easy easy sub. Nice job roby
Woot woot, thanks for the kind words 🙂
@@codingwithroby do you have any info on how to access this from a client side call? Say I want to make a get request with a json payload how would i access this? Is it the function URL? Also what type of auth would it be?
Just curious. Knowing the functionality of Lambda that it is serverless(instances are spawned on request), should I still choose fastapi with Mangum or go with Chalice framework?
I see nothing wrong with using FastAPI with Mangum
hello my friend
what theme are you using in vscode?
One Dark Pro - it is an theme that you need download!
AMAZING tutorial! 👏
Glad you enjoyed it! Woot woot!
I would like to host the frontend files on an S3 bucket, but I'm not finding any tips on how to make this work. Would you have any material on that?@@codingwithroby
Well, just as I wrote the last comment I thought that I can just specify the S3 URL as path for the templates and it will load as if it were on the same local as the app (that in my case will be in lambda)
I get internal server error despite changing my handler details. What could be the reason
same
Thanks!
You bet!
the second command doesn't work in my end. It is giving 1. Missing closing ) in expression and 2. Unexpected token ) in expression or statement
Hmm, I just retried it and it is working correctly. Are you sure there is no typos?
@@codingwithroby Yeah, I noticed it afterwards that I was using powerhell while you were in zsh. I had to use an alternative to that command for powershell
Do you mind sharing what the alternative command was?
@@codingwithroby this is the command for VSCode powershell terminal : "Compress-Archive -Path . -destinationpath ../aws_lambda_artifact.zip"
what if we update our code ? we create and upload a new zip file?
Yes, in this video I didn't go over how to setup lambda deployments automatically. If you want to do this I'd look at GitHub actions to communicate to your lambda
Very well, thanks a lot
You are welcome!
Nice!
Thank you! Cheers!
if i import some files from another file is it zip every thing
Yes, you would want to zip everything
@@codingwithroby file2.py imported into main.py then is it zip all files?
how about larger files? i think you cant upload larger packaged .zip files.
For larger files you will probably not want to do a lambda, but instead use EC2 which I will eventually make a video on
Use lambda layers for dependencies. That way you also get the option to edit the code in the editor on the lambda page. If your lambda gets to the point where without dependencies it exceeds the lambda limit, you are packing too much stuff into it.
Mount a EFS on a EC2, pip install --target with target to the EFS, delete ec2 instance, create a lambda with a mounting to the EFS and make PYTHONPATH as the local on efs you installed your dependencies.
I am doing this for a segmentation model inference application
i followed your video and deploy code zip to lambda when i test i am getting error: "errorMessage": "Unable to import module 'main': No module named 'pydantic_core'"
I'm having the same issue
did you solve this problem? I have the same issue
did you solve this problem? I have the same issue@@KnightDark1233
I found a workaround to this issue, see my post from Feb 9, 2024 in comments. Hope it helps someone else too...
This deployment on aws in this video free or not ?
and also can open /docs or swagger ?
Yes (part of free tier) and yes 🙂
@@codingwithroby I follow your video until deploy successful and open /docs swagger also successful. api can use complete But except api method post api must have parameter/data{} before excute after I input and press excute result error 504 ( This swagger from my code on local have no any problems )
mangum seems like no longer maitained ....?
It has not had a PR in awhile, but it is still widely used.
Kindly make a s3 uploads and trigger using lambda and fast api bro
I will add it to my backlog 🙂
@@codingwithroby and also how to use custom packages like ffmpeg for audio conversation and others , to trigger lambda and use GPU , inhouse models like whisper model how to use them to trigger pls kindly make a video I'm eagerly waiting
Use everything in vscode because cloud developers can't give credentials to developers in company, so kindly make a video for python automatic project in vscode and if possible dockerize integration deploy and aws lambda and Sam
mangum seems abandoned. Last commit was made almost a year ago
Yeah, but Mangum is still the best option for deploying FastAPI on lambdas. When a better handler comes out you can wrap app in it
I am getting Internal Server Error After followed,
Me too, did you fix that?
@@pedroalbertorojasvalladare260 th-cam.com/video/UauMQGqaxGo/w-d-xo.html. I Used CI/CD Piples of Github.
@@pedroalbertorojasvalladare260 If you want upload more than 2.4 GB Lambda function won't work.
@@amarnathreddysurapureddy9111 thanks, I deployed my API on render it's more easily
Check endpoint url and cors maybe
The only issue is their cold start
Magnum is not maintained so I don't we should use it.
As of right now though it is a wrapper that doesn"t need maintained (as of now).
Hi, How are you? first thank you for the tutorial, second, i have troblue when test de app in aws, it still show Internal server Error message, although i edited Handler in console. i wrote main.handler. the only ting that i made differenet was replace this command lines 2. (cd dependencies; zip ../aws_lambda_artifact.zip -r .)
3. zip aws_lambda_artifact.zip -u main.py for
Compress-Archive dependencies/*.* lambda_function2.zip and Compress-Archive main.py -update aws_lambda_artifact.zip
Could you help me?
You need to debug to see what is happening. Maybe it is some incompatibility between lib version supported by aws lambda. What you can try to do is in your requirements.txt, force the version of fast api to 0.99.0 (fastapi==0.99.0) and remove the version of pydantic and pydantic core (so it will get the correct one. When you run locally with uvicorn you will find the right version). This happened to me in another project and might fix the issue.
For the time being, I found the best FastAPI version to be 0.99.0
Thanks all for the answer. I solve the problem changing the py lance versión, i mafe a video in spanish with the solution th-cam.com/video/FUsJmLO2G4g/w-d-xo.htmlsi=z-mQPlbBjFde_3g0
@@codingwithroby hi,
I deployed my api with mysql in lambda but not all of them work, only the delete one and the query by user_id work. vbx54teybuuh3rvjyss67x3j740mnyft.lambda-url.us-east-2.on.aws/docs. could you help me
@@SoulCTO Thank you so much for leaving this comment, I have spent days to figure out what goes wrong. AWS won't give us that much hints to fix the problem. I did not realise it is the fastapi version that making things to fail.
Thanks!
Hey - thank you so much!