How to Deploy a Python FastAPI Application to an EC2 Instance Using Amazon ECR

H

Hassan Shahzad

Guest
Deploying a Python FastAPI application on the cloud ensures scalability, reliability, and performance. Amazon Web Services (AWS) provides powerful tools like Amazon EC2 (Elastic Compute Cloud) and Amazon ECR (Elastic Container Registry) to simplify the deployment process using Docker containers.

In this guide, we’ll walk you through step-by-step instructions to build, containerize, push, and run your FastAPI app on an AWS EC2 instance using ECR.

Why Use FastAPI with AWS EC2 and ECR?​


FastAPI is one of the fastest-growing Python web frameworks because of its speed, simplicity, and built-in async support. Pairing it with AWS services like ECR and EC2 makes it easy to:

  • Store and manage Docker images securely in ECR.
  • Scale and run containerized applications on EC2.
  • Maintain flexibility with environment variables and networking configurations.
  • Set up a production-ready deployment with Nginx and custom domains.

Step 1: Prepare AWS CLI and Authenticate​

Install AWS CLI​


On your local machine, install the AWS CLI:


Code:
sudo apt update && sudo apt install awscli -y

Configure AWS CLI​


Enter your AWS credentials:


Code:
aws configure

Provide:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region name
  • Output format (optional)

Authenticate Docker with ECR​


Run the login command (replace <region> and <account_id>):


Code:
aws ecr get-login-password --region <region> \
| docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com

Step 2: Push the Docker Image to Amazon ECR​

Build the Docker Image​


Code:
docker build -t <repository_name>:latest .

Tag the Image for ECR​


Code:
docker tag <repository_name>:latest <account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:latest

Push the Image​


Code:
docker push <account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:latest

Step 3: Launch and Configure the EC2 Instance​

  1. Launch an EC2 instance with desired CPU, memory, and storage.
  2. Attach a security group that allows:
  • SSH (TCP 22) β†’ from your IP
  • HTTP (TCP 80) β†’ from 0.0.0.0/0 (public access)

Step 4: Set Up the EC2 Instance​

Connect to EC2​


Code:
ssh -i <key_pair.pem> ec2-user@<public-ip>

Install Docker​


Code:
sudo apt update
sudo apt install docker.io -y
sudo systemctl enable docker
sudo systemctl start docker

Install AWS CLI (if not installed)​


Code:
sudo apt install awscli -y

Step 5: Pull and Run the Docker Image​

Authenticate Docker with ECR​


Run the same login command used on your local machine.

Pull the Image​


Code:
docker pull <account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:latest

Run the FastAPI Container​


Code:
docker run -d \
  --name fastapi_app \
  -p 80:8000 \
  -e FERNET_KEY="your_fernet_key" \
  -e DATABASE_URL="your_database_url" \
  <account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:latest

Step 6 (Optional): Set Up a Domain and Nginx​


If you want to serve your app on a custom domain:

  1. Point your domain’s DNS A record to the EC2 public IP.
  2. Install Nginx:

Code:
   sudo apt install nginx -y
  1. Configure Nginx as a reverse proxy to your FastAPI app.
  2. Restart Nginx:

Code:
   sudo systemctl restart nginx

Step 7: Verify the Deployment​


Visit:


Code:
http://<EC2_PUBLIC_IP>/docs

You should see the FastAPI Swagger UI.

βœ… If not, double-check:

  • EC2 security group rules
  • Docker container logs
  • Nginx configuration (if using a domain)

Conclusion​


Deploying a Python FastAPI application on AWS EC2 with ECR gives you a secure, scalable, and production-ready environment. With Docker, you can ensure consistency across development and production.

Whether you’re building an MVP or scaling a SaaS application, this setup allows you to:

  • Manage images easily with ECR.
  • Deploy containers on demand with EC2.
  • Optionally enhance your deployment with Nginx and custom domains.

Now it’s your turn: Have you deployed FastAPI on AWS before? What challenges did you face? Share your experience in the comments!



Continue reading...
 


Join 𝕋𝕄𝕋 on Telegram
Channel PREVIEW:
Back
Top