Install system dependencies
RUN apt-get update && apt-get install -y -no-install-recommends \ build-essential && \ rm -rf /var/lib/apt/lists/*
📑 Table of Contents
- 🌐 Deploy FastAPI on Azure App Service — App Service
- 🧩 Deploy FastAPI on AKS — AKS
- ⚖️ Comparison — Metrics
- 🟩 Final Thoughts
- ❓ Frequently Asked Questions
- Can I use the same Docker image for both App Service and AKS?
- Do I need a separate Azure DNS record for the AKS LoadBalancer IP?
- How does scaling differ between the two options?
- 📚 References & Further Reading
🌐 Deploy FastAPI on Azure App Service — App Service
The CLI steps below create a Linux App Service that runs the FastAPI container.
$ az group create -name fastapi-rg -location eastus
{ "id": "/subscriptions/xxxxxx/resourceGroups/fastapi-rg", "location": "eastus", "name": "fastapi-rg", "properties": { "provisioningState": "Succeeded" }
}
$ az appservice plan create -name fastapi-plan -resource-group fastapi-rg -sku B1 -is-linux true
{ "id": "/subscriptions/xxxxxx/resourceGroups/fastapi-rg/providers/Microsoft.Web/serverfarms/fastapi-plan", "name": "fastapi-plan", "sku": { "name": "B1", "tier": "Basic" }, "kind": "linux"
}
$ az webapp create -resource-group fastapi-rg -plan fastapi-plan -name fastapi-appsvc -deployment-container-image-name myregistry.azurecr.io/fastapi:latest
{ "hostNames": [ "fastapi-appsvc.azurewebsites.net" ], "state": "Running"
}
After the web app is created, configure the container registry credentials:
$ az webapp config container set \ -name fastapi-appsvc \ -resource-group fastapi-rg \ -docker-registry-server-url https://myregistry.azurecr.io \ -docker-registry-server-user myuser \ -docker-registry-server-password mypassword
{ "dockerCustomImageName": "myregistry.azurecr.io/fastapi:latest", "dockerRegistryServerUrl": "https://myregistry.azurecr.io"
}
Why this, not a direct az webapp up command? The explicit steps give you control over the App Service plan SKU (B1 provides 1 vCPU and 1.75 GB RAM) and enable future scaling without redeploying the container.
According to Microsoft documentation, App Service integrates a built‑in load balancer that automatically distributes traffic across instances, eliminating the need for a separate ingress object. (Also read: 🚀 Deploy Flask App AWS Free Tier — Easy EC2 & Nginx Setup) (More onPythonTPoint tutorials)
Key point: Deploying to Azure App Service requires only a handful of CLI commands and offers a managed runtime with automatic scaling.
🧩 Deploy FastAPI on AKS — AKS
Deploying to AKS involves provisioning a Kubernetes cluster, pushing the image to Azure Container Registry (ACR), and applying Deployment and Service manifests.
$ az aks create \ -resource-group fastapi-rg \ -name fastapi-aks \ -node-count 2 \ -enable-addons monitoring \ -generate-ssh-keys
{ "provisioningState": "Succeeded", "nodeResourceGroup": "MC_fastapi-rg_fastapi-aks_eastus"
}
$ az aks get-credentials -resource-group fastapi-rg -name fastapi-aks
Merged "fastapi-aks" as current context in /home/user/.kube/config
Push the Docker image to ACR (assume the registry already exists):
$ az acr login -name myregistry
Login Succeeded
$ docker tag fastapi:latest myregistry.azurecr.io/fastapi:latest
$ docker push myregistry.azurecr.io/fastapi:latest
The push refers to repository [myregistry.azurecr.io/fastapi]
latest: digest: sha256:abcd1234 size: 1234
Create a Deployment manifest. The file name is deployment.yaml:
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata: name: fastapi-deploy
spec: replicas: 3 selector: matchLabels: app: fastapi template: metadata: labels: app: fastapi spec: containers: - name: fastapi image: myregistry.azurecr.io/fastapi:latest ports: - containerPort: 8000 env: - name: ENV value: production
What this does:
- replicas: Creates three pod instances, enabling horizontal scaling.
- selector / matchLabels: Ties the Deployment to the Service defined later.
- containerPort: Exposes the Uvicorn port inside the pod.
- env: Demonstrates how to inject runtime configuration without rebuilding the image.
Apply the Deployment and expose it with a LoadBalancer Service:
# service.yaml
apiVersion: v1
kind: Service
metadata: name: fastapi-svc
spec: type: LoadBalancer selector: app: fastapi ports: - protocol: TCP port: 80 targetPort: 8000
$ kubectl apply -f deployment.yaml
deployment.apps/fastapi-deploy created
$ kubectl apply -f service.yaml
service/fastapi-svc created
Retrieve the external IP assigned by the Azure Load Balancer:
$ kubectl get service fastapi-svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
fastapi-svc LoadBalancer 10.0.0.45 20.45.123.67 80:31123/TCP 2m
Why this, not a simple az container create command? AKS gives you declarative control over pod lifecycle, rolling updates, and fine‑grained networking policies that App Service cannot expose. The Horizontal Pod Autoscaler can later adjust the replica count based on CPU utilization (default target 80%). (Also read: ☁️ Azure Cosmos DB vs MongoDB for FastAPI — Which One Should You Use?)
Key point: Deploying to AKS requires explicit Kubernetes manifests, but it provides granular scaling and the ability to run multiple microservices side‑by‑side.
⚖️ Comparison — Metrics
The table below summarizes the major differences when you deploy FastAPI on Azure App Service vs AKS.
| Aspect | Azure App Service | Azure Kubernetes Service (AKS) |
|---|---|---|
| Operational Model | Managed PaaS; Microsoft handles OS patches, scaling, and load balancing. | Self‑managed control plane; you define Deployments, Services, and Ingress. |
| Scaling Granularity | Scale at the app‑service‑plan level (instance count). | Scale at pod level; horizontal pod autoscaler reacts to metrics. |
| Startup Latency | Cold start ~5‑10 s (container pull + platform init). | Cold start similar, but can be mitigated with pre‑pulling images on nodes. |
| Cost Model | Pay per instance hour; includes OS and platform overhead. | Pay for VM nodes + ACR storage; more granular but requires capacity planning. |
| Complexity | Low – single CLI command after image push. | Higher – requires manifests, kubectl, and monitoring setup. |
Key point: Choose App Service for simplicity and rapid iteration; choose AKS when you need multi‑service orchestration and fine‑tuned scaling.
🟩 Final Thoughts
The decision to deploy FastAPI on Azure App Service vs AKS rests on how much operational control you require versus how quickly you want to get to production. App Service abstracts away most infrastructure concerns, letting you focus on code and API design. AKS, by contrast, adds a layer of orchestration that pays off when you run several services, need custom networking, or must enforce strict resource quotas.
Both paths share a common Docker image, so you can start with App Service for a fast proof‑of‑concept and later migrate to AKS if the workload grows beyond the PaaS limits. The shared image ensures that the behavior of your FastAPI code remains identical across environments, reducing regression risk.
Consistent container images let you move between Azure App Service and AKS without rewriting application code.
❓ Frequently Asked Questions
Can I use the same Docker image for both App Service and AKS?
Yes. The Dockerfile shown earlier builds a single image that satisfies the runtime requirements of both platforms. You only need to push it once to Azure Container Registry.
Do I need a separate Azure DNS record for the AKS LoadBalancer IP?
Creating a CNAME or A record that points to the external IP of the LoadBalancer Service is recommended for stable endpoint naming, but it is not required for basic testing.
How does scaling differ between the two options?
App Service scales at the instance level, adding or removing whole VMs. AKS scales at the pod level, using the Horizontal Pod Autoscaler to adjust replica counts based on CPU or custom metrics.
💡 Want to practise this hands-on? DigitalOcean gives new accounts $200 free credit for 60 days — enough to spin up a full Linux/Docker/Kubernetes environment at no cost.
📚 Recommended reading: Best DevOps & cloud books on Amazon — from Linux fundamentals to Kubernetes in production, curated for working engineers.
📚 References & Further Reading
- Official FastAPI documentation — guide to building ASGI apps: fastapi.tiangolo.com
- Azure App Service documentation — container deployment details: learn.microsoft.com
- AKS documentation — managing Kubernetes clusters on Azure: learn.microsoft.com
Top comments (0)