[PYTHON] [Notes / Updated from time to time] This and that of Azure Functions

1.First of all

This article is about things I couldn't write in Last Memorandum I will write.

The items are as follows. Also, this article will be updated from time to time.

section title Contents
2 Integrated with API Manaegment Make AzureFunctions executable at a domained URL
3 Use Azure Identity Allow access to other resources

2. Integration with API Management

In this theory, API Management is used to make the HttpTrigger of the created AzureFunctions more convenient.

2.1 Insufficiency of HttpTrigger for Azure Functions

Before creating API Management, let's talk about what Azure Functions HttpTrigger isn't enough for. It's in the URL published by AzureFunctions.

The URL obtained from the Get function URL of Azure Functions is shown below.

スクリーンショット 2020-12-28 1.09.29.png

The URL below ** has the AzureFunctions name as part of the domain **, If you need to rename AzureFunctions to something else You need to change all the URLs you are using. The problem may be small if you can handle it, Expanding this URL and forcing other developers to change it is not very good by design.

In addition, the *** part of code = ***, which is the key to restrict execution, is different for each AzureFunctions in the same FunctionApp. It is also costly to manage.

Obtained URL


https://gsy0911-function.azurewebsites.net/api/tech/qiita?code=***

2.2 Creating API Management

The following two points were insufficient for Azure Functions.

  1. The AzureFunctions name is part of the domain and is vulnerable to change
  2. code is different for each Azure Functions and management cost is high

To solve these problems, create and use API Management.

スクリーンショット 2020-12-28 1.23.23.png

Items such as names are set in Basics. Pricing tier sets Developer, but it still costs about 5,000 yen per month. If you are not using it, it is better to delete it.

スクリーンショット 2020-12-28 1.24.42.png

Monitoring is the setting for whether to output logs to Application Insights, and is set to On.

スクリーンショット 2020-12-28 1.24.57.png

Also turn on the Managed identity item.

スクリーンショット 2020-12-28 1.25.25.png

Other items will go by default. When you reach the end, press the Create button. It will take some time to complete the creation.

2.3 Azure Functions integration

Once API Management is created, register the Function App immediately. Press APIs and Function App in that order.

スクリーンショット 2020-12-28 2.06.34.png

Then, a pop-up like the one below will appear, so press Browse.

スクリーンショット 2020-12-28 2.07.33.png

It's hard to understand at first glance, but press the Configure required settings button. Then a new tab will appear from the right side, select the FunctionApp that contains the AzureFunctions you want to add.

スクリーンショット 2020-12-28 2.07.41.png

A gsy0911-function has been added to the name of the Function App, and a http_trigger item has been added to the Azure Function. In addition, HTTP methods has POST and URL template has tech/qiita. All items are the items set in Previous article.

When you're done so far, press Select at the bottom left of the screen (not shown in the photo below).

スクリーンショット 2020-12-28 2.08.06.png

Then you can see that the selected Functions App is set. Here you can change the following:

item value
Display name Value displayed in API Management
Name It doesn't seem to make any sense
API URL suffix important:After the Base URL/Value given by

If you don't mind, you can leave the default, The API URL suffix is important and should be considered carefully. If you leave gsy0911-function as shown in the picture, the URL given will be https://apim-gsy0911.azure-api.net/gsy0911-function/tech/qiita. You can also specify an empty string, so it seems better to enter nothing for everyday use. If you do not enter anything, it will be https://apim-gsy0911.azure-api.net/tech/qiita.

スクリーンショット 2020-12-28 2.08.32.png

You've successfully integrated Function App and API Management. If you press the Test tab and select http_trigger, the API name and Key will be issued at the bottom of the right screen.

スクリーンショット 2020-12-28 2.09.21.png

Here's how to call this API from Python:

import requests

URL = "https://apim-gsy0911.azure-api.net/gsy0911-function/tech/qiita"
SUBSCRIPTION_KEY = "****"

payload = {
    "name": "taro"
}

headers = {
    "Ocp-Apim-Subscription-Key": SUBSCRIPTION_KEY
}

response = requests.post(URL, json=payload, headers=headers)
print(response)

When using curl, it will be as follows.

$ curl -X POST -H "Content-Type:application/json" -H "Ocp-Apim-Subscription-Key:****" -d '{"name": "taro"}' https://apim-gsy0911.azure-api.net/gsy0911-function/tech/qiita

2.4 Issuing API Key

Almost completed in 2.3 above, When deploying the created API, it is better to issue the API Key separately.

The reason is as follows.

We will create an API Key immediately.

Press Add from Products.

スクリーンショット 2020-12-28 2.30.38.png

Then you will be taken to the screen to create a new API Key. Set the items for Display Name, Id, Published, APIs, and press Create.

スクリーンショット 2020-12-28 2.31.29.png

After creating the API Key, follow in the order of APIs, Test, http_trigger, If there is the Display Name created earlier in the place of Product, it can be created.

スクリーンショット 2020-12-28 2.32.00.png

By following the steps above, you are ready to deploy this API.

3. Use Azure Identity

(I haven't written yet)

4. Conclusion

If you notice anything, I will update it here from time to time.

Recommended Posts

[Notes / Updated from time to time] This and that of Azure Functions
[Updated from time to time] PostmarketOS related notes
Summary of vtkThreshold (updated from time to time)
[Updated from time to time] LetCode algorithm and library
Notes on machine learning (updated from time to time)
[Updated from time to time] Review of Let Code NumPy
[Updated from time to time] Summary of design patterns in Java
vtkXMLUnstructuredGridReader Summary (updated from time to time)
vtkOpenFOAMReader Summary (Updated from time to time)
This and that learned from boost.python
Engineer vocabulary (updated from time to time)
This and that of python properties
Tensorflow memo [updated from time to time]
List of my articles that may be useful in competition pros (updated from time to time)
This and that of the inclusion notation.
Private Python handbook (updated from time to time)
vtkClipPolyData / DataSet Summary (Updated from time to time)
OpenFOAM post-processing cheat sheet (updated from time to time)
progate Python learning memo (updated from time to time)
Useful help sites, etc. (updated from time to time)
From Excel file to exe and release of tool that spits out CSV
(Updated from time to time) Summary of machine learning APIs that allow you to quickly build apps by Team AI
Understand design patterns by comparing implementations in JavaScript and Java [Updated from time to time]
Machine learning python code summary (updated from time to time)
Apache settings, log confirmation, etc. (* Updated from time to time)
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
A memorandum of commands, packages, terms, etc. used in linux (updated from time to time)
matplotlib this and that
I read the Chainer reference (updated from time to time)
(Updated from time to time) Storage location of various VS Code configuration files Memorandum memo
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
This and that for using Step Functions with CDK + Python
This and that about pd.DataFrame
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
This and that using reflect
Zabbix API this and that
Summary of folders where Ruby, Python, PostgreSQL, etc. are installed on macOS (updated from time to time)
A list of functions that I came across with 100 Numpy knocks and thought "This is convenient!"
[Introduction to pytorch-lightning] Autoencoder of MNIST and Cifar10 made from scratch ♬
Introduction to TensorFlow-Summary of four arithmetic operations and basic mathematical functions
[Note] AI / machine learning / python related websites [updated from time to time]
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
Formulas and functions (updated as appropriate)
Python (from first time to execution)
This and that using NLTK (memo)
[MS Azure] Slack notification of competition information using Azure Functions and Kaggle API
From user registration of Microsoft Azure to creation of virtual machine (free tier)
A note about the functions of the Linux standard library that handles time
(Preserved version: Updated from time to time) A collection of useful tutorials for data analysis hackathons by Team AI