Initial Setup and Dependencies
Hey friends, and in this video, we are going to go over how to deploy a FastAPI application on AWS Lambda step by step. Alright, so once you open up your Visual Studio Code, the very first thing we need to do is install FastAPI, Uvicorn, and Mangum. And Mangum is going to be the handler that we're going to be using on our AWS Lambdas so we can successfully deploy this application.
So let's start by saying pip install fastapi uvicorn and mangum. Now, once we install these dependencies, we need to create a requirements.txt file. And we can do this by saying pip freeze > requirements.txt.
When you do this, you'll see that it's going to create a requirements.txt file. And now when you open this up, you're going to see a bunch of dependencies that come automatically with FastAPI. And all we really need is FastAPI and this Mangum. Everything else can be completely erased. And that's because we are going to grab all the dependencies that we've already installed and stick it on our FastAPI Lambdas.
Building the FastAPI Application
Now the next thing we need to do is go ahead and just create our main.py file, which is our main FastAPI application file, where we can say from fastapi import FastAPI. And then we want to say from mangum import Mangum. We then want to say app = FastAPI(). We then need to create a handler, so we can say handler = Mangum(app). And then we want to say @app.get('/') where inside I'm just going to have an empty slash, where we are going to create a new Python function of async def hello(): where we just return a dictionary of message and I'm going to say, "Hello from coding with Roby."
All right, so this is everything we need to run a FastAPI application. Now, just to make sure everything works, I'm going to say uvicorn main:app --reload. We can see that everything is working as intended and if we open this up, we can see that we get our hello API that when executed, we get a message of "Hello from coding with Roby." So everything is perfect from there.
Packaging the Application for AWS Lambda
Now the next thing that we need to do to get this ready is we need to type a couple commands to get this ready to be deployed on AWS Lambdas. Now I have them on another screen just so I don't mess up. But what we need to do is jump into our terminal and type pip3 install -t dependencies -r requirements.txt.
Now when you click this, we're going to get a new dependencies directory that has all of the dependencies that we installed into our FastAPI application. And we need to bundle this with our main.py file so we can successfully add it to a AWS Lambda. So then the Lambda can take over everything when running the application.
So after typing in that command of creating this dependencies package, we need to come here and also type in this new command of (cd dependencies; zip ../aws-lambda-artifact.zip -r . ). When you do this, you will see that we now have a aws-lambda-artifact.zip file in our directory. Now for the very last command, we need to say zip aws-lambda-artifact.zip -u main.py. What this is doing is we are creating a new AWS Lambda artifact where we're adding the -u means update. We are adding and updating the zip file with our main.py file.
So let's go ahead and add that and we can see that it's added our main.py file to our AWS Lambda artifact.
Creating the Lambda Function in AWS
Now what we need to do is head over to your AWS console. If you don't already have an AWS account, you need to go ahead and create this, but this Lambda is going to be totally free if you are brand new to AWS using the free tier. So let's go ahead and type into the search bar, "Lambda".
We want to create a function. And here we can name it whatever we want. So I'm going to name this FastAPI coding with Roby.
We want to change the runtime environment from node.js to Python. We can leave the architecture at this.
And now let's go down to our advanced settings and let's enable the function URL. So use function URLs to assign HTTPS endpoints to our Lambda functions. And then where it says auth type, for this example, go ahead and change your auth type to None. This will add a new rule, but what we're essentially saying is anyone can access your AWS Lambda. Now the reason I'm doing this is so we don't have to authenticate and use authorization to make sure that this Lambda is successfully working. So I'm going to say None, so anyone has access to this Lambda. And then I'm going to say create function.
Uploading the Code and Initial Deployment
Now one powerful thing about AWS is it creates these serverless functions extremely fast. This should only take a couple seconds to maybe a few minutes. All right, so once you create a AWS Lambda, it'll give you a URL immediately. So we can say open up a new tab where we get a "hello from Lambda."
Now what this essentially is saying is automatically in our Lambda, when you create a new AWS Lambda, you get this JSON that just returns a 200 and a body of "hello from Lambda". So that's exactly what it's printing right here. So if you scroll a little bit down, you'll see the file. So now what we want to do is upload our FastAPI application to be at AWS Lambda. So let's go ahead and just say upload from, we want to say a .zip file. Click upload. And what we want to go to now is our directory where we just built the application and click our AWS Lambda artifact. Once you upload this, go ahead and click save. And this will have all of the dependencies that we bundled together using those three commands so our Lambda will successfully be able to work.
So the very first thing we can see is that it says success. But if we go back to our Lambda and we refresh, we're going to get an internal server error.
Configuring the Handler and Final Deployment
Now the reason we're getting an internal server error is due to our handler. So remember, we downloaded Mangum to control our FastAPI handler, but we're getting a default lambda_function.lambda_handler. So what we need to do here is go ahead and click edit. And this is on our homepage, I'm just scrolling down. Click edit under runtime settings. And instead of lambda_function.lambda_handler, we need to specify where our handler lives within our FastAPI application.
So we can say main because main is our main file and our handler, our Mangum, was inside main. So we can say main.handler. Now what this is saying is inside our main file, we have our handler variable which is controlling our app.
So if we go ahead and just save this, wait for the updating to happen. It's successfully updated. And now if we refresh the page, we're going to get message "Hello from coding with Roby" on a HTTPS route using AWS Lambdas. Now this is unbelievable how fast you can do this, and I hope you enjoyed this video on how to deploy FastAPI on AWS Lambdas, and I will see you in the next video.