From Code to Cloud: A Step-by-Step Guide to Deploying Your FastAPI App on Azure in 2025

Listen to this article

Hey there! If you’re like me, you love building fast, modern APIs with FastAPI, but deploying them to the cloud can feel like a daunting task. I recently went through the process of deploying a FastAPI app on Microsoft Azure, and while it took some trial and error, I finally got it working smoothly. In this tutorial, I’ll walk you through every step of deploying your FastAPI app on Azure, from setting up your local environment to getting your app live on the cloud. I’ll keep things simple, using plain English, so even if you’re new to Azure, you can follow along. By the end, you’ll have your FastAPI app up and running, ready to handle requests from anywhere in the world. Let’s dive in!

Why FastAPI and Azure?

Before we get started, let’s talk about why I chose FastAPI and Azure. FastAPI is a Python framework that’s super fast (hence the name), easy to use, and perfect for building APIs. It’s built on top of Starlette and Pydantic, which means you get automatic data validation and great performance out of the box. I’ve been using it for a small project a task management API and I like how quickly I can go from idea to working code.

Azure, on the other hand, is Microsoft’s cloud platform. It’s reliable, scales well, and has a lot of tools to make deployment easier. I picked Azure because I wanted to learn a cloud platform that’s widely used in the industry, and I’d heard good things about its support for Python apps. Plus, Azure offers a free tier, which is great for small projects or just experimenting. Now, let’s get to the fun part deploying the app!

Step 1: Build a Simple FastAPI App

First, we need a FastAPI app to deploy. If you already have one, you can skip this step, but if not, let’s create a basic one together. I’m assuming you have Python installed on your computer (I’m using Python 3.9, but anything 3.7 or higher should work).

Start by creating a new project folder and setting up a virtual environment. Open your terminal and run:

mkdir fastapi-azure-app
cd fastapi-azure-app
python -m venv venv
source venv/bin/activate  # On Windows, use: venv\Scripts\activate

Next, install FastAPI and Uvicorn (a server to run our app). Run this command:

pip install fastapi uvicorn

Now, let’s create a simple FastAPI app. Create a file called main.py and add this code:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello from FastAPI on Azure!"}

@app.get("/tasks/{task_id}")
def read_task(task_id: int):
    return {"task_id": task_id, "description": f"Task {task_id} is awesome!"}

This app has two endpoints: a root endpoint (/) that says hello, and a /tasks/{task_id} endpoint that returns a task based on an ID. Let’s test it locally to make sure it works. Run this command:

uvicorn main:app --reload

Open your browser and go to http://127.0.0.1:8000. You should see {"message": "Hello from FastAPI on Azure!"}. Try http://127.0.0.1:8000/tasks/42—you’ll get {"task_id": 42, "description": "Task 42 is awesome!"}. If everything works, we’re ready to move on!

Step 2: Prepare Your App for Deployment

Before we can deploy to Azure, we need to make sure our app is ready. First, let’s create a requirements.txt file so Azure knows which packages to install. In your terminal, run:

pip freeze > requirements.txt

This will create a file listing FastAPI, Uvicorn, and their dependencies. Open requirements.txt and make sure it includes fastapi, uvicorn, and any other packages you’re using.

Next, Azure needs to know how to start our app. Create a file called startup.sh in your project folder with this content:

#!/bin/bash
uvicorn main:app --host 0.0.0.0 --port $PORT

This script tells Azure to run our app using Uvicorn and listen on the port Azure provides (stored in the $PORT environment variable). Make the script executable by running:

chmod +x startup.sh

Finally, let’s add a .gitignore file so we don’t upload unnecessary stuff (like our virtual environment) to Azure. Create a file called .gitignore and add:

venv/
__pycache__/
*.pyc

Step 3: Set Up Your Azure Account and Tools

Now we need to get Azure ready. If you don’t have an Azure account, go to portal.azure.com and sign up. Azure offers a free tier with some credits for new users, which is enough for this project.

Once you’re signed in, we need to install the Azure CLI (Command Line Interface) to manage our deployment from the terminal. On a Mac, I used Homebrew:

brew install azure-cli

If you’re on Windows or Linux, check the official Azure CLI docs for installation steps. After installing, log in to Azure by running:

az login

This will open a browser window where you can sign in to your Azure account. Once you’re logged in, you’re ready to create a resource group and an app service for our FastAPI app.

Step 4: Create an Azure App Service

Azure App Service is a platform for hosting web apps, and it’s perfect for our FastAPI app. Let’s create one using the Azure CLI. First, create a resource group (think of it as a folder for your Azure resources):

az group create --name FastAPIResourceGroup --location eastus

I chose eastus as the location because it’s close to me, but you can pick a region closer to your users (run az account list-locations to see options).

Next, create an App Service plan. This defines the pricing tier and resources for your app. I’ll use the free tier (F1) for this tutorial:

az appservice plan create --name FastAPIAppServicePlan --resource-group FastAPIResourceGroup --sku F1 --is-linux

