Serverless applications with Azure Functions, Python and Docker

Camilo Soto Montoya
5 min readAug 21, 2020

--

Photo by Florian Olivo on Unsplash

Serverless infrastructure allows developers to focus on their code without worrying about application infrastructure.

TIP: The Azure functions runtime is open source and you can run it outside of Azure cloud (https://github.com/Azure/Azure-Functions).

Normally, you don’t have to use docker to deploy your Azure Function project, but sometimes your project requires a specific language version or have a specific dependency or configuration that isn’t provided by just code functionality, for example, ODBC drivers, call operative system programs, connect by VPN to another network, etc.

To solve this, you can create Docker custom images with the Azure Function runtime and publish in the Azure Cloud (or in an on-premise infrastructure).

Let’s rock it!

in an empty folder, you have to create a python virtual environment and activate it.
Open a terminal and run (the activation method can change if you are using PowerShell, bash, cmd, or another terminal).

python -m venv .venv
.venv\scripts\activate

To initialize the Azure Function project run.

func init --worker-runtime python --docker

Create an HTTP triggered function.

func new --name httpLab --template "HTTP trigger"

Install the python packages.

pip install -r requirements.txt

Now, you can open the project in your favorite IDE, for example, VS Code.

Opened project in Visual Studio Code

As you can see, the project has a folder called httpLab with the function files.

HTTP Triggered function files

Open the __init__.py file and replace it with the following code.

Now, let’s run the Azure Function, in the terminal use the next command.

func start

In your browser open http://localhost:7071/api/httpLab and check the result.

Dockerizing the Azure Function

As you can see we ran the code with the “normal ”serverless runtime. Now we will create a custom docker image to install pyodbc with FreeTDS Driver.

Examine the DockerFile and check the base image.

FROM mcr.microsoft.com/azure-functions/python:3.0-python3.8

Replace the file content with the following code:

Open the requirementes.txt file and add pyodbc ad sqlalchemy packages.

azure-functions
pyodbc
sqlalchemy

Open the /httpLab/__init__.py file and replace it with the following code:

To test the local container, in the /httpLab/function.json file you must change the "authLevel": "function" to "authLevel": "anonymous" as the following code:

To build the docker image you have to tag it with the docker hub or azure container registry id, in your terminal run the next command (replace <docker_id> with your docker registry id).

docker build --tag <docker_id>/globant_lab_image:v1.0.0 .

For example (using Azure Container Registry):

docker build --tag globanlab.azurecr.io/globant_lab_image:v1.0.0 .

To test the build, run the image in a local container using the next command. (replace <docker_id> with your docker registry id).

docker run -p 8080:80 -it <docker_id>/globant_lab_image:v1.0.0

For example:

docker run -p 8080:80 -it globanlab.azurecr.io/globant_lab_image:v1.0.0

To check that everything is running open a browser to http://localhost:8080

To test the function open a browser to http://localhost:8080/api/httpLab

After you’ve verified the function app in the container, stop docker with Ctrl+C.

Now let’s reset "authLevel": "function" in /httpLab/function.json :

Rebuild the docker image.

docker build --tag <docker_id>/globant_lab_image:v1.0.0 .

To share your image, which includes deploying to Azure, you must push it to a registry.

docker login
docker push <docker_id>/globant_lab_image:v1.0.0

For example (using an Azure Container Registry) :

docker login globanlab.azurecr.io
docker push globanlab.azurecr.io/globant_lab_image:v1.0.0

Publishing the Azure Function

Create an Azure Functions service, and in the publish option choose Docker Container.

Open the created resource and navigate to the container settings blade.

Fill the form with the information of the container registry where your image was uploaded. As you can see, you can choose between Azure Container Registry, Docker Hub, or a private Registry. (here we are using an Azure Container Registry). Press the save button to start to install the docker image in the Function App.

Refresh the log to check the container installation process. The first time will take more or less 5 minutes while the image is downloaded and loaded.

Navigate to the function blade and as you can see the httpLab function appears in the list. click in the function name.

Select Get Function Url.

In the pop-up window, select default (function key) and then copy the URL to the clipboard. The key is the string of characters following ?code=.

Paste the function URL into your browser’s address bar.

Now you can use CI/CD pipelines to the automatic image build and deployment. In a next story, we will show you how to do it using Azure DevOps pipelines.

ENJOY YOUR CODING.

--

--