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

  1. Create an empty folder, then open it with your text editor

  2. Using your text editor cmd terminal, create a python environment

      python -m venv venv
    
  3. Activate the venv environment

     venv\Scripts\activate
    
  4. 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"}
    
  5. Install the required depencies: fastapi, uvicorn and Mangum using pip

     pip install uvicorn[standard] fastapi mangum
    
  6. Create a requirements.txt file and run the following command in your terminal

      pip freeze> requirements.txt
    
  7. 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

Step 2: Containerizing your FastApi Application

  1. 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"]
    
  2. Run the following command to build your docker container

     docker build -t myapp .
    
  3. Login into your aws management console and navigate to AWS ECR(Elastic Container Registry)

  4. Navigate to Private repositories and click Create Repository, then name it myapp

  5. Navigate into the repository and click view push commands

  6. 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

  1. Visit your AWS management console and navigate to lambda

  2. Click ‘create function’

  3. Select Container image

  4. give your function a name of your choice

  5. Click on Browse images and select the latest image of the repository you have just created,then click select image.

  6. 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

  1. Open your lambda function

  2. Navigate to Configuration,then Function URL

  3. click Create function URL

  4. On Auth type, chose NONE

  5. Then click save

  6. 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