DEV Community

Hassan Shahzad
Hassan Shahzad

Posted on

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

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:

sudo apt update && sudo apt install awscli -y
Enter fullscreen mode Exit fullscreen mode

Configure AWS CLI

Enter your AWS credentials:

aws configure
Enter fullscreen mode Exit fullscreen mode

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>):

aws ecr get-login-password --region <region> \
| docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

Step 2: Push the Docker Image to Amazon ECR

Build the Docker Image

docker build -t <repository_name>:latest .
Enter fullscreen mode Exit fullscreen mode

Tag the Image for ECR

docker tag <repository_name>:latest <account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:latest
Enter fullscreen mode Exit fullscreen mode

Push the Image

docker push <account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:latest
Enter fullscreen mode Exit fullscreen mode

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

ssh -i <key_pair.pem> ec2-user@<public-ip>
Enter fullscreen mode Exit fullscreen mode

Install Docker

sudo apt update
sudo apt install docker.io -y
sudo systemctl enable docker
sudo systemctl start docker
Enter fullscreen mode Exit fullscreen mode

Install AWS CLI (if not installed)

sudo apt install awscli -y
Enter fullscreen mode Exit fullscreen mode

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

docker pull <account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:latest
Enter fullscreen mode Exit fullscreen mode

Run the FastAPI Container

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
Enter fullscreen mode Exit fullscreen mode

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:
   sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode
  1. Configure Nginx as a reverse proxy to your FastAPI app.
  2. Restart Nginx:
   sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify the Deployment

Visit:

http://<EC2_PUBLIC_IP>/docs
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (2)

Collapse
 
vatul16 profile image
Atul Vishwakarma

If running a docker container then AWS ECS with Fargate will be a good choice…

Collapse
 
hassan_shahzad_086a39f774 profile image
Hassan Shahzad

agreed but i have been free tier to just practice the resource as ECS is on paid aws with fargate feature so that why i go with ec2 just to keep it simple