Create API and publish to Azure

Gisli Gudmundsson
6 min readJun 7, 2021

--

When we try to decide what API we should use and deploy, we go through different scenarios, e.g., what type of applications are connecting, how many users will use the API, and many other things we need to think of. In this blog, we will create an API using Python and FastAPI framework; we will containerize it using Docker, and from there, we will publish it to Azure. This blog is a follow-up to a blog from https://365viking.com/ where you can deploy the docker container to Azure using Azure DevOps.

Prepping the project

First, we need to install Python on our computer, but I will not teach how to install Python; there are many articles on installing it, so I leave it. Search Link To Python Install. I will be using Visual Studio Code to create the API, but first, create a project directory and then upgrade pip and create a virtual environment for Python. Start by upgrading pip by using this command:

py -m pip install --upgrade pip

My project directory is c:\Development\Teaching\MyDetailedInfo, now; to create the virtual environment, type in the following command in the folder where your project is located:

python -m venv venv

Now you need to enable the environment, so to do that, you need to be in the project directory and from there run the following command:

.\venv\Scripts\activate
You should see something similar to me with venv.

Next, we need to install the FastAPI framework and create a requirements file that docker will utilize later. In the command line, type in the following command:

pip install fastapi
pip install uvicorn

Now, when the installation is finished, you should type in the following command to create the requirements.txt file:

pip freeze > requirements.txt
Requirements file created by the freeze command

Create the project file structure, and it should contain these folders and files. Create an app folder within the project, and in the app, the folder should contain an empty main.py file.

You should now be ready for creating the API

Creating simple API service

Our API will contain some details such as different types of operating systems and their unique ids. From there, we can use that to do some Powershell scripting to attach more details. But to start with, we will create just a simple API that shows hello world when we try to browse the service.

from typing import Optional
from fastapi import FastAPI
app = FastAPI()@app.get("/")
def read_root():
return {"Hello": "World"}
When done, we can then browse the swagger documentation

When we have finished writing the application and running it, we can open a browser and get the data we are providing. Browse to the following path HTTP://127.0.0.1:8000/docs. From there, we can see the Hello World response body.

Since we have created this, we can now add some sample data about the operating systems. Add this to the code, just after app = FastAPI().

mydata = [{"name": "Windows 2012 R2","sku": "win2012"},{"name": "Windows 2016","sku": "win2016"},{"name": "Windows 2019","sku": "win2019"}]

Add the variable my data to the return of the read_root. The current code should then look like this.

from typing import Optional
from fastapi import FastAPI
app = FastAPI()mydata = [{"name": "Windows 2012 R2","sku": "win2012"},{"name": "Windows 2016","sku": "win2016"},{"name": "Windows 2019","sku": "win2019"}]@app.get("/")
def read_root():
return mydata

If we rerun the API in the browser, we should get the following data.

Note this is just dummy data. We can use SQL or a similar solution to fetch data.

Creating the docker container

Create a new file under the project directory named Dockerfile. This file should contain information about how you will deploy service into a container. Before you can do anything, you need to install docker software which you can download from https://www.docker.com/get-started. When you have installed everything required, you have the option to run docker commands. After you have created the Dockerfile, you will need to add the following text within the file:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8
COPY ./app /app
Creating the Dockerfile

Now you can build the image and test it out by typing the following command:

docker build -t myimage ./
Docker working on getting the image to publish the API.

You should see the image, but it should not be running; type in the following command:

docker images

Now you can run the docker image by typing the following command:

docker run -d --name mycontainer -p 80:80 myimage

You are exposing port 80 from the docker image, so please select another port if it is in use. If you get a Windows Security Alert like this one, you click on Allow access

Allow access to docker

The docker image should be running, and you can therefore open up a browser and then type the path of the docker container exposed port. Which is, in my case, 127.0.0.1, and the port is 80.

Browsing the API on the local machine through the docker container.

Publishing to Azure

You have gotten this far, so the next thing is to deploy to Azure, and there are few ways to do this. But what I am going to do is push this by using the portal itself. First, you need to create a container registry in Azure; you can do that by searching for the container and clicking on the container registry.

Creating a container registry.

Now you have created the container registry, you should copy the password to use it later. Next is to push the container into the registry, and for that, you need to type in the following information:

Note that myapicontaineros.azure.cr.io should be your container URL. You should also download the Azure CLI to use the az command.

After you have published the docker image to Azure, you should see the container in the repository:

Now you can deploy the container to the web app, and for that, you need to create the web app. Start by going into App Services and from there click on create app service.

Deploying Azure Web App with Docker container

We have deployed everything based on our configuration, and we should be able to browse the website, as we can see in this screenshot:

Checking if everything works correctly.

We have now finished creating API using Python and API, deployed it to the Docker container, and from there to Azure as a service that we can then use to extend whatever we need. An example of that would be using the PowerPlatform.

--

--