azure-functions

Description

The azure-functions Plugin is used to integrate APISIX with Azure Serverless Function as a dynamic upstream to proxy all requests for a particular URI to the Microsoft Azure Cloud.

When enabled, the Plugin terminates the ongoing request to the configured URI and initiates a new request to Azure Functions on behalf of the client with configured authorization details, request headers, body and parameters (all three passed from the original request). It returns back the response with headers, status code and the body to the client that initiated the request with APISIX.

Attributes

NameTypeRequiredDefaultValid valuesDescription
function_uristringTrueAzure FunctionS endpoint which triggers the serverless function. For example, http://test-apisix.azurewebsites.net/api/HttpTrigger.
authorizationobjectFalseAuthorization credentials to access Azure Functions.
authorization.apikeystringFalseGenerated API key to authorize requests.
authorization.clientidstringFalseAzure AD client ID to authorize requests.
timeoutintegerFalse3000[100,…]Proxy request timeout in milliseconds.
ssl_verifybooleanFalsetruetrue/falseWhen set to true performs SSL verification.
keepalivebooleanFalsetruetrue/falseWhen set to true keeps the connection alive for reuse.
keepalive_poolintegerFalse5[1,…]Maximum number of requests that can be sent on this connection before closing it.
keepalive_timeoutintegerFalse60000[1000,…]Time is ms for connection to remain idle without closing.

Metadata

NameTypeRequiredDefaultDescription
master_apikeystringFalse“”API Key secret that could be used to access the Azure Functions URI.
master_clientidstringFalse“”Azure AD client ID that could be used to authorize the function URI.

Metadata can be used in the azure-functions Plugin for an authorization fallback. If there are no authorization details in the Plugin’s attributes, the master_apikey and master_clientid configured in the metadata is used.

The relative order priority is as follows:

  1. Plugin looks for x-functions-key or x-functions-clientid key inside the header from the request to APISIX.
  2. If not found, the Plugin checks the configured attributes for authorization details. If present, it adds the respective header to the request sent to the Azure Functions.
  3. If authorization details are not configured in the Plugin’s attributes, APISIX fetches the metadata and uses the master keys.

To add a new master API key, you can make a request to /apisix/admin/plugin_metadata with the required metadata as shown below:

  1. curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/azure-functions -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "master_apikey" : "<Your Azure master access key>"
  4. }'

Enabling the Plugin

You can configure the Plugin on a specific Route as shown below assuming that you already have your Azure Functions up and running:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "azure-functions": {
  5. "function_uri": "http://test-apisix.azurewebsites.net/api/HttpTrigger",
  6. "authorization": {
  7. "apikey": "<Generated API key to access the Azure-Function>"
  8. }
  9. }
  10. },
  11. "uri": "/azure"
  12. }'

Now, any requests (HTTP/1.1, HTTPS, HTTP2) to the endpoint /azure will invoke the configured Azure Functions URI and the response will be proxied back to the client.

In the example below, the Azure Function takes in name from the query and returns a message “Hello $name”:

  1. curl -i -XGET http://localhost:9080/azure\?name=APISIX
  1. HTTP/1.1 200 OK
  2. Content-Type: text/plain; charset=utf-8
  3. Transfer-Encoding: chunked
  4. Connection: keep-alive
  5. Request-Context: appId=cid-v1:38aae829-293b-43c2-82c6-fa94aec0a071
  6. Date: Wed, 17 Nov 2021 14:46:55 GMT
  7. Server: APISIX/2.10.2
  8. Hello, APISIX

Another example of a request where the client communicates with APISIX via HTTP/2 is shown below (make sure you have configured enable_http2: true in your default configuration file (config-default.yaml). You can do this by uncommenting the port 9081 from the field apisix.node_listen):

  1. curl -i -XGET --http2 --http2-prior-knowledge http://localhost:9081/azure\?name=APISIX
  1. HTTP/2 200
  2. content-type: text/plain; charset=utf-8
  3. request-context: appId=cid-v1:38aae829-293b-43c2-82c6-fa94aec0a071
  4. date: Wed, 17 Nov 2021 14:54:07 GMT
  5. server: APISIX/2.10.2
  6. Hello, APISIX

Configuring path forwarding

The azure-functions Plugins also supports URL path forwarding while proxying requests to the Azure Functions upstream. Extensions to the base request path gets appended to the function_uri specified in the Plugin configuration.

azure-functions - 图1IMPORTANT

The uri configured on a Route must end with * for this feature to work properly. APISIX Routes are matched strictly and the * implies that any subpath to this URI would be matched to the same Route.

The example below configures this feature:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "azure-functions": {
  5. "function_uri": "http://app-bisakh.azurewebsites.net/api",
  6. "authorization": {
  7. "apikey": "<Generated API key to access the Azure-Function>"
  8. }
  9. }
  10. },
  11. "uri": "/azure/*"
  12. }'

Now, any requests to the path azure/HttpTrigger1 will invoke the Azure Function and the added path is forwarded:

  1. curl -i -XGET http://127.0.0.1:9080/azure/HttpTrigger1\?name\=APISIX\
  1. HTTP/1.1 200 OK
  2. Content-Type: text/plain; charset=utf-8
  3. Transfer-Encoding: chunked
  4. Connection: keep-alive
  5. Date: Wed, 01 Dec 2021 14:19:53 GMT
  6. Request-Context: appId=cid-v1:4d4b6221-07f1-4e1a-9ea0-b86a5d533a94
  7. Server: APISIX/2.11.0
  8. Hello, APISIX

Disable Plugin

To disable the azure-functions Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/azure",
  4. "plugins": {},
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:1980": 1
  9. }
  10. }
  11. }'