Azure web services has a very good and powerful feature: Traffic Routing. Traffic Routing allows you to test your production website with live customers!! (Is this true?). This feature allows you to test new version of your website with subset of live users, so that you can verify functionality, performance and bugs if any. If you see any issues then you can move traffic back to old site else you can gradually move users to the next version of application. This will allow you to do rapid deployments without application downtime.

One are use this feature, if Azure App Service is running in Standard OR Premium mode and web application must be deployed in one or more deployment slots. Traffic routing can be configured in 2 ways:

  1. Through new Azure Portal
  2. Using PowerShell

 

In this post, we will take a look at how to do traffic routing using Azure PowerShell. If you are planning to use automated release management, then PowerShell is the way. Here is how you can route traffic using PowerShell:


$RoutingRule = New-Object Microsoft.WindowsAzure.Commands.Utilities.Websites.Services.WebEntities.RampUpRule
$RoutingRule.ActionHostName = $ActionHostName
$RoutingRule.ReroutePercentage = $ReroutePercentage;
$RoutingRule.Name = "ProductionRouting"

Set-AzureWebsite $_ -Slot Production -RoutingRules $RoutingRule
 
Here is description of parameters:
  • $ActionHostName: Name of azure web service deployment slot
  • $ReroutePercentage: Percentage of people you wan to move to new deployment

You can opt for routing 100% traffic to new deployment OR you can move it gradually. Gradual traffic movement can be done using PowerShell as well:


# Below properties can be used for ramp up or ramp down
$RoutingRule.ChangeIntervalInMinutes = 10;
$RoutingRule.ChangeStep = 5;
$RoutingRule.MinReroutePercentage = 1;
$RoutingRule.MaxReroutePercentage = 80;

BUT, how to remove traffic routing rule?

Let’s say we want to move traffic back to old website. In this case to Production website slot. There is very less documentation available about how to remove traffic routing rule. I spend lot of time researching how to do this. Here is how it can be achieved


Set-AzureWebsite $_ -Slot Production -RoutingRules @()

This will remove the traffic routing rules that you have added.

Here is parameterized version of PowerShell script for traffic routing management

Param(
[string] [Parameter(Mandatory=$true)] $WebsiteName,
[string] [Parameter(Mandatory=$true)] $DeploymentSlot,
[string] [Parameter(Mandatory=$true)] $ReroutePercentage
)

Write-Host "$WebsiteName : Moving Traffic to Slot - $DeploymentSlot => Start"

if($DeploymentSlot.ToLower() -eq "production")
{
Write-Host "Removing Routing Rules"
Set-AzureWebsite $WebsiteName -Slot Production -RoutingRules @()
}
else
{
$ActionHostName = $WebsiteName + "-" + $DeploymentSlot + ".azurewebsites.net"

Write-Host $ActionHostName

$RoutingRule = New-Object Microsoft.WindowsAzure.Commands.Utilities.Websites.Services.WebEntities.RampUpRule
$RoutingRule.ActionHostName = $ActionHostName
$RoutingRule.ReroutePercentage = $ReroutePercentage;
$RoutingRule.Name = "ProductionRouting"

# Below properties can be used for ramp up or ramp down
#$RoutingRule.ChangeIntervalInMinutes = 10;
#$RoutingRule.ChangeStep = 5;
#$RoutingRule.MinReroutePercentage = 1;
#$RoutingRule.MaxReroutePercentage = 80;

Set-AzureWebsite $WebsiteName -Slot Production -RoutingRules $RoutingRule
}

Write-Host "$WebsiteName : Moving Traffic to Slot - $DeploymentSlot => End"

Happy programming!