Create API and publish to Azure

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:

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:

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:

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:

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

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.

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.

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().

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

If we rerun the API in the browser, we should get the following 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:

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

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

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

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

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.

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.

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.

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

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.

IT tech geek, developer etc.