[Azure Functions / Python] Chain functions with Queue Storage binding

Introduction

This article aims to chain Azure Functions using Queue Storage bindings among Azure Functions.

The outline is as follows,

It looks like this in the figure.

azure-Page-2 (1).png

  1. User etc. hit curl
  2. Azure Function of ② starts
  3. Queue Storage has messages enqueue
  4. Azure Functions of ③ starts with enqueue as trigger

Premise

Preparation / implementation

Preparation: ① Queue Storage

Create a Queue called piyo in Queue Storage from the" + Queue "button.

storage_account_qiita.jpg

Implementation: ② Azure Functions

Create Azure Functions for HTTP Trigger from VS Code and modify the automatically generated code. Add the bottom 5 items, " type "," direction "," name "," queueName "," connection ". (If it is VSCode, you can also add it with add binding.)

function.json


{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "type": "queue",
      "direction": "out",
      "name": "fuga1",
      "queueName": "piyo",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

The additional items are briefly explained below. A setting that fits easily is name. This name is the argument name you receive in your python program. (At first, I didn't understand this, and the output of the function execution log was slow, so it took time to identify the cause of the error ...)

item name constant Parameters Description
type queue Type of binding
direction out Binding direction. Out for output
name fuga1 When receiving with a python execution functionArgument name
queueName piyo The name of the Queue created in the steps above
connection AzureWebJobsStorage This time it is a constant because it is the same Storage Account. Do you usually enter a connection string etc.?

Next is the Python code body. Now, put fuga1 in the argument of the main function. That is, specify the same name as function.json. ** Caution If you put a symbol such as _ in name, it will not work. ** **

__init__.py


import logging
import azure.functions as func


def main(req: func.HttpRequest, fuga1: func.Out[str]) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    #Get parameters
    name = req.params.get('name')
    #Put in queue
    fuga1.set(name)
    
    #Return value (It is a little longer in the case of automatic generation, but it is omitted because it is not relevant here)
    return func.HttpResponse("\n".join(return_list))

Implementation: ③ Azure Functions

As in (2), the Queue Trigger function is automatically generated from VS Code this time. This time only the input binding. For queueName, specify piyo of Queue Storage. name is fuga2. This name can be uniquely determined for each ʻAzure Functions`.

function.json


{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "fuga2",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "piyo",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Next is the Python code body. Modify only the argument of the main function to the variable name fuga2 specified in function.json.

__init__.py


import logging
import azure.functions as func


def main(fuga2: func.QueueMessage) -> None:
    logging.info('Python queue trigger function processed a queue item: %s',
                 fuga2.get_body().decode('utf-8'))

Execution example

If you hit it from your local curl, you can see from the log that it's working.

First of all, Azure Functions of ② is started by HTTP request, test_function.jpg

③ Azure Functions is started with Queue as a trigger. queue_trigger.jpg

You can see that Azure Functions is running 2 seconds after the message is entered in the Queue. (Close your eyes on the function name ...)

in conclusion

I found that it can be chained with Queue Trigger of Azure Functions. When creating a service that actually works, it seems that Azure Functions can be chained nicely by generating the name of Queue well and using multiple out bindings.

reference

Recommended Posts

[Azure Functions / Python] Chain functions with Queue Storage binding
Storage I / O notes in Python with Azure Functions
Use Python and MeCab with Azure Functions
Curry arbitrary functions with Python ....
Getting Started with Python Functions
Azure Functions: Try Durable Functions for Python
10 functions of "language with battery" python
Use Azure Blob Storage from Python
File upload to Azure Storage (Python)
Image upload & download to Azure Storage. With Python + requests + REST API
Azure table storage with PTVS Flask app
Using Python and MeCab with Azure Databricks
Python functions
Try using Python with Google Cloud Functions
Working with Azure CosmosDB from Python Part.2
[GCP] Operate Google Cloud Storage with Python
[Azure] Hit Custom Vision Service with Python
Use C ++ functions from python with pybind11
VS Code + Azure Functions + Python environment construction procedure
Markov Chain Chatbot with Python + Janome (1) Introduction to Janome
Markov Chain Chatbot with Python + Janome (2) Introduction to Markov Chain
Azure External Storage MySQL Challenges with Django (PTVS)
Use Python / Django with Windows Azure Cloud Service!
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Integrate with Python
Python #stack queue
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
#Python basics (functions)
Bingo with python
Zundokokiyoshi with python
Python Easy-to-use functions
Python basics: functions
Excel with Python
Microcomputer with Python
Cast with python
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Learn Service Bus Queue with Azure SDK for Go (1)
Easily enter Azure LUIS learning texts with Python scripts
Working with Azure CosmosDB from Python (quick start digging)
Text extraction (Read API) with Azure Computer Vision API (Python3.6)