Deploying Python Apps to AWS: A Complete Beginner's Guide
โก Quick Answer
A beginner's guide to deploying Python apps to AWS in 2026: EC2, Lambda, Elastic Beanstalk, and free alternatives โ step-by-step with real commands.
Get more content like this on Telegram!
Daily AI tips, notes & resources โ free
Advertisement
Deploying Python Apps to AWS: A Complete Beginner's Guide
Deploying a Python app to AWS means packaging your code so one of AWS's compute services โ EC2, Lambda, or Elastic Beanstalk โ can run it and expose it to the internet. Think of it like moving from cooking in your own kitchen (your laptop) to running a food stall in a shared market (the cloud): the recipe doesn't change, but now other people can walk up and order.
This guide covers the four real deployment paths with working commands, and says plainly when a simpler platform beats AWS.
Before AWS: Should You Use AWS?
AWS is powerful, but it's not always the right choice for beginners. Let me be direct:
Consider Railway, Render, or Fly.io first if:
- You're deploying a hobby project or portfolio piece
- You want to deploy in under 10 minutes without learning AWS
- You don't specifically need AWS for a job or project requirement
Use AWS if:
- Your team or employer uses AWS
- You need specific AWS services (RDS, S3, SQS)
- You're learning AWS for career reasons
- Your app needs to scale significantly
For a portfolio project, Railway or Render gives you a live URL in 5 minutes. AWS gives you the same result in 30โ60 minutes with more configuration. Know your goal before choosing.
Option 1: AWS Lambda (Serverless Python)
AWS Lambda runs Python functions in response to events โ HTTP requests, scheduled triggers, file uploads โ without you managing any server. It behaves like a vending machine: it sits idle costing nothing, then springs to life the instant someone presses a button, and switches off again right after.
Deploying a Simple FastAPI App to Lambda
# main.py
from fastapi import FastAPI
from mangum import Mangum
app = FastAPI()
@app.get("/")
def root():
return {"message": "Running on Lambda"}
@app.get("/hello/{name}")
def hello(name: str):
return {"message": f"Hello, {name}!"}
# Mangum wraps FastAPI for Lambda
handler = Mangum(app)Deployment with AWS SAM
# Install AWS CLI and SAM CLI
pip install aws-sam-cliCreate template.yaml:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
PythonApi:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: main.handler
Runtime: python3.11
MemorySize: 256
Timeout: 30
Events:
Api:
Type: HttpApi
Properties:
Path: /{proxy+}
Method: ANYCreate requirements.txt:
fastapi
mangumDeploy:
sam build
sam deploy --guidedSAM will ask for a stack name, region, and confirmation. After deployment, you'll receive an API URL.
Limitations of Lambda:
- Cold starts (first request after ~5 minutes idle is slower โ 500msโ2s)
- 15 minute maximum execution time
- Local file system is read-only (use S3 for file storage)
- 10GB maximum package size
Option 2: EC2 (Virtual Machine)
Amazon EC2 gives you a full virtual machine that runs around the clock โ the opposite of Lambda's vending-machine model. It's closer to renting an apartment: you get full control of the space, but you're also responsible for setup, maintenance, and the rent whether you use it or not.
Step 1: Launch an EC2 Instance
- Sign into AWS Console โ EC2 โ Launch Instance
- Choose: Ubuntu Server 22.04 LTS (free tier eligible)
- Instance type: t2.micro (free tier)
- Key pair: Create new, download .pem file
- Security group: Allow SSH (port 22) and HTTP (port 80)
- Launch
Step 2: Connect to Your Instance
# Make key file secure
chmod 400 your-key.pem
# Connect
ssh -i your-key.pem ubuntu@your-ec2-public-ipStep 3: Install Python and Dependencies
# Update system
sudo apt update && sudo apt upgrade -y
# Install Python and pip
sudo apt install python3 python3-pip python3-venv nginx -y
# Create app directory
mkdir ~/app && cd ~/app
# Create virtual environment
python3 -m venv venv
source venv/bin/activateStep 4: Deploy Your FastAPI App
# Upload your app (from local machine)
scp -i your-key.pem -r ./app ubuntu@your-ec2-ip:~/app/
# On the server: install dependencies
pip install fastapi uvicorn gunicornStep 5: Configure Gunicorn as a Service
Create /etc/systemd/system/fastapi.service:
[Unit]
Description=FastAPI application
After=network.target
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/app
Environment="PATH=/home/ubuntu/app/venv/bin"
ExecStart=/home/ubuntu/app/venv/bin/gunicorn main:app -w 2 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
[Install]
WantedBy=multi-user.targetsudo systemctl start fastapi
sudo systemctl enable fastapi
sudo systemctl status fastapiStep 6: Configure Nginx as Reverse Proxy
sudo nano /etc/nginx/sites-available/fastapiserver {
listen 80;
server_name your-ec2-public-ip;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}sudo ln -s /etc/nginx/sites-available/fastapi /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxYour app is now accessible at http://your-ec2-public-ip.
Option 3: Elastic Beanstalk (Managed PaaS)
AWS Elastic Beanstalk handles the EC2 setup, load balancing, and deployment for you โ like hiring a property manager for that apartment instead of doing repairs yourself. It costs more than raw EC2, but removes most of the manual configuration.
pip install awsebcliIn your project directory:
eb init -p python-3.11 my-python-app
eb create my-python-env
eb deployElastic Beanstalk expects a application.py or application callable. For FastAPI:
# application.py
from fastapi import FastAPI
from mangum import Mangum
application = FastAPI()
@application.get("/")
def root():
return {"status": "ok"}Option 4: Docker on ECS (Production-Grade)
For production apps, Docker containers running on AWS ECS Fargate are the modern standard โ a Docker image is a shipping container that runs identically on your laptop, in CI, and in the cloud, so "it works on my machine" stops being an excuse.
Create a Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]# Build and push to ECR
aws ecr create-repository --repository-name my-python-app
docker build -t my-python-app .
docker tag my-python-app:latest <account-id>.dkr.ecr.<region>.amazonaws.com/my-python-app:latest
docker push <account-id>.dkr.ecr.<region>.amazonaws.com/my-python-app:latestThen create an ECS Fargate service from the ECR image in the AWS Console.
Environment Variables and Secrets
Never hardcode secrets in code. Use AWS Systems Manager Parameter Store or Secrets Manager:
import boto3
def get_secret(secret_name: str) -> str:
client = boto3.client("secretsmanager")
response = client.get_secret_value(SecretId=secret_name)
return response["SecretString"]
DATABASE_URL = get_secret("my-app/database-url")For Lambda and EC2, environment variables are the simpler approach:
import os
DATABASE_URL = os.environ["DATABASE_URL"]Simpler Alternatives for Beginners
If AWS feels overwhelming, these alternatives are production-ready and developer-friendly:
| Platform | Free Tier | Deploy Time | Best For |
|---|---|---|---|
| Railway | $5/month credit | 5 minutes | Hobby projects, startups |
| Render | 750h/month free | 5 minutes | Web services, static sites |
| Fly.io | Generous free | 10 minutes | Containers, global deployment |
| PythonAnywhere | Free tier | 5 minutes | Simple Python web apps |
For building the FastAPI apps you'll deploy, see our FastAPI tutorial. For OOP patterns that make deployed apps maintainable, our Python OOP guide covers the structure large applications need.
Further Reading
- Python Virtual Environments Guide 2026 โ venv, pip, and Poetry Explained
- 10 Python Projects That Will Get You a Developer Job
- How I Built a Python Bot That Makes $500/Month Passively
- Python vs JavaScript in 2025: Which Should a Beginner Learn First?
- Python + AI: How to Build Your First Machine Learning Model
- Vector Database Guide: Pinecone, Weaviate, Chroma, and pgvector Compared
- Building Real-Time Apps with WebSockets and React
- Computer Vision Tutorial: Build an Image Classifier from Scratch
Advertisement
๐ฌ DiscussionPowered by GitHub Discussions
Frequently Asked Questions
Problem Solver and Cloud Expert
Solves complex infrastructure challenges and architects reliable, scalable cloud deployments for every project. Shamshur Rahman keeps AiTechWorldsโ hosting and cloud infrastructure fast, resilient, and cost-efficient.
Not sure yet? Ask AI about this article
Get an instant, unbiased AI summary of โDeploying Python Apps to AWS: A Complete Beginner's Guideโ.
Advertisement
Related Articles
The Python Libraries Every Developer Must Know in 2026
The essential Python libraries for 2026: from requests and pandas to FastAPI and LangChain โ what each does, when to use it, and how to get started quickly.
Django vs Flask in 2026: Which Framework Should You Learn?
An honest Django vs Flask comparison for 2026 โ which Python framework to learn first, when each excels, and why FastAPI has changed the equation.
FastAPI Tutorial: Building Your First REST API in 30 Minutes
A hands-on FastAPI tutorial for beginners: build a fully functional REST API in 30 minutes with CRUD endpoints, request validation, and automatic docs.
Jupyter Notebook Guide: The Data Scientist's Favorite Tool
A complete Jupyter Notebook guide for 2026: installation, essential shortcuts, best practices, and how data scientists use Jupyter for exploration, analysis, and sharing.