FastAPI-AzureFunctions-Deployment

image FastAPI Deployment to Azure Functions (Learning Project)

This repository demonstrates how to deploy a FastAPI application to Azure Functions using the v2 model with a function_app.py file. The goal of this project is to showcase the deployment process of a basic FastAPI app using Azure’s serverless computing capabilities.

🔗 GitHub Repository

GitHub Repository Link

📋Table of Contents

🌟About

This is a learning project aimed at showcasing the deployment process of a FastAPI application to Azure Functions. I am working on FastAPI-related tasks at Mila AI, and this project helps me apply and demonstrate my knowledge of deploying cloud applications, focusing on the serverless approach using Azure.

Azure Functions Deployment


🛠Prerequisites

Before starting, ensure you have the following:


image FastAPI Application Setup

Once the FastAPI application code is set up with a basic structure, you’ll need to ensure that it can handle HTTP requests when deployed as an Azure Function. The setup includes a simple FastAPI app with one or two routes to demonstrate the deployment.

We will use the v2 model of Azure Functions, which structures the project using a function_app.py file, eliminating the need for a function.json file.


Creating an Azure Function

Next, we’ll initialize the Azure Function and set up the HTTP trigger to route requests to our FastAPI app.

  1. Initialize a new Azure Function in your project directory:
     func init --worker-runtime python
    
  2. Create an HTTP-triggered function:
     func new --name HttpTrigger --template "HTTP trigger" --authlevel "anonymous"
    
  3. Modify function_app.py to route requests to FastAPI app (as given in codebase).

🚀Deploying FastAPI to Azure Functions

Deploying the FastAPI app to Azure Functions involves the following steps:

  1. Login to Azure via CLI:
     az login
    
  2. Deploy the App:

    • First, create a ZIP file of your function app:
      zip -r function.zip .
      
    • Then, deploy the ZIP file to your Azure Function App:
      az functionapp deployment source config-zip --name <YOUR_FUNCTION_APP_NAME> --resource-group <YOUR_RESOURCE_GROUP_NAME> --src function.zip
      

image


Testing the Deployment

Once deployed, you can test your FastAPI app by accessing the provided function URL.

  1. Get the URL: The Azure CLI will output the URL for function app after deployment. It will look something like:
    https://<YOUR_FUNCTION_APP_NAME>.azurewebsites.net/HttpTrigger
    
  2. Test the API: Use curl or Postman to send a request to your FastAPI endpoint:
    curl https://<YOUR_FUNCTION_APP_NAME>.azurewebsites.net/HttpTrigger
    

    You should see a JSON response similar to:

    {
        "message": "Hello from FastAPI deployed on Azure Functions!"
    }
    

image


🧠What I Learned

While working on this project, I gained hands-on experience with:


🎯Conclusion

This project serves as a demonstration of deploying a simple FastAPI application to Azure Functions using the v2 model. While this project focuses on the deployment process, it can be extended into a full-featured API in the future.

Feel free to explore, contribute, or provide feedback on this repository!


🔗References

This project was built based on the official Microsoft Azure documentation. You can learn more about deploying Python apps on Azure Functions from the following resources: