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.
Create a Queue called piyo
in Queue Storage from the" + Queue "button.
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))
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'))
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,
③ Azure Functions is started with Queue as a trigger.
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 ...)
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.
Recommended Posts