The --is-linux flag tells Azure we’re deploying a Linux-based app (since FastAPI runs on Python). Now, create the web app itself:

az webapp create --name myfastapiapp2025 --resource-group FastAPIResourceGroup --plan FastAPIAppServicePlan --runtime "PYTHON|3.9"

Here, myfastapiapp2025 is the name of your app (it needs to be unique, so pick something different if this name is taken). The --runtime "PYTHON|3.9" flag tells Azure to use Python 3.9.

Step 5: Configure Your App Service

Before we deploy, we need to configure a few settings in Azure. First, tell Azure to use our startup.sh script to run the app. Run this command:

az webapp config set --resource-group FastAPIResourceGroup --name myfastapiapp2025 --startup-file startup.sh

Next, let’s set an environment variable to make sure Azure knows which port to use. By default, Azure uses port 8000, but we’re letting Uvicorn use the $PORT variable in startup.sh, so this should work automatically. Still, it’s good to double-check:

az webapp config appsettings set --resource-group FastAPIResourceGroup --name myfastapiapp2025 --settings WEBSITES_PORT=8000

Step 6: Deploy Your FastAPI App to Azure

Now we’re ready to deploy! First, initialize a Git repository in your project folder (Azure App Service can pull code from Git):

git init
git add .
git commit -m "Initial commit of FastAPI app"

Next, set up Azure to pull from your local Git repo. Run this command to get the deployment URL:

az webapp deployment source config-local-git --name myfastapiapp2025 --resource-group FastAPIResourceGroup

This will output a Git URL like https://myfastapiapp2025.scm.azurewebsites.net/myfastapiapp2025.git. Add this as a remote to your local Git repo:

git remote add azure https://myfastapiapp2025.scm.azurewebsites.net/myfastapiapp2025.git

You’ll also need to set up deployment credentials. Run:

az webapp deployment user set --user-name mydeployuser --password MySecurePassword123!

Replace mydeployuser and MySecurePassword123! with your own username and password (make sure the password meets Azure’s requirements—minimum 8 characters, with letters, numbers, and special characters).

Finally, push your code to Azure:

git push azure main

You’ll be prompted for the username and password you just set. Once the push completes, Azure will build and deploy your app. This might take a few minutes—Azure needs to install the packages from requirements.txt and start the app.

Step 7: Test Your Deployed App

Once the deployment is done, your app should be live! Azure will give your app a default URL like https://myfastapiapp2025.azurewebsites.net. Open that URL in your browser, and you should see {"message": "Hello from FastAPI on Azure!"}. Try the other endpoint too—go to https://myfastapiapp2025.azurewebsites.net/tasks/42, and you should see {"task_id": 42, "description": "Task 42 is awesome!"}.

If something doesn’t work, don’t panic! You can check the logs to see what went wrong. Run this command:

az webapp log tail --name myfastapiapp2025 --resource-group FastAPIResourceGroup

This will show you the live logs from your app. Common issues include missing packages in requirements.txt, errors in startup.sh, or port conflicts. Fix any issues, commit the changes, and push to Azure again.

Step 8: Add a Custom Domain (Optional)

If you want your app to have a custom domain (like mytaskapi.com instead of myfastapiapp2025.azurewebsites.net), you can set that up in Azure. First, buy a domain from a registrar like Namecheap or Google Domains. Then, in the Azure portal, go to your App Service, click “Custom domains,” and follow the steps to add your domain. You’ll need to update your domain’s DNS settings with your registrar—Azure will give you the details. Note that the free tier doesn’t support custom domains, so you’ll need to upgrade to a paid tier (like B1, which starts at around $10/month).

Troubleshooting Tips

I ran into a few hiccups while deploying, so here are some tips in case you hit similar issues:

  • App Doesn’t Start: Check your startup.sh file and make sure Uvicorn is running on the correct port. Also, ensure all dependencies are listed in requirements.txt.
  • 404 Errors: Make sure your endpoints are correct. FastAPI is strict about trailing slashes—if your code expects /tasks/42 but you visit /tasks/42/, you’ll get a 404.
  • Deployment Fails: Double-check your Git commands and Azure credentials. If Azure can’t install a package, you might need to update your Python runtime (e.g., switch to 3.10).

Deploying my FastAPI app on Azure was a bit of a learning curve, but it was totally worth it. Now my task management API is live, accessible from anywhere, and I don’t have to worry about managing a server myself. Azure handles scaling, updates, and security, so I can focus on building features for my app. The whole process took me about 2 hours the first time (including debugging), but now that I know the steps, I can deploy a new app in under 30 minutes.

If you’re looking to get your FastAPI app online, Azure is a solid choice. It’s affordable, reliable, and has great tools for Python developers. Give it a try, and let me know how it goes I’d love to hear about your experience! Now that my app is live, I’m already planning to add more features, like user authentication and a database. But for now, I’m just happy to have my API up and running in the cloud.

Leave a Reply

Your email address will not be published. Required fields are marked *