Daniel's Tech Blog

Cloud Computing, Cloud Native & Kubernetes

Publishing Azure Functions on AKS through the Ambassador API gateway

In my last blog post I introduced you to the Ambassador Kubernetes-native microservices API gateway as an ingress controller running on Azure Kubernetes Service.

-> https://www.danielstechblog.io/running-ambassador-api-gateway-on-azure-kubernetes-service/

Today I would like to show you how to publish an Azure Function running on Kubernetes through the Ambassador API gateway. It is nothing special compared to a web application or anything else providing a HTTP REST API. But you must pay attention to one setting of the Ambassador configuration.

As you may know serverless function having a so-called cold start time.

-> https://azure.microsoft.com/en-us/blog/understanding-serverless-cold-start/

That said it needs a couple of seconds for the function to start up and processing the request after a long idle time.

Per default Ambassador has a timeout of 3 seconds and its very likely that you receive the upstream request timeout error message by Ambassador in the mentioned scenario.

> curl -X POST http://helloworld.trafficmanager.net/api/helloWorld -H "Content-Type: application/json" --data-raw '{"input": "Azure Functions in a Container on AKS"}'
upstream request timeout

So, you would like to prevent this user experience, when running Azure Functions on Kubernetes and published through Ambassador. All what needs to be done is to add the timeout_ms setting to the service object. The timeout is specified in milliseconds.

apiVersion: v1
kind: Service
metadata:
  name: helloworld-function-figlet
  labels:
    app: function-figlet
  annotations:
    getambassador.io/config: |
      ---
        apiVersion: ambassador/v1
        kind: Mapping
        name: helloworld-function-figlet
        prefix: /
        host: helloworld.trafficmanager.net
        service: helloworld-function-figlet
        timeout_ms: 20000
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: function-figlet

I recommend setting the timeout to 20 seconds in such a scenario.

> curl -X POST http://helloworld.trafficmanager.net/api/helloWorld -H "Content-Type: application/json" --data-raw '{"input": "Azure Functions in a Container on AKS"}'
    _                          _____                 _   _
   / \    _____   _ _ __ ___  |  ___|   _ _ __   ___| |_(_) ___  _ __  ___
  / _ \  |_  / | | | '__/ _ \ | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __|
 / ___ \  / /| |_| | | |  __/ |  _|| |_| | | | | (__| |_| | (_) | | | \__ \
/_/   \_\/___|\__,_|_|  \___| |_|   \__,_|_| |_|\___|\__|_|\___/|_| |_|___/

 _                  ____            _        _
(_)_ __     __ _   / ___|___  _ __ | |_ __ _(_)_ __   ___ _ __    ___  _ __
| | '_ \   / _` | | |   / _ \| '_ \| __/ _` | | '_ \ / _ \ '__|  / _ \| '_ \
| | | | | | (_| | | |__| (_) | | | | || (_| | | | | |  __/ |    | (_) | | | |
|_|_| |_|  \__,_|  \____\___/|_| |_|\__\__,_|_|_| |_|\___|_|     \___/|_| |_|

    _    _  ______
   / \  | |/ / ___|
  / _ \ | ' /\___ \
 / ___ \| . \ ___) |
/_/   \_\_|\_\____/
WordPress Cookie Notice by Real Cookie Banner