# Azure Functions

The `azure-functions` plugin makes an HTTP-triggered [Azure Function](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger) 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[​](#examples "Direct link to 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](https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-http-endpoint) 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[​](#invoke-a-key-protected-function "Direct link to Invoke a Key-Protected Function")

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

* Admin API
* ADC
* Ingress Controller

```
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
```

adc.yaml

```
services:

  - name: azure-functions-service

    labels:

      docs-example: azure-functions

    routes:

      - name: azure-functions-route

        uris:

          - /azure

        plugins:

          azure-functions:

            function_uri: "${AZURE_FUNCTION_URI}"

            authorization:

              apikey: "${AZURE_FUNCTION_KEY}"

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

Preview the scoped changes:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=azure-functions
```

Review `diff.yaml`, then synchronize the labeled service:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=azure-functions
```

[Set up the Ingress Controller and gateway](https://docs.api7.ai/ingress-controller/set-up-ingress-controller-and-gateway.md) in the `aic` namespace before continuing. Replace the function hostname and access key in either manifest.

* Gateway API
* APISIX CRD

azure-functions-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

  ports:

    - port: 80

      targetPort: 80

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: azure-functions-route

spec:

  plugins:

    - name: azure-functions

      config:

        function_uri: https://your-function-host/api/HttpExample

        authorization:

          apikey: replace-with-function-key

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: azure-functions-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /azure

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: azure-functions-route

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

azure-functions-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

  ports:

    - port: 80

      targetPort: 80

---

apiVersion: apisix.apache.org/v2

kind: ApisixPluginConfig

metadata:

  namespace: aic

  name: azure-functions-route

spec:

  ingressClassName: apisix

  plugins:

    - name: azure-functions

      enable: true

      config:

        function_uri: https://your-function-host/api/HttpExample

        authorization:

          apikey: replace-with-function-key

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: azure-functions-route

spec:

  ingressClassName: apisix

  http:

    - name: azure-functions-route

      match:

        paths:

          - /azure

      backends:

        - serviceName: httpbin-external-domain

          servicePort: 80

      plugin_config_name: azure-functions-route
```

Apply the configuration:

```
kubectl apply -f azure-functions-ic.yaml
```

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[​](#preserve-a-matched-route-sub-path "Direct link to 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.

* Admin API
* ADC
* Ingress Controller

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
```

In `adc.yaml`, replace the route URI and plugin configuration:

adc.yaml (route excerpt)

```
uris:

  - /azure/*

plugins:

  azure-functions:

    function_uri: "${AZURE_FUNCTION_BASE_URI}"

    authorization:

      apikey: "${AZURE_FUNCTION_KEY}"
```

Preview the updated service:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=azure-functions
```

Review `diff.yaml`, then synchronize the labeled service:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=azure-functions
```

Update the plugin and route with the Kubernetes API used by the deployment:

* Gateway API
* APISIX CRD

azure-functions-ic.yaml

```
apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: azure-functions-route

spec:

  plugins:

    - name: azure-functions

      config:

        function_uri: https://your-function-host/api

        authorization:

          apikey: replace-with-function-key

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: azure-functions-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: PathPrefix

            value: /azure/

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: azure-functions-route

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

azure-functions-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixPluginConfig

metadata:

  namespace: aic

  name: azure-functions-route

spec:

  ingressClassName: apisix

  plugins:

    - name: azure-functions

      enable: true

      config:

        function_uri: https://your-function-host/api

        authorization:

          apikey: replace-with-function-key

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: azure-functions-route

spec:

  ingressClassName: apisix

  http:

    - name: azure-functions-route

      match:

        paths:

          - /azure/*

      backends:

        - serviceName: httpbin-external-domain

          servicePort: 80

      plugin_config_name: azure-functions-route
```

Apply the updated configuration:

```
kubectl apply -f azure-functions-ic.yaml
```

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[​](#protect-function-keys "Direct link to 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.
