• Alibek Jakupov

Azure Functions with Python : get environment details

Updated: May 3


In this short article we are going to discover one more tip to get more information about Azure Functions environment. Up we go!


Before we start please check out if you have:

  • Python 3.6.x.

  • Azure Functions Core Tools version 2.7.1575 or a later version

  • Azure CLI version 2.x or a later version (optional)

  • An active Azure subscription.


We are not limited in the choice of IDE, however it should be mentioned that Visual Studio

Code has excellent Azure and Azure Function extensions that may dramatically accelerate the development process.


We are now ready to start.



Why do we need this information?


You may have definitely seen the official documentation explaining how to set up your Azure Function up and running in Python environment.


To sum up here are the steps you need to implement:

  • Create and activate a virtual environment. This is actually useful when you want to test whether you have listed all the needed packages in your requirements.txt file. One may forget some of them while working on a global environment which is already pre-configured.

  • Create a local Functions project

  • Create a functionRun the function locally

  • Create a resource groupCreate an Azure Storage account

  • Create a function app in Azure

  • Deploy the function app project to Azure

Easy and straightforward, right? Knowing that Python functions are running on Linux is already sufficient for the most of the cases. However, in some cases this information may be crucial. For instance, installing PyODBC may turn out to be a huge challenge as there a plenty of additional dependencies to be installed. Moreover, if you are willing to connect your function to the SQL Server Database it may be even more complicated to install as there are different procedures for different linux distributions, because of the Microsoft ODBC Driver 17 for SQL Server.


Here are, for example, the list of available drivers:


Debian


RedHat Enterprise Server


SUSE Linux Enterprise Server


Ubuntu


Quite vast, right? When you start looking for the version information there is nothing, and you ask yourself:

What is the linux distribution used in mcr.microsoft.com/azure-functions/base ?


Fortunately, there is a workaround


You need to create a simple function that is based on the "Hello, world" example provided by Microsoft. And here is the tip, instead of showing the "Hello {name}" message why not adding some useful data? There is an excellent built-in Python package called platform, which allows to access to underlying platform’s identifying data. After some simple modification the code should look like:


import logging
import platform
import azure.functions as func



def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
 
    name = req.params.get('name')
 if not name:
 try:
            req_body = req.get_json()
 except ValueError:
 pass
 else:
        name = req_body.get('name')
 
 if name:
            output = platform.platform()
 return func.HttpResponse(f"Hello {name} {output}!")
 else:
 return func.HttpResponse("Please pass a name on the query string\
             or in the request body", status_code=400)

And after saving and publishing it the output is

Hello Alibek Linux-4.19.65-microsoft-standard-x86_64-with-debian-9.9!

Great! We now have all the needed information to start configuring our virtual environment.

Hope this was helpful.

©2018 by macnabbs. Proudly created with Wix.com