- Courses
- πLibraries & Frameworks
- β‘FastAPI
- Deployment and Monitoring
- Final Project: Production-Grade Task Manager API
On this page
Deploying FastAPI to the Cloud β Render, Fly.io, and AWS
Published on August 17, 2026
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
A containerized, tested API sitting in a CI/CD pipeline still isn't reachable by anyone outside the machine building it β the final step is getting it running on a real server, on the public internet, in front of real traffic. This walks through three deployment targets β Render, Fly.io, and AWS ECS β with the exact configuration files and commands each one needs, followed by the complete production architecture, load testing, and a concrete set of next steps once the API is live. Every step below shows exactly what to type and what to expect; nothing is left as an exercise to figure out independently.
info
This exercise is the continuation of hands-on exercises compelted in the previous modules. Be sure to check the previous parts to follow along:
- Part 1
- Part 2
- Part 3
- Part 4
What Cloud Deployment Addsβ
Running docker-compose up on a local machine produces an API reachable only at localhost. Cloud deployment changes that in four concrete ways: the API becomes reachable at a public URL from any device, the server keeps running once a laptop closes, a managed platform handles TLS certificates, DNS routing, and infrastructure updates, and auto-scaling adds or removes capacity automatically as traffic changes.
The Docker image already built and pushed to a registry by a CI/CD pipeline is exactly what gets deployed to every platform covered below β none of the three options require rebuilding the image differently for their specific environment.
Option A: Renderβ
Render is the lowest-friction path to a running FastAPI container in production. It connects directly to a GitHub repository, builds the Docker image from the Dockerfile, and redeploys automatically on every push to the configured branch.
The $PORT requirementβ
Render assigns a dynamic port to each service and routes external HTTPS traffic to it. The single most common Render deployment failure is hardcoding a port β Render sets a $PORT environment variable at runtime, and the start command has to actually read it. A service listening on a fixed port like 80 or 8000 regardless of what Render assigns returns 502 Bad Gateway on every single request.
The Dockerfile exposes the container's own port normally:
EXPOSE 8000
CMD ["fastapi", "run", "app/main.py", "--host", "0.0.0.0", "--port", "8000"]
On Render itself, the start command gets overridden in the service settings to read $PORT instead:
fastapi run app/main.py --host 0.0.0.0 --port $PORT
Deploying through the Render dashboardβ
Push the code and Dockerfile to a GitHub repository. Log in at dashboard.render.com. Click New β Web Service, connect the GitHub account, and select the repository. Configure the service with Environment set to Docker, Region set to whichever is closest to the expected user base, Branch set to main, and Dockerfile path set to ./Dockerfile. Under Environment, add DATABASE_URL (the PostgreSQL connection string), JWT_SECRET_KEY (the production secret), and DEBUG set to false. Set the Health Check Path to /health. Click Create Web Service.
Render builds the image, starts the container, and assigns an onrender.com subdomain automatically. Every subsequent push to main triggers a new deploy with zero downtime.
Deploying through render.yaml (Infrastructure as Code)β
For a repeatable, version-controlled deployment, a render.yaml Blueprint file at the repository root gets detected and applied by Render automatically:
services:
- type: web
name: taskmanager-api
env: docker
dockerfilePath: ./Dockerfile
dockerContext: .
branch: main
healthCheckPath: /health
autoDeploy: true
envVars:
- key: DATABASE_URL
sync: false # value set manually in the Render dashboard, never stored in YAML
- key: JWT_SECRET_KEY
sync: false
- key: DEBUG
value: "false"
scaling:
minInstances: 1
maxInstances: 3
targetMemoryPercent: 80
targetCPUPercent: 70
databases:
- name: taskmanager-db
databaseName: taskmanager
user: taskmanager
plan: free
sync: false on a secret environment variable means the key itself is declared in the YAML, but its actual value is set through the Render dashboard and never stored in the repository β the correct approach for any secret living inside an infrastructure-as-code file.
Free tier considerationsβ
Render's free tier is genuinely useful for prototyping, but it carries limitations worth knowing before relying on it for anything real. Free web services spin down after 15 minutes with no inbound traffic, and the next request triggers a cold start taking roughly 30β60 seconds. A free PostgreSQL database expires 30 days after creation, with a 14-day grace period to upgrade before the database and its data are deleted permanently. And every Render service β free or paid β has an ephemeral filesystem: anything written to local disk, an uploaded file, a local SQLite database used for anything other than the app's own PostgreSQL connection, is lost every time the service redeploys, restarts, or spins down. For a production API serving real users, the Starter plan (from $7/month) removes the spin-down behavior entirely, and a persistent, non-expiring managed database is what removes the 30-day database limitation.
Option B: Fly.ioβ
Fly.io runs containers on edge servers distributed globally, routing each request to the nearest region β a good fit specifically when geographic latency to users matters.
Installation and authenticationβ
# macOS
brew install flyctl
# Linux
curl -L https://fly.io/install.sh | sh
# Authenticate
fly auth login
Initializing the applicationβ
Run from the project root β the directory containing the Dockerfile:
fly launch
fly launch detects the Dockerfile, prompts for an application name and a region, and generates fly.toml. The generated file needs editing to set the correct internal port and a health check:
app = "taskmanager-api"
primary_region = "nrt" # Tokyo β change to whichever region is closest to your users
[build]
dockerfile = "Dockerfile"
[http_service]
internal_port = 80 # must match the port in CMD [... "--port", "80"]
force_https = true
auto_stop_machines = "stop" # or "suspend" for near-instant resume instead of a cold start
auto_start_machines = true
min_machines_running = 1
[[http_service.checks]]
grace_period = "10s"
interval = "30s"
method = "GET"
path = "/health"
timeout = "5s"
[env]
DEBUG = "false"
auto_stop_machines = "stop" is the current, standard value telling Fly's proxy to stop idle machines and start them again on the next request. "suspend" is an alternative worth knowing about: rather than a full cold start, a suspended machine resumes from a saved snapshot in a few hundred milliseconds instead of several seconds, at the cost of the machine still holding its allocated memory while suspended rather than fully releasing it.
Setting secretsβ
Secrets are set through the CLI directly β never stored in fly.toml:
fly secrets set DATABASE_URL="postgresql://user:password@host:5432/taskmanager"
fly secrets set JWT_SECRET_KEY="your-production-secret-key"
Deployingβ
fly deploy
Fly builds the Docker image (or uses one already pushed to a registry), deploys it to the configured regions, and prints the public URL. Every later deployment uses this exact same command.
Useful Fly.io commandsβ
# View running application status
fly status
# Stream live logs
fly logs
# Scale to multiple instances (horizontal scaling)
fly scale count 3
# Open a shell inside the running container
fly ssh console
Option C: AWS ECS with Fargateβ
AWS Elastic Container Service (ECS) with Fargate runs Docker containers without managing any underlying virtual machines directly β the container definition, networking, and scaling rules are all declared in configuration rather than provisioned by hand.
Prerequisitesβ
An AWS account with the appropriate IAM permissions, the AWS CLI installed and configured (aws configure), and the Docker image pushed to Amazon ECR (Elastic Container Registry).
Pushing the image to Amazon ECRβ
# Create the ECR repository
aws ecr create-repository --repository-name taskmanager-api --region us-east-1
# Authenticate Docker to ECR
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin \
<account-id>.dkr.ecr.us-east-1.amazonaws.com
# Tag the local image for ECR
docker tag taskmanager:latest \
<account-id>.dkr.ecr.us-east-1.amazonaws.com/taskmanager-api:latest
# Push the image
docker push <account-id>.dkr.ecr.us-east-1.amazonaws.com/taskmanager-api:latest
ECS task definition (key fields)β
{
"family": "taskmanager-api",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "512",
"memory": "1024",
"containerDefinitions": [
{
"name": "taskmanager-api",
"image": "<account-id>.dkr.ecr.us-east-1.amazonaws.com/taskmanager-api:latest",
"portMappings": [{ "containerPort": 80, "protocol": "tcp" }],
"environment": [{ "name": "DEBUG", "value": "false" }],
"secrets": [
{ "name": "DATABASE_URL", "valueFrom": "arn:aws:secretsmanager:..." },
{ "name": "JWT_SECRET_KEY", "valueFrom": "arn:aws:secretsmanager:..." }
],
"healthCheck": {
"command": ["CMD-SHELL", "curl -f http://localhost:80/health || exit 1"],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 10
},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/taskmanager-api",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
}
}
]
}
Secrets here are stored in AWS Secrets Manager and referenced by ARN β they're injected as environment variables at container start time and never appear anywhere in the task definition itself in plaintext.
Creating an ECS service on top of this task definition, with an Application Load Balancer in front of it, is the last step β configured to scale out when CPU exceeds 70% for two consecutive minutes, and scale back in when CPU drops below 30% for ten minutes.
Scaling Strategiesβ
Horizontal vs. vertical scalingβ
Horizontal scaling adds more instances of the application container, with a load balancer distributing traffic across all of them β elastic, fully reversible, and achievable with zero downtime, which makes it the correct first response to increased traffic in almost every case. Vertical scaling instead increases the CPU and memory allocated to a single container; it has a hard ceiling at whatever the largest available instance size is, and it requires a restart to apply. Vertical scaling earns its place specifically when a single request is genuinely CPU- or memory-intensive β generating a large report, processing an uploaded file β rather than when the bottleneck is simply request volume.
For the Task Manager API specifically, horizontal scaling is the right default: every individual request is lightweight, and the bottleneck under real traffic is the number of concurrent requests, not the compute cost of any single one.
Multiple workers vs. multiple containersβ
Running multiple workers inside one container (fastapi run --workers 4) uses every CPU core on a single machine, and fits a single-server Docker Compose deployment well. Running multiple containers on an orchestrator β ECS Fargate, several Fly.io machines, Kubernetes pods β distributes load across separate, fully isolated processes instead, with each container running a single Uvicorn process and the orchestrator itself handling health checks and restarting any container that fails. On Render and Fly.io, the instance count is set directly in the dashboard or configuration file; on ECS, it's the desired task count on the service, paired with an auto-scaling policy.
Load Testingβ
Before a production launch, confirming the API actually holds up under the expected request volume is worth doing deliberately rather than finding out from real users. locust is a Python-based load testing tool, with a test defined in plain Python and a web UI showing results in real time.
pip install locust
locustfile.py:
from locust import HttpUser, task, between
class TaskManagerUser(HttpUser):
wait_time = between(0.5, 2.0) # random wait between requests per simulated user
def on_start(self):
"""Log in once per simulated user and store the token."""
response = self.client.post(
"/auth/login",
data={"username": "testuser", "password": "testpassword"},
)
self.token = response.json()["access_token"]
self.headers = {"Authorization": f"Bearer {self.token}"}
@task(3)
def list_tasks(self):
self.client.get("/tasks/", headers=self.headers)
@task(1)
def create_task(self):
self.client.post(
"/tasks/",
json={"title": "Load test task", "completed": False},
headers=self.headers,
)
Running the load test against the deployed API:
locust --host=https://your-api-url.onrender.com
Opening http://localhost:8089 in a browser configures the number of simulated users and the spawn rate, then shows request rates, response times, and failure counts updating live. Watching Prometheus and Grafana dashboards from the application's own observability setup at the same time shows CPU, memory, and request metrics responding directly to the generated load.
Complete Production Architectureβ
ββββββββββββββββββββββββββββββββββββββββββββ
β GitHub Repository β
β βββββββββββββββ ββββββββββββββββββββ β
β β CI Workflow β β CD Workflow β β
β β (on push) β β (on v* tag) β β
β β lint β β build image β β
β β type check β β push to Hub β β
β β pytest Γ3 β β SSH deploy β β
β βββββββββββββββ ββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββΌββββββββββββββββ
β Docker Hub β
β taskmanager:1.x.x :latest β
βββββββββββββββββ¬ββββββββββββββββ
β
βββββββββββββββββΌββββββββββββββββββββββββ
β Production Server / Platform β
β ββββββββββββββββββββββββββββββββββββββ
β β FastAPI Container ββ
β β POST /auth/signup POST /auth/loginββ
β β GET/POST /tasks/ GET /tasks/{id} ββ
β β PUT/PATCH/DELETE /tasks/{id} ββ
β β GET /admin/users WS /ws/tasks ββ
β β GET /health GET /metrics ββ
β ββββββββββββββββββββββββββββββββββββββ
β ββββββββββββ ββββββββββββββ ββββββββββ
β βPostgreSQLβ β Prometheus β βGrafanaββ
β ββββββββββββ ββββββββββββββ ββββββββββ
βββββββββββββββββββββββββββββββββββββββββββ
What the API providesβ
FeatureImplementationUser registrationPOST /auth/signup β Argon2 hashing via pwdlib, Role.user defaultAuthenticationPOST /auth/login β OAuth2 Password Flow, JWT issued on successJWT verificationget_current_user dependency β decodes token, checks is_activeRole-based accessrequire_role() dependency factory β Role.admin for admin routesTask CRUDGET/POST /tasks/ β filtered, paginated, owner-scopedTask ownershipuser_id foreign key β service layer enforces isolationPartial updatesPATCH /tasks/{id} β model_dump(exclude_unset=True)Real-time updatesWS /ws/tasks β ConnectionManager broadcasts JSON eventsBackground tasksScheduled after DB commit via BackgroundTasksRate limitingSlowAPI β 3/min on signup, 5/min on login, Redis-backed in productionCORSCORSMiddleware β explicit origins, allow_credentials=TrueStructured loggingpython-json-logger β named logger, JSON outputMetricsprometheus-fastapi-instrumentator + custom counters and histogramsTracingOpenTelemetry β FastAPIInstrumentor, manual spans on DB operationsHealth checkGET /health β verifies database connectivityContainerizationDockerfile β python:3.13-slim, exec-form CMD, layer-cache optimizedCI pipelineGitHub Actions β lint, type check, pytest across supported Python versionsCD pipelineGitHub Actions β Docker build, push, and deploy on version tags
Extensions and Next Stepsβ
Database migrations with Alembicβ
create_all() creates missing tables but never modifies an existing one β adding a column to an already-deployed model requires Alembic:
pip install alembic
alembic init migrations
migrations/env.py needs configuring to import SQLModel.metadata and point at DATABASE_URL. Generating and applying a migration afterward:
alembic revision --autogenerate -m "add created_at to task"
alembic upgrade head
Frontend integrationβ
The API exposes both a REST interface and a WebSocket endpoint, and a React or Vue frontend connects to both directly:
// REST: fetch tasks
const tasks = await fetch("/tasks/", {
headers: { Authorization: `Bearer ${token}` },
}).then((r) => r.json());
// WebSocket: receive real-time updates
const ws = new WebSocket(`wss://your-api.onrender.com/ws/tasks?token=${token}`);
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.event === "task_created") refreshTaskList();
};
Email notificationsβ
Replacing a file-based notification with a real email integration β aiosmtplib provides an async SMTP client:
pip install aiosmtplib
import aiosmtplib
from email.message import EmailMessage
async def send_welcome_email(to_address: str, username: str) -> None:
message = EmailMessage()
message["From"] = "[emailΒ protected]"
message["To"] = to_address
message["Subject"] = "Welcome to Task Manager"
message.set_content(f"Hi {username}, your account is ready.")
await aiosmtplib.send(
message,
hostname="smtp.sendgrid.net",
port=587,
username="apikey",
password=settings.sendgrid_api_key,
start_tls=True,
)
Security hardeningβ
Enforcing HTTPSRedirectMiddleware and configuring TLS termination at the reverse proxy closes off plain-HTTP access entirely. Audit logging every state-changing request (POST, PUT, PATCH, DELETE) with the authenticated user's ID, the endpoint, and a timestamp creates a record worth having during any later investigation. Validating string lengths with Field(max_length=255) prevents abnormally large payloads from reaching the database at all. Adding pip-audit to the CI workflow catches known vulnerabilities in dependencies automatically, on every push, rather than relying on someone noticing a security advisory manually.
Practice: A Full Deployment Walkthroughβ
Deploying to Render with render.yamlβ
Commit the render.yaml file shown earlier to the repository root, alongside the existing Dockerfile. In the Render dashboard, click New β Blueprint, connect the repository, and Render detects render.yaml automatically and provisions both the web service and the PostgreSQL database it declares. Once provisioning finishes, open the service's Environment tab and set the actual values for DATABASE_URL and JWT_SECRET_KEY β these were declared with sync: false in the YAML specifically so their real values are entered here rather than committed to the repository. Trigger the first deploy, then visit https://<your-service-name>.onrender.com/health once it finishes. A correctly wired health check endpoint returns:
{"status": "ok", "database": "ok"}
If "database" instead reads "error" or the request times out, the most common cause is DATABASE_URL not yet being set correctly in the Environment tab, or the Postgres instance still finishing its own provisioning β waiting a minute and retrying resolves the second case.
Load testing the deployed APIβ
With the service live, point locust directly at the public URL from the locustfile.py shown earlier:
locust --host=https://<your-service-name>.onrender.com
Opening http://localhost:8089, set Number of users to 50 and Spawn rate to 5 (five new simulated users starting per second), then click Start swarming. Letting the test run for at least two to three minutes gives the P95 latency figure time to stabilize rather than reflecting only the initial ramp-up. The Statistics tab in the Locust web UI reports the median, P95, and P99 response times per endpoint directly, and the same load appears in Grafana as a spike in request rate and any corresponding CPU or memory increase on the container β watching both at once confirms the load actually reached the deployed service rather than failing silently at the load generator's end.
Tagging and verifying a releaseβ
git tag v1.0.0
git push origin v1.0.0
Pushing a tag matching the CD workflow's configured pattern (v*) triggers it automatically. In the repository's Actions tab, the running workflow should show three stages completing in order: building the Docker image, pushing both taskmanager:1.0.0 and taskmanager:latest to Docker Hub, and connecting to the production server to redeploy the container. Confirming the deploy actually landed means checking two things directly: that Docker Hub's repository page now lists a 1.0.0 tag alongside latest, and that /health on the live URL responds successfully immediately after the workflow finishes.
Adding a priority column with Alembicβ
With Alembic already initialized as shown above, add the new field directly to the Task model first:
class Task(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
title: str
description: Optional[str] = None
completed: bool = False
priority: int = Field(default=0)
user_id: Optional[int] = Field(default=None, foreign_key="user.id")
Generating the migration from that change:
alembic revision --autogenerate -m "add priority to task"
Opening the generated file inside migrations/versions/ and confirming it contains an op.add_column("task", sa.Column("priority", sa.Integer(), nullable=False, server_default="0")) line β autogenerate is reliable for a straightforward addition like this one, but reading the generated migration before applying it is worth doing as a habit, since autogenerate doesn't always produce exactly the intended migration for more complex model changes. Applying it against the production database:
alembic upgrade head
Confirming it landed correctly means connecting to the production database directly and checking that every existing row now has priority set to 0 β the server_default is specifically what backfills that value onto rows that existed before the migration ran, rather than leaving them with a null value in a column that's declared NOT NULL.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Monitoring and Observability
Next
πΊNext.js
-
What Cloud Deployment Adds
-
Option A: Render
- The $PORT requirement
- Deploying through the Render dashboard
- Deploying through render.yaml (Infrastructure as Code)
- Free tier considerations
-
Option B: Fly.io
- Installation and authentication
- Initializing the application
- Setting secrets
- Deploying
- Useful Fly.io commands
-
Option C: AWS ECS with Fargate
- Prerequisites
- Pushing the image to Amazon ECR
- ECS task definition (key fields)
-
Scaling Strategies
- Horizontal vs. vertical scaling
- Multiple workers vs. multiple containers
-
Load Testing
-
Complete Production Architecture
- What the API provides
-
Extensions and Next Steps
- Database migrations with Alembic
- Frontend integration
- Email notifications
- Security hardening
-
Practice: A Full Deployment Walkthrough
- Deploying to Render with render.yaml
- Load testing the deployed API
- Tagging and verifying a release
- Adding a priority column with Alembic
