Getting Python Functions to Work as Azure Functions

Azure Functions offer the opportunity to run a wide variety of code in a serverless fashion. Function Apps generally work well for microservices and other small pieces of code which run occasionally and don’t really justify a permanent allocation of resources. A series of such functions can be chained together to achieve more complex functionality.

There are many use cases where this is a useful. Functions which need to run in response to a web call, Functions which need to run when new data is added to a Azure Blob Storage. Functions which need to run after another function. Functions which need to run on a timer. All these and more can be automated via Azure Function Apps.

This article will describe how to get started with Azure Functions in Python with VSCode and provide information about some pitfalls that may be encountered and how to overcome them. The process is not difficult, but there are quite a lot of prerequisites and a lot of steps to follow.

Getting Started

Probably the easiest way to get started is with the official quickstart guides and documentation. The Python quickstart is at https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-python. There are some other pages you will want to refer to as well including the Functions reference for Python https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python and the core Azure Functions Documentation page https://docs.microsoft.com/en-us/azure/azure-functions/.

Install Tools

Before you start, you will need to install three tools if you are using VSCode (wwhich you can download from https://code.visualstudio.com/) for developing your Python. If you don;t already have VSCOde, you will likely want to install some addons to help with Python development. This is outside the scope of this article, but you can look at https://code.visualstudio.com/docs/languages/python.

The Azure CLI https://docs.microsoft.com/en-us/cli/azure/install-azure-cli. The quickstart instructions for this are here: https://docs.microsoft.com/en-us/cli/azure/get-started-with-azure-cli. You will not need to use this much, but it is a prerequisite for the Azure Functions Core Tools to let you authenticate with Azure.

Next you will need the Azure Functions Core Tools plugin for VSCode. Details are here: https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local. Once this is installed you will see an Azure Symbol in the left navigation. This will let you run and upload your functions.

Finally it is a good idea to install Azurite, https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azurite. This is a plugin which allows emulation of certain functions triggers and other details of Azure Function Apps on your local machine. Azurite replaces the old Azure Storage Emulator which is no longer under development (but is still refered to here and there in the documentation)

You may also find yourself getting  warnings about dotnet not being installed when trying to create functions. This can be solved by getting the appropriate dotnet runtime from https://dotnet.microsoft.com/download

Create Function

We are now in a position to create our first function. The tutorial at https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-python walks you through this process. However there are a few things to note that may be helpful.

Firstly the combination of VSCode, Azure Functions Core Tools plugin  and Anaconda do not appear to play well together, especially with respect to Anaconda virtual environments. This shows up at the step  Select a Python alias to create a virtual environment. This may be because of the standard install pattern of Anaconda which recommends against adding to your PATH variable. The easiest solution I found was to install a second Python install from the Microsoft store. Then from the Windows command prompt type where python and input the resulting location into the request for a Python alias.

Another issue with trying to use Anaconda is that attempting to run the debug step may fail with a error. The Anaconda solution is given here https://stackoverflow.com/questions/60497854/vs-code-azure-functions-pip-is-configured-with-locations-that-require-tls-ssl, but using a non-Anaconda install solves this too

If you are using Git for source control, note that Azurite adds files to your project that you may want to exclude from your git repo by adding the following to your .gitignore:

# Azurite files to ignore
__azurite_db*
__blobstorage__/*
__queuestorage__/*

Before you can run most triggers except the HTTPTrigger locally you need to start Azurite in VS code. To do this open the VSCode command palette with Ctrl-Shift-P and type Azurite: Start

Its a good idea to open a command window at the bottom of VSCode (Ctrl-Backtick).  This is because the output from the emulator will be displayed there. Also after you start the emulator from the debugger, then go to the Azure tab to test run your function, remember to close the emulator once you are done with Ctrl-C (you need to have clicked in the command window area of the screen before you do this). If you don’t do this , you may find that future operations in the debugger fail. This is mentioned in the documentation but is easily overlooked and produces cryptic unhelpful error messages.

If you have trouble uploading functions to Azure as described, you may need to check your permissions in Azure.

Testing Functions

A final surprising note. Unfortunately it is not possible to test a function app in place on Azure unless it is a C# app Therefore all testing needs to be done prior to upload. A note of what troubleshooting you can do on python apps is given here https://docs.microsoft.com/en-us/azure/azure-functions/recover-python-functions?tabs=vscode

Deleting Azure Functions

Now you have created a test Function App, how do you delete it?

To delete function apps, the cleanest way appears to be to delete the Resource Group Azure creates when you upload them https://social.msdn.microsoft.com/Forums/azure/en-US/693dd936-d933-4c2e-97cd-af5434b9d2d2/how-to-delete-a-function-app . Note that you may need to ensure you have the correct permissions in order to be able to do this.

Further Reading

This has only scratched the surface of getting Functions working on Azure. Here is some more documentation that may help.

Information about Triggers and bindings https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=python

Using Key Vault with Azure Functions https://docs.microsoft.com/en-us/azure/app-service/app-service-key-vault-references

Another introduction to Functions: https://medium.com/ashsoftware/azure-functions-explained-part-1-71da22eabf74

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.