Skip to main content

Deploying on AWS

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. In this tutorial, we will cover how one can deploy a FastAPI application on Amazon Web Services (AWS).

Prerequisites

Before you get started, there are a few things you need:

  1. Basic understanding of Python and FastAPI
  2. An AWS account
  3. AWS CLI installed and configured on your local machine
  4. Docker installed on your local machine

Step 1: Create a FastAPI Application

Firstly, let's create a basic FastAPI application. Create a new file main.py and add the following code:

from fastapi import FastAPI

app = FastAPI()

@app.get('/')
def read_root():
return {"Hello": "World"}

Here we've created a basic FastAPI application that responds with {"Hello": "World"} when accessed at the root (/) URL.

Step 2: Dockerize the FastAPI Application

To run our application on AWS, we need to dockerize it. Create a new file Dockerfile in the same directory as main.py and add the following content:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8

COPY ./app /app

This Dockerfile takes a base image equipped with Python, Uvicorn, Gunicorn, and FastAPI, and copies our application code into the image.

Step 3: Build and Push Docker Image to AWS ECR

Now, we need to build our Docker image and push it to the AWS ECR (Elastic Container Registry).

First, navigate to AWS ECR and create a new repository.

Then, build your Docker image using the following command:

docker build -t fastapi-app .

Tag your Docker image with the URI of the ECR repository:

docker tag fastapi-app:latest <your-ecr-repository-uri>:latest

Finally, push your Docker image to the ECR repository:

docker push <your-ecr-repository-uri>:latest

Step 4: Deploy FastAPI Application on AWS Fargate

We will use AWS Fargate to run our Docker containers. It's a serverless compute engine for containers that work with both Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS).

Go to the AWS ECS console and create a new Fargate task definition. For the container definition, use the ECR image URI. Configure the networking, memory, and CPU according to your requirements.

Once the task definition is ready, create a new ECS service using the Fargate launch type and the task definition you just created.

Step 5: Accessing the FastAPI Application

Once the service is up and running, AWS will provide a public IP that you can use to access your FastAPI application. If you navigate to the root URL (/) of this IP in a web browser, you should see the message {"Hello": "World"}.

Conclusion

And that's it! You have successfully deployed a FastAPI application on AWS. This tutorial is a basic introduction, and there's a lot more to explore. AWS provides many features like load balancing, service discovery, secrets management, etc., that you can use to manage and scale your FastAPI applications.


I hope this tutorial helps you understand how to deploy FastAPI applications on AWS. Happy coding!