Serverless FastAPI on Azure

Ethan Cerami
FastAPI Tutorials
Published in
4 min readApr 7, 2021
Photo by Alex Machado on Unsplash

“Serverless” is not my favorite term.

It implies the complete absence of servers. But of course, serverless actually involves many servers. It’s just that you don’t have to manage these servers yourself, and they are deployed transparently in the background.

I personally prefer the term “functions as a service (FaaS)”. Not as catchy as serverless, but let’s unpack this a bit and consider how it applies to APIs.

First off, APIs are an excellent use case for serverless, as they are usually stateless functions that respond to events. API traffic can also be highly variable, and the automatic scaling provided by FaaS platforms can be very compelling.

Given that, let’s assume you have developed a nice new API and you want to deploy it the world.

In Option 1, you provision a virtual machine on your favorite cloud provider, and deploy your API on this virtual machine. Regardless of demand or traffic, you are stuck with one virtual machine. On the positive size, you pay a set rate, and it’s relatively easy to budget around.

In Option 2, you package up your API and deploy it to a serverless platform. The platform service takes care of deploying your API and installing any required dependencies. You pay only for API executions, and the platform takes care of automatically scaling your API up and down to meet demand.

Fortunately for us, each of the major cloud platforms provides serverless options. In Amazon, we have Lambda Functions. In Google, we have Cloud Functions. In Azure, we have Azure Functions.

Here I focus on Microsoft Azure, and demonstrate how to deploy a bare bones FastAPI to Azure Functions.

If you are new to FastAPI, I have a whole series of tutorials at: https://medium.com/fastapi-tutorials.

The trick with all of these FaaS options is that you need an ASGI wrapper to deploy your FastAPI application. For AWS Lambda, there is a well maintained package called Mangum. For Azure Functions, there is Bonnette, but the library is no longer maintained.

Fortunately for us though, Anthony Shaw from Microsoft has recently written a new ASGI adapter for Azure Functions:

You can find his code at: https://github.com/tonybaloney/ants-azure-demos/tree/master/fastapi-functions.

For our purposes, I forked Anthony’s code, and simplified the FastAPI example. However, I kept the ASGI wrapper as is. You can find my forked and simplified repo at: https://github.com/ecerami/fastapi_azure.

Let’s go through the code briefly here, and then I will then show you how to deploy the application to Azure via VSCode.

  • http_asgi.py: The ASGI wrapper for Azure Functions that Anthony wrote. You can re-use this in your applications.
  • user_api.py: The FastAPI application itself. Here, we have a single end point that returns fake user data. I simplified this a bit from Anthony’s original example.
  • function.json: The configuration file where you describe your function to Azure. For more background on this file, see the Azure Functions Python developer guide.
  • requirements.txt: The set of python requirements. This is used to deploy locally and on Azure functions.

You can deploy the FastAPI application via the Azure Command Line Interface (CLI), but there is also a VSCode Extension for Azure Functions that greatly simplifies the work.

To get started, first install the extension. Then, open the cloned repo from within VSCode. VSCode will automatically create a virtual environment for you, and install all the dependencies in requirements.txt.

To deploy the FastAPI application locally, you just press F5. That’s it!

You will see output similar to that shown below:

Running FastAPI locally within an Azure Functions container.

You can then open a web browser and go to: http://localhost:7071/docs to see the interactive FastAPI docs. Alternatively, you can hit the endpoint directly: http://localhost:7071/user/1 and verify that that you see fake user data.

Deploying to Azure is (almost) just as easy. It’s just two clicks:

You will be prompted with a series of questions and deployment takes just a few minutes. You can then verify that your app is running by going to: https://<APP_NAME>.azurewebsites.net/user/1. You should see similar fake data as shown in Step 4.

That’s it!

In my next installment, I will add some performance benchmark tests for measuring and monitoring the scale out features of Azure Functions.

Happy coding!

--

--

Ethan Cerami
FastAPI Tutorials

Director, Knowledge Systems Group @ Dana-Farber Cancer Institute, Boston MA.