Creating Your First Azure API with Python, Azure Web App, and Terraform Using Azure API Management

Abhimanyubajaj
5 min readNov 2, 2023

Azure API Management helps organizations publish APIs to external, partner, and internal developers to unlock the potential of their data and services. It is made up of an API gateway, a management plane, and a developer portal. These components are Azure-hosted and fully managed by default.

  • The API gateway is the endpoint that accepts API calls and routes them to appropriate backends, verifies API keys and other credentials presented with requests.
  • The management plane is the administrative interface where you set up your API program. Its provision and configure API Management service settings, define or import API schema.
  • The Developer portal is an automatically generated, fully customizable website with the documentation of your APIs. Its read API documentation, call an API via the interactive console.

In this tutorial, We will first create an Azure API Management service instance. We will than create a WebApp(Python) that we will use to create our first API in API management.

API Management Service setup

Step 1: Go to Azure portal and search for API Management Service.

Step 2: In the API management service, You would need to have a UNIQUE resource name, User your name as organization email and provide your email as Administrator email.

Once your instance is deployed, Navigate to API management service again and select the resource you created, In my case it will be first-apim-demo. It takes few minutes for the instance to get ready.

You can also create the API management service using terraform which is available https://github.com/Abhimanyu9988/azure-api-management.git
To access ->

git clone https://github.com/Abhimanyu9988/azure-api-management.git
cd azure-api-management
terraform init
terraform apply

Application Setup

Moving on, Let’s create a simple python application (also availaible in git repo) using fastapi that will run on gunicorn.

from fastapi import FastAPI
from pydantic import BaseModel

class Input(BaseModel):
text: str

# Define the app
app = FastAPI(
title="MyApp",
description="Hello API developer!",
version="0.1.0"
)

# Define a GET operation
@app.get("/")
async def main():
return {"message": "Hello Universe!"}

# Define a POST operation
@app.post("/submit")
async def submit(input: Input):
return {"message": f"Data submitted is: {input.text}"}

The code defines 2 API endpoints using decorators:

  • @app.get("/"): This decorator specifies that the following function handles GET requests to the root path ("/"). The function returns a JSON response with a "Hello Universe!" message.
  • @app.post("/submit"): This decorator specifies that the following function handles POST requests to the "/submit" path. It expects a JSON request body that matches the Input data model and returns a response with the submitted text.

Great. We would need to create a Docker image and use that as the source code for our Azure linux web app. You should get the Dockerfile from the repo, located under TerraformForAzureApp folder.

git clone https://github.com/Abhimanyu9988/azure-api-management.git
cd azure-api-management/TerraformForAzureApp

From TerraformForAzureApp directory, Run below commands to create the image->

docker build -t <image-name> --platform=linux/amd64 .
docker tag <image-name> <your-repo>/<image-name>
docker push <your-repo>/<image-name>

Beautiful!! Once you have pushed the image. Let’s create a WebApp which will host this application for us. You can get the terraform files under TerraformForAzureApp folder itself. From inside the folder, Run->

terraform init
terraform apply --auto-approve

Once done, You should see the WebApp in Azure portal -> Azure App Service

You can click on Default Domain and you should see the message

“Hello Universe!”

Good work all!! Now let’s set up our API using this Python Web App.

Azure API using Python Web App

As part of our earlier 2 steps we have our API management Instance and Web App ready, Let’s set up the API.

Step 1: Search for API Management in Azure portal.
Step 2: On the API management page, Click on APIs, You will see a bunch of options under Add API

We will be using App Service in the “Create from Azure resource” section.

Step 3: Click on App Service -> Browse and select your Web App.

All the details will get populated automatically once you select your web app. Once it does, Click on “Create”

Great work!! You have just created your first API. Now, Let’s test it out.

Click on Test -> Get <API> -> Send

Viola!! We can see “Hello Universe!” in our Response, Which is what we configured in our Python application :)

For more information or in case you get stuck, Feel free to connect with me on LinkedIn

https://www.linkedin.com/in/theabhibajaj/

--

--

Abhimanyubajaj

I solve problems. CKAD, CKA, Azure, AWS, GCP, Terraform Certified. Senior Software Engineer at Cisco.