Search code examples
azurekubernetesazure-aksazure-api-managementapim

How to dynamically update the backend/outbound URL in Azure APIM


we are using AIPM with AKS.I am trying to dynamically redirect the target URL based on the availability. For e.g say I have couple of backend url configured with load balancer. and when I hit the load balancer I want to to go to endpoint 1 or endpoint 2 based on the availability

I am tryin to do something similar as mentioned here in the HA proxy https://www.haproxy.com/blog/failover-and-worst-case-management-with-haproxy/. Since we already using APIM I am assuming it would be a overkill to have HA proxy just for this unless the feature is not available. while I can understand we can rewrite the URL or set the backend dynamically I am not sure if APIM can decide whether the URL is up and if not it can fallback to other URL's mentioned in the config something available in the link above.

What my current understand is that I can use the policy and if there is an error I can use an error policy where I can redirect. but that is for one condition. Any suggestion or pointers would be helpful.


Solution

  • You can react on the response code from your backend API and set a new backend url. This might not be perfect but it will get you started. In this example I assume that you get a 500 response from your backend if it's "down":

    <backend>
        <retry condition="@(context.Response.StatusCode == 500)" count="2" interval="1" first-fast-retry="true">
            <choose>
                <when condition="@(context.Response != null && context.Response.StatusCode == 500)">
                    <set-backend-service base-url="https://my-other-service.azurewebsites.net" />
                </when>
                <otherwise />
            </choose>
            <forward-request />
        </retry>
    </backend>