Azure Functions with different Azure services

Jonathan
6 min readMar 24, 2022

--

Introduction

If you are like me, a newbie in the serverless world, the first thought is probably to read through all official documentation you can find related with Azure Functions. However, I was not able to successfully create and run an Azure Function just based on the instructions provided by the official documentation. Maybe it is just a personal situation. I spent a decent amount of time pulling information from different places and I was able to eventually understand Azure Functions’ basics and make it run the way I want.

Before we start digging into the details, let’s take a look at what are the common services Azure Functions interact with.

Image Source: Guidance for developing Azure Functions | Microsoft Docs

For demonstration purpose, we will pick one Python example of HTTP trigger, Queue Storage trigger and Timer trigger.

HTTP Trigger

Usually, users develop Azure Functions in an Integrated Development Environment (IDE) such as Visual Studio Code (VS Code). However, with the latest features, users can actually create a C# Azure Functions with just Azure portal. Since I am familiar with Python, I will demonstrate everything from VS Code.

This article is a great starting point to set up IDE, test locally and publish it to Azure. Users are not required to have much knowledge on either the programming language (Python), local environment tooling (VS Code, Azure Functions Core Tools) or Azure platform as long as you are following the article step by step. It is perfect for people who are entirely new.

Instead of going through “Create and activate a virtual environment”, go through “Create a local function project” first. This way, you can keep every related setup and modules clean within the new project alone.

  • The name you provided in “func new” command will be shown as a folder under the name you provided in “func init” command. In my case, I give the 2 the same name.
  • To show terminal in VS Code, go to “View” and then “Terminal”.
  • Set up virtual environment for Python development. In the official tutorial, it names the virtual environment as “.venv”. I prefer not to name the virtual environment, so I put “.” instead of a name. Once that is done, a folder named “bin” will show under the parent folder. If you are running in Python3+, please use
python3 -m venv <virtual env. name> 
  • Install all the required modules with one liner
pip install -r requirements.txt
  • Execute the command below and something similar should be shown.
func start
  • Access the URL listed. In this example, it is the one with
http://localhost:7071/api/jonwhttptrigfunc3
  • Add “?name=<whatever name>” in the end of the URL and something similar to the below will be shown.
  • Azure Functions needs a storage account to save all the information related to the function. More information can be found here.
#create a resource group
az group create --name <RG_NAME> --location <LOCATION>
#create a storage account
az storage account create --name <STORAGE_NAME> --sku Standard_LRS
  • Create an Azure Functions in your Azure subscription.
az functionapp create -g <RG_NAME> --consumption-plan-location <LOCATION_NAME> --runtime python --runtime-version 3.8 --functions-version 3 --name <APP_NAME> --os-type linux --storage-account <STORAGE_NAME>
  • Deploy the local Python function app to Azure as Azure Functions.
func azure functionapp publish <APP_NAME>

Here is the complete codes related to the HTTP-trigger Azure Functions mentioned above.

Queue Storage Trigger

The way to set up Azure Functions with queue storage is almost identical to Azure Functions with HTTP trigger except for the fact that the application will need to bind with the input and output queue storage.

  • First things first, create an input and an output queue storage.
#create an input queue storage
az storage queue create -n <input_queue_name> --account-name <storage_name>
#create an output queue storage
az storage queue create -n <output_queue_name> --account-name <storage_name>
  • Get the storage account’s connection string and put it into “local.settings.json” in the local project.
  • Update “functions.json” in the local project. Refer the light green part to “local.settings.json” in the project; refer the dark blue part to the queues created in the storage account; refer the dark purple part to the actual code file “__init__.py”.
  • As the code is interacting with JSON-format data, the example will need to leverage JSON example data to see how it is being transformed in queue-triggered Azure Functions. The below shows what input the function gets.
  • The below shows what the output is like. Basically, for whatever JSON-format data the Azure Functions gets in the input queue, it will transform it into JSON-format data,
{ "text": "abc" }
  • Azure Functions needs a storage account to save all the information related to the function. More information can be found here.
#create a resource group
az group create --name <RG_NAME> --location <LOCATION>
#create a storage account
az storage account create --name <STORAGE_NAME> --sku Standard_LRS
  • Create an Azure Functions in your Azure subscription.
az functionapp create -g <RG_NAME> --consumption-plan-location <LOCATION_NAME> --runtime python --runtime-version 3.8 --functions-version 3 --name <APP_NAME> --os-type linux --storage-account <STORAGE_NAME>
  • Deploy the local Python function app to Azure as Azure Functions.
func azure functionapp publish <APP_NAME>

Here is the complete code of the queue-triggered Azure Functions mentioned above.

Timer Trigger

Timer-trigger Azure Functions are having really similar setups like the 2 above. The only thing that will need extra attention is the settings in “function.json” that is using CRON schedule syntax.

{  
"scriptFile": "__init__.py",
"bindings": [
{ "name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "* */10 * * * *"
}
]
}

Users will still need to create a storage account to save Azure Functions setting information and an Azure Functions to upload all the local project details.

Here is the complete code of the queue-triggered Azure Functions mentioned above.

When I was stuck at first, I searched on the Internet and found this website giving detailed instructions on how to create different bindings with Azure Functions. The content is extremely straightforward and easy to follow!

--

--

Jonathan

Started my career as a consultant, moved to support engineer, service engineer and now a product manager. Trying to be a better PM systematically every day.