AWS Builder Center
Scaling Python APIs: Deploying a Dockerized FastAPI Application on AWS App Runner

Scaling Python APIs: Deploying a Dockerized FastAPI Application on AWS App Runner

Disclaimer: This content is intended strictly for educational purposes. The configurations demonstrated here are simplified for learning and will require adaptation such as configuring strict IAM roles and VPC networking before being utilized in a production environment.

ml/ai engineer
As a student developer and the Director of Operations for a university AWS student builder group, I am frequently asked how to get a web application online quickly without getting bogged down in server management. When you need to host a stateless API, AWS App Runner is a fantastic, fully managed solution that handles the infrastructure for you.
In this guide, we will walk through the process of writing, containerizing, and deploying a Python microservice.
Prerequisites Before diving in, ensure you have the following ready:
An active AWS account.
Docker installed on your local machine.
The AWS CLI installed and configured with your credentials.
1.Building the Application First, let's create a lightweight web API. Save the following Python snippet as main.py. This code initializes a basic FastAPI server that returns a simple JSON response upon receiving a web request.
Python from fastapi import FastAPI
app = FastAPI()
@app.get("/") def read_root(): return {"message": "Hello from AWS App Runner!"}
2. Containerizing the API To ensure our application runs consistently in the cloud, we need to package it. Create a Dockerfile in the same directory as your Python script. This file outlines the exact environment your API needs to execute successfully.
Dockerfile FROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . EXPOSE 8000 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
3. Deploying to AWS Once you have built your Docker image and pushed it to an Amazon Elastic Container Registry (ECR) repository, you can deploy it directly from your terminal. Here is the AWS CLI command to create and launch your App Runner service:
Bash aws apprunner create-service --service-name my-fastapi-service --source-configuration '{"ImageRepository": {"ImageIdentifier": "YOUR_ACCOUNT_ID.dkr.ecr.REGION.amazonaws.com/your-repo:latest", "ImageRepositoryType": "ECR"}}'
Conclusion By leveraging AWS App Runner, you can bypass the heavy lifting of infrastructure provisioning and focus entirely on writing great code. I hope this quick walkthrough helps you deploy your first microservice to the cloud!
Written by a passionate AWS Builder and student community leader.
#AWS #AWSAppRunner #FastAPI #Python #Docker
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article