Friday 14 August 2020

Change Routing of Azure Function Apps

When you implement functions in Azure by default the routing is the https://functionappname/api/functionname . However, this implementation would not let you proper organizing of routing when you have multiple function apps in your software application project. You might want to create custom routing to make your function access from other application organized appropriately. Let’s look at default behaviors and how we can setup custom routing.

When you setup your fist function app in the resource group, say with name funct-sample01, and create two functions say func01 and func02, their default URLs would be as follows (note that anonymous access allowed in below functions).

https://funct-sample01.azurewebsites.net/api/func01

https://funct-sample01.azurewebsites.net/api/func02

Then if you create another function app named func-sample02, and create a function names func01, it would have below URL.

https://func-sample02.azurewebsites.net/api/func01

This is completely OK as long as your applications are going to call the functions directly. However, if you are going to use an APIM based routing, allowing the function app access via say Azure APIM (we can discuss about APIM routing and advantageous in a later post), you may want to alter function app rout prefix to add more than, /api/ which is the default rout prefix, to allow more clarity in routing.

If explained in other words both funct-sample01 and func-sample02 app have function named func01 which has routing setup as /api/func01, which might be creating confusions and misleading the consumers of the function.

https://funct-sample01.azurewebsites.net/api/func01

https://func-sample02.azurewebsites.net/api/func01

If we can make the above functions app routing to make the rout url for each function to add function app specific rout, that would avoid the confusion. For example, below urls would make more sense for the both function app above.

https://funct-sample01.azurewebsites.net/api/sample01/func01

https://funct-sample01.azurewebsites.net/api/sample01/func02

https://func-sample02.azurewebsites.net/api/sample02/func01

This make sample01 and sample 02 to have clear routing, distinguished from each other, and would be really help full in setting up App Gateway or APIM routing for additional protection.

To make this change what you have to do is in the host.json of the function app, setup the route prefix as specified in below syntax.

{

"extensions": {

  "http": {

    "routePrefix": "customPrefix"

  }

}

}

For funct-sample01, function app the rout prefix should be set as below.

{

"extensions": {

  "http": {

    "routePrefix": "/api/sample01"

  }

}

}

You can update Azure function app host.json from azure portal itself if required using app service editor or using kudus.


Once saved the host.json function URL updates as below.

No comments:

Popular Posts