Skip to main content

Azure Functions

The azure-functions plugin makes an HTTP-triggered Azure Function the effective upstream for a route. It forwards the client method, headers, query parameters, and body, then returns the function's status, headers, and body to the client.

The plugin can supply an Azure Functions access key without exposing it to callers. Use a function-specific key when one route invokes one function, and protect the route with appropriate client authentication and authorization.

Examples​

The examples show how to invoke a key-protected HTTP trigger and how to preserve a matched route sub-path when calling the function.

Prepare an HTTP-triggered function before configuring the gateway:

  1. Follow Microsoft's HTTP endpoint guide to create a Function App and an HTTP trigger named HttpExample. If the portal does not support editing the selected runtime, develop the function locally and publish it to the Function App.
  2. Set the trigger's authorization level to Function instead of Anonymous.
  3. Implement the function to read the name query parameter and return Hello, <name>!, which gives the examples a predictable result.
  4. In the function's Function keys page, copy a function-specific key. Avoid the host and master keys because they grant access beyond this function.
  5. Select Get function URL and copy the URL. Remove the ?code=... query parameter because the plugin sends the key in the x-functions-key header.

Export the function endpoint, Function App API base, and key. Replace the example host with the hostname from the copied function URL:

export AZURE_FUNCTION_URI="https://your-function-host/api/HttpExample"
export AZURE_FUNCTION_BASE_URI="https://your-function-host/api"
export AZURE_FUNCTION_KEY="replace-with-function-key"

The gateway must be able to resolve and reach the function hostname. If the Function App uses access restrictions or private networking, allow traffic from every gateway instance.

Invoke a Key-Protected Function​

Configure a route that invokes the HTTP trigger and adds its function-specific access key.

curl "http://127.0.0.1:9180/apisix/admin/routes/azure-functions-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"uri": "/azure",
"plugins": {
"azure-functions": {
"function_uri": "${AZURE_FUNCTION_URI}",
"authorization": {
"apikey": "${AZURE_FUNCTION_KEY}"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}
EOF

The configurations above include a conventional fallback backend. When azure-functions handles the request, the gateway does not contact that backend; verify the Azure Function response rather than treating a backend response as confirmation that the plugin ran.

Send a request with a name:

curl --include "http://127.0.0.1:9080/azure?name=API7"

The gateway returns the function's response:

HTTP/1.1 200 OK
...
Hello, API7!

Preserve a Matched Route Sub-Path​

Update the same route to match sub-paths and use the Function App API base. The plugin appends the path matched by * to the configured function URI.

Update the route path:

curl "http://127.0.0.1:9180/apisix/admin/routes/azure-functions-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{"uri":"/azure/*"}'

Replace the plugin configuration with the Function App API base:

curl "http://127.0.0.1:9180/apisix/admin/routes/azure-functions-route/plugins/azure-functions" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"function_uri": "${AZURE_FUNCTION_BASE_URI}",
"authorization": {
"apikey": "${AZURE_FUNCTION_KEY}"
}
}
EOF

Send a request whose suffix identifies the function:

curl --include "http://127.0.0.1:9080/azure/HttpExample?name=API7"

The plugin appends /HttpExample to the Function App API base and returns the function response:

HTTP/1.1 200 OK
...
Hello, API7!

Protect Function Keys​

Azure Functions access keys are shared secrets. Prefer a function-specific key for a route that invokes one function. A host key grants access to every function in the Function App, and the master key also grants access to administrative runtime endpoints.

The plugin uses credentials configured on the plugin instance before plugin metadata. Metadata provides defaults for instances that omit authorization. If an incoming request contains either x-functions-key or x-functions-clientid, the plugin injects neither configured nor metadata credentials and forwards the client values unchanged. Do not allow untrusted clients to supply these headers when the gateway is responsible for function authentication.

Rotate keys through the Function App and update every plugin instance or metadata entry that stores the old value. Keep function endpoints on HTTPS and use Azure network restrictions or private networking when the deployment requires them.