A Guide: Deploying your Python backend Endpoints on Lambda Functions using Docker
AWS Lambda
Lambda is a serverless computing service . It allows you to run code without provisioning or managing servers. Lambda runs your code only when needed and scales automatically, from a few requests per day to thousands per second.
One of the notable aspects of AWS Lambda is its pricing model. Lambda functions are billed based on the number of requests made to them and the time it takes for the code to execute. There are no charges when your code is not running. This "pay-per-request" pricing model can make Lambda one of the most cost-effective options for certain workloads, especially those with unpredictable usage patterns.
Deploying an end to end fastapi endpoint to AWS Lambda using docker images.
Step 1: Setting up your Fastapi Application
Create an empty folder, then open it with your text editor
Using your text editor cmd terminal, create a python environment
python -m venv venv
Activate the venv environment
venv\Scripts\activate
Create the main.py file and paste the following code
from typing import Union from fastapi import FastAPI from mangum import Mangum app = FastAPI() handler = Mangum(app) @app.get("/") def read_root(): return {"Hello": "World"}
Install the required depencies: fastapi, uvicorn and Mangum using pip
pip install uvicorn[standard] fastapi mangum
Create a requirements.txt file and run the following command in your terminal
pip freeze> requirements.txt
Now run the following command to start the application server
uvicorn main:app --reload
By now, your fastapi application code should be looking like the following
now visit localhost:8000/docs to access your endpoint
Step 2: Containerizing your FastApi Application
Create a Dockerfile file and add the following code
FROM public.ecr.aws/lambda/python:3.11 # Copy the contents of the current directory into the LAMBDA_TASK_ROOT directory COPY . /${LAMBDA_TASK_ROOT} # Install the specified packages RUN pip install -r requirements.txt # Set the CMD to your handler CMD [ "main.handler"]
Run the following command to build your docker container
docker build -t myapp .
Login into your aws management console and navigate to AWS ECR(Elastic Container Registry)
Navigate to Private repositories and click Create Repository, then name it
myapp
Navigate into the repository and click view push commands
copy the commands, paste them in the terminal and run them one by one
Kindly note that on the above commands, you need to have logged into aws cli with your terminal
Visit
logging into aws cli
on how to gain access to ECR from your text editor
Step 3: Creating the lambda function
Visit your AWS management console and navigate to lambda
Click ‘create function’
Select
Container image
give your function a name of your choice
Click on Browse images and select the latest image of the repository you have just created,then click select image.
Click create function
- Make sure your function’s execution role allows your lambda function permission to access AWS resources and services it interacts with.
Step 4: Creating a Function URL
Open your lambda function
Navigate to Configuration,then Function URL
click
Create function URL
On Auth type, chose
NONE
Then click save
Now if you checkyour right side of your lambda console, you will see a link under Function Arn, copy it and paste in your browser
- NOW YOU ARE GOOD TO GO