Route traffic for high availability of applications - Azure PowerShell
This script creates a resource group, two app service plans, two web apps, a traffic manager profile, and two traffic manager endpoints. Traffic Manager directs traffic to the application in one region as the primary region, and to the secondary region when the application in the primary region is unavailable. Before executing the script, you must change the MyWebApp, MyWebAppL1 and MyWebAppL2 values to unique values across Azure. After running the script, you can access the app in the primary region with the URL mywebapp.trafficmanager.net.
If needed, install the Azure PowerShell using the instruction found in the Azure PowerShell guide, and then run Connect-AzAccount
to create a connection with Azure.
If you don't have an Azure subscription, create an Azure free account before you begin.
Sample script
Note
We recommend that you use the Azure Az PowerShell module to interact with Azure. To get started, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
# Variables for common values
$rgName1="MyResourceGroup1"
$rgName2="MyResourceGroup2"
$location1="eastus"
$location2="westeurope"
# The values of the variables below must be unique (replace with your own names).
$webApp1="mywebapp$(Get-Random)"
$webApp2="mywebapp$(Get-Random)"
$webAppL1="MyWebAppL1"
$webAppL2="MyWebAppL2"
# Create a resource group in location one.
New-AzResourceGroup -Name $rgName1 -Location $location1
# Create a resource group in location two.
New-AzResourceGroup -Name $rgName2 -Location $location2
# Create a website deployed from GitHub in both regions (replace with your own GitHub URL).
$gitrepo="https://github.com/Azure-Samples/app-service-web-dotnet-get-started.git"
# Create a hosting plan and website and deploy it in location one (requires Standard 1 minimum SKU).
$appServicePlan = New-AzAppServicePlan -Name $webappl1 -ResourceGroupName $rgName1 `
-Location $location1 -Tier Standard
$web1 = New-AzWebApp -ResourceGroupName $rgname1 -Name $webApp1 -Location $location1 `
-AppServicePlan $webappl1
# Configure GitHub deployment from your GitHub repo and deploy once.
$PropertiesObject = @{
repoUrl = "$gitrepo";
branch = "master";
isManualIntegration = "true";
}
Set-AzResource -PropertyObject $PropertiesObject -ResourceGroupName $rgname1 `
-ResourceType Microsoft.Web/sites/sourcecontrols -ResourceName $webapp1/web `
-ApiVersion 2015-08-01 -Force
# Create a hosting plan and website and deploy it in location two (requires Standard 1 minimum SKU).
$appServicePlan = New-AzAppServicePlan -Name $webappl2 -ResourceGroupName $rgName2 `
-Location $location2 -Tier Standard
$web2 = New-AzWebApp -ResourceGroupName $rgname2 -Name $webApp2 `
-Location $location2 -AppServicePlan $webappl2
$PropertiesObject = @{
repoUrl = "$gitrepo";
branch = "master";
isManualIntegration = "true";
}
Set-AzResource -PropertyObject $PropertiesObject -ResourceGroupName $rgname2 `
-ResourceType Microsoft.Web/sites/sourcecontrols -ResourceName $webapp2/web `
-ApiVersion 2015-08-01 -Force
# Create a Traffic Manager profile.
$tm = New-AzTrafficManagerProfile -Name 'MyTrafficManagerProfile' -ResourceGroupName $rgname1 `
-TrafficRoutingMethod Priority -RelativeDnsName $web1.SiteName -Ttl 60 `
-MonitorProtocol HTTP -MonitorPort 80 -MonitorPath /
# Create an endpoint for the location one website deployment and set it as the priority target.
$endpoint = New-AzTrafficManagerEndpoint -Name 'MyEndPoint1' -ProfileName $tm.Name `
-ResourceGroupName $rgname1 -Type AzureEndpoints -Priority 1 `
-TargetResourceId $web1.Id -EndpointStatus Enabled
# Create an endpoint for the location two website deployment and set it as the secondary target.
$endpoint2 = New-AzTrafficManagerEndpoint -Name 'MyEndPoint2' -ProfileName $tm.Name `
-ResourceGroupName $rgname1 -Type AzureEndpoints -Priority 2 `
-TargetResourceId $web2.Id -EndpointStatus Enabled
Run the following command to remove the resource group, VM, and all related resources.
Remove-AzResourceGroup -Name myResourceGroup1
Remove-AzResourceGroup -Name myResourceGroup2
Script explanation
This script uses the following commands to create a resource group, web app, traffic manager profile, and all related resources. Each command in the table links to command specific documentation.
Command | Notes |
---|---|
New-AzResourceGroup | Creates a resource group in which all resources are stored. |
New-AzAppServicePlan | Creates an App Service plan. This is like a server farm for your Azure web app. |
New-AzWebApp | Creates an Azure web app within the App Service plan. |
Set-AzResource | Creates an Azure web app within the App Service plan. |
New-AzTrafficManagerProfile | Creates an Azure Traffic Manager profile. |
New-AzTrafficManagerEndpoint | Adds an endpoint to an Azure Traffic Manager Profile. |
Next steps
For more information on the Azure PowerShell, see Azure PowerShell documentation.
Additional networking PowerShell script samples can be found in the Azure Networking Overview documentation.