Is it possible to enable Always On for Azure websites through management/resource management APIs?
Asked Answered
S

3

14

I am writing some code for automatic deployment of Azure websites (including the creation of the website in Azure). I'm using the Azure Management Libraries and Azure Resource Management Libraries available in Nuget. Most of it is in place, however I have not been able to find a way to enable the "Always On" property through any of the APIs I've seen. This property can be set through the azure management portal under the Configure tab for a website.

I've checked:

  1. The properties reference on MSDN: http://msdn.microsoft.com/en-us/library/azure/dn236426.aspx
  2. The powershell APIs (get-azureresource, get-azurewebsite, ...) to see if there's a reference to Always On (there's not)
  3. The REST calls the management portal is sending, through Fiddler. Here there is a reference to Always On in a POST going to https://manage.windowsazure.com/Websites/UpdateConfig (which is not part of the management or resource management APIs as far as I know). The exact path in the JSON body that is sent is /siteConfig/AlwaysOn.

So, the question is, is it possible to enable/disable Always On through an "official" API?

Thanks!

Spinneret answered 5/11, 2014 at 11:55 Comment(0)
S
12

I believe I found the solution!

Using the resource management API, I can set the AlwaysOn property through the siteConfig object. In powershell:

Set-AzureResource -ApiVersion 2014-04-01 -PropertyObject @{"siteConfig" = @{"AlwaysOn" = $false}} -Name mywebsite -ResourceGroupName myrg -ResourceType Microsoft.Web/sites

In the resource management API in .NET it would be similar to this.

The resulting REST call, to https://management.azure.com/subscriptions/xxx/resourcegroups/yyy/providers/Microsoft.Web/sites/zzz?api-version=2014-04-01: { "location": "West Europe", "properties": { "siteConfig": { "AlwaysOn": true } }, "tags": {} }

Spinneret answered 5/11, 2014 at 12:45 Comment(4)
This indeed worked for me as well, thank you! You should mark this as the correct answer :)Firework
I can't get this to work. Are there any preconditions, like having the web site stopped ?Rowlock
See also: https://mcmap.net/q/830698/-azure-ignores-site-config-settings-in-arm-template, which shows another place to put the siteConfig block.Babineaux
Set-AzureResource or Set-AzureRmResource ?Chord
A
2

Using updated ARM (Azure Resource Manager) Powershell, v1.0+

Get-AzureRmResource: https://msdn.microsoft.com/en-us/library/mt652503.aspx

Set-AzureRmResource: https://msdn.microsoft.com/en-us/library/mt652514.aspx

# Variables - substitute your own values here
$ResourceGroupName = 'My Azure RM Resource Group Name'
$WebAppName = 'My Azure RM WebApp Name'
$ClientAffinityEnabled = $false

# Property object for nested, not exposed directly properties
$WebAppPropertiesObject = @{"siteConfig" = @{"AlwaysOn" = $true}}

# Variables
$WebAppResourceType = 'microsoft.web/sites'

# Get the resource from Azure (consider adding sanity checks, e.g. is $webAppResource -eq $null)
$webAppResource = Get-AzureRmResource -ResourceType $WebAppResourceType -ResourceGroupName $ResourceGroupName -ResourceName $WebAppName

# Set a directly exposed property, in this case whether client affinity is enabled
$webAppResource.Properties.ClientAffinityEnabled = $ClientAffinityEnabled

# Pass the resource object into the cmdlet that saves the changes to Azure
$webAppResource | Set-AzureRmResource -PropertyObject $WebAppPropertiesObject -Force
Ashleighashlen answered 23/4, 2016 at 14:20 Comment(1)
I get this error: Get-AzureRmResource : Object reference not set to an instance of an object. At line:1 char:1 + Get-AzureRmResource + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-AzureRmResource], NullReferenceException + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceCmdl etChord
C
1

For those using the .Net API, it's

var cfg = await websiteClient.Sites.GetSiteConfigAsync(site.ResourceGroup, site.Name, cancellationToken).ConfigureAwait(false);
if (!cfg.AlwaysOn.GetValueOrDefault())
{
    cfg.AlwaysOn = true;
    await websiteClient.Sites.UpdateSiteConfigAsync(site.ResourceGroup, site.Name, cfg, cancellationToken).ConfigureAwait(false);
}
Congruent answered 18/7, 2016 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.