deploying flask APIs on Azure App Service_nehaguptadev

Deploying Flask APIs on Azure App Service: Single API & Multiple APIs with Deployment Slots

Deploying Flask APIs to the cloud doesn’t have to be complicated. In this guide, we’ll walk through how to deploy single and multiple Flask APIs on Azure App Service, using both VS Code (one-click deployment) and the Azure Portal.


🚀 Why Azure App Service for Flask APIs?

Azure App Service is a fully managed platform (PaaS) that allows you to deploy web apps and APIs without worrying about infrastructure.

Key benefits:

  • Easy deployment (one-click from VS Code)
  • Built-in scaling
  • Deployment slots (for staging & production)
  • Secure and highly available

📦 Prerequisites

Before starting, ensure you have:

  • Python installed (3.8+)
  • Flask app ready
  • VS Code with Azure extensions
  • Azure account

🧩 1. Deploying a Single Flask API (VS Code – One Click)

Step 1: Prepare Your Flask App

Your project should look like this:

app.py
requirements.txt

Example app.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello from Flask API!"

if __name__ == "__main__":
    app.run()

Create requirements.txt

flask
gunicorn

Step 2: Add Startup Command

Azure needs a production server:

gunicorn app:app

Step 3: Deploy via VS Code

  1. Install Azure Tools extension pack (including Azure App Service)
  2. Sign in to Azure
  3. Right-click project folder → Deploy to Web App
  4. Create new Web App:
    • Runtime: Python
    • Region: closest to you
  5. Click Deploy

🎉 Done! Your API is live.


🌐 2. Deploying via Azure Portal

Step 1: Create Web App

  1. Go to Azure Portal
  2. Click Create → Web App
  3. Fill details:
    • Runtime: Python
    • OS: Linux
    • Plan: Basic (or Free for testing)

Step 2: Deploy Code

You can deploy using:

  • ZIP upload
  • GitHub integration
  • Azure DevOps

ZIP Deployment

  1. Zip your project
  2. Go to:https://<your-app-name>.scm.azurewebsites.net
  3. Upload ZIP file

Step 3: Configure Startup Command

Go to:

Settings → Configuration → General Settings

Add:

gunicorn app:app

🧠 3. Deploying Multiple Flask APIs

There are 2 main approaches:


✅ Approach 1: Multiple APIs in One App (Recommended)

Structure:

project/
 ├── api1/
 │    └── app.py
 ├── api2/
 │    └── app.py
 └── main.py

main.py

from api1.app import app as api1
from api2.app import app as api2

from flask import Flask

main_app = Flask(__name__)

main_app.register_blueprint(api1, url_prefix="/api1")
main_app.register_blueprint(api2, url_prefix="/api2")

@main_app.route("/")
def home():
    return "Multi API App"

app = main_app

Startup Command

gunicorn main:app

👉 Access APIs:

  • /api1
  • /api2

✅ Approach 2: Separate App Services

Create multiple App Services:

  • flask-api-1
  • flask-api-2

Each has:

  • Its own deployment
  • Independent scaling

👉 Best for:

  • Microservices architecture
  • Large-scale systems

🔄 4. Using Deployment Slots (Staging → Production)

Deployment slots allow zero-downtime deployment.


Step 1: Create Slot

In Azure Portal:

App Service → Deployment Slots → Add Slot

  • Name: staging

Step 2: Deploy to Slot

Deploy your code to staging slot instead of production.


Step 3: Swap Slots

Click Swap:

Staging → Production

🎯 Benefits:

  • Test before going live
  • Instant rollback
  • No downtime

⚡ VS Code Deployment to Slots

  1. Right-click project → Deploy
  2. Select App Service
  3. Choose deployment slot
  4. Deploy

🧪 5. Best Practices

✔ Always use gunicorn (not Flask dev server)
✔ Use .env for secrets
✔ Enable logging in Azure
✔ Use deployment slots for production apps
✔ Scale up based on traffic


📊 Architecture Summary

ScenarioRecommended Approach
Single APIOne App Service
Multiple small APIsSingle App with Blueprints
MicroservicesMultiple App Services
Safe deploymentUse Deployment Slots

🎉 Conclusion

Azure App Service makes Flask deployment extremely simple—from one-click VS Code deployment to advanced multi-API architecture with deployment slots.

Whether you’re building:

  • A simple API
  • A multi-service backend
  • Or a production-ready scalable system


    Leave a Comment

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