Create API and publish to Azure

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
python -m venv venv
.\venv\Scripts\activate
You should see something similar to me with venv.
pip install fastapi
pip install uvicorn
pip freeze > requirements.txt
Requirements file created by the freeze command
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
mydata = [{"name": "Windows 2012 R2","sku": "win2012"},{"name": "Windows 2016","sku": "win2016"},{"name": "Windows 2019","sku": "win2019"}]
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
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
docker build -t myimage ./
Docker working on getting the image to publish the API.
docker images
docker run -d --name mycontainer -p 80:80 myimage
Allow access to docker
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.
Deploying Azure Web App with Docker container
Checking if everything works correctly.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store