Replies: 2 comments
-
|
Serverless + Terraform for FastAPI is definitely doable. Here's what I've learned deploying cloud-native Python apps: Quick WinsFor GCP Cloud Run (simplest serverless): resource "google_cloud_run_service" "api" {
name = "fastapi-app"
location = "us-central1"
template {
spec {
containers {
image = "gcr.io/${var.project_id}/fastapi:latest"
ports {
container_port = 8000
}
}
}
}
}For AWS Lambda + API Gateway: Architecture Trade-offsServerless FastAPI works best for:
Watch out for:
Alternative: Kubernetes ServerlessIf you need more control, consider Knative on GKE — gives you serverless autoscaling WITHOUT vendor lock-in. I built productivity-cloud exactly for this:
The repo has:
Terraform Starter (Google Cloud Run)# main.tf
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 5.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
}
resource "google_cloud_run_service" "fastapi" {
name = "fastapi-serverless"
location = var.region
template {
spec {
containers {
image = "gcr.io/${var.project_id}/fastapi:latest"
env {
name = "DATABASE_URL"
value = var.database_url
}
resources {
limits = {
cpu = "1000m"
memory = "512Mi"
}
}
}
}
}
traffic {
percent = 100
latest_revision = true
}
}
resource "google_cloud_run_service_iam_member" "public" {
service = google_cloud_run_service.fastapi.name
location = google_cloud_run_service.fastapi.location
role = "roles/run.invoker"
member = "allUsers" # Make public (or restrict to your domain)
}
output "url" {
value = google_cloud_run_service.fastapi.status[0].url
}Next Steps
Happy to help if you hit specific blockers. Cloud-native Python deployment is tricky the first time but becomes muscle memory quickly. |
Beta Was this translation helpful? Give feedback.
-
Serverless FastAPI Architecture PatternFor production-grade serverless FastAPI deployments, here's a battle-tested pattern: # handler.py - Lambda/Cloud Function entry point
from mangum import Mangum
from app.main import app # Your FastAPI app
handler = Mangum(app, lifespan="off")Key Architecture Decisions:
Real-World Example:I built productivity-cloud - a FastAPI-based task orchestration platform designed for serverless deployment:
# Dockerfile optimized for serverless
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]Production Checklist:✅ Enable CloudWatch/Stackdriver logging Happy to share more deployment patterns if anyone's tackling multi-region serverless FastAPI! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
The current deployment setup works well, but I think it would be very useful for this project to include Terraform configuration files that enable serverless deployment, for example on Google Cloud. I need this for my own project, and since I don’t have much experience with infrastructure and Terraform, setting it up has been challenging.
Just throwing a word out here...
Beta Was this translation helpful? Give feedback.
All reactions