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 |
In this theory, API Management is used to make the HttpTrigger
of the created AzureFunctions more convenient.
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.
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=***
The following two points were insufficient for Azure Functions.
code
is different for each Azure Functions and management cost is highTo solve these problems, create and use API Management
.
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.
Monitoring
is the setting for whether to output logs to Application Insights, and is set to On.
Also turn on the Managed identity
item.
Other items will go by default.
When you reach the end, press the Create
button.
It will take some time to complete the creation.
Once API Management is created, register the Function App immediately.
Press APIs
and Function App
in that order.
Then, a pop-up like the one below will appear, so press Browse
.
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.
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).
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
.
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.
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
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
.
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
.
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.
By following the steps above, you are ready to deploy this API.
(I haven't written yet)
If you notice anything, I will update it here from time to time.
Recommended Posts