Provisioning Azure Websites with application settings using Resource Manager

Some time ago Azure Resource Manager was introduced. It allows to create Azure Resources based on an JSON file called Resource Template. You'll use it to create Azure resources which can be accessed in the new Azure Portal.

This post will not explain in details what Azure Resource Manager is or how to use Azure PowerShell. I will expect that you have some basic knowledge about Azure Powershell.

When you create a website using a default resource template you don’t have the option to add application settings. You’ll need to update the existing template with your settings. This post will explain how you’d do that.

Azure Resource Manager module was introduced with the Azure PowerShell version 0.8.0. Beginning with this version you can switch to use Azure Resource Manager module and back to Azure Service Management.

If you are not sure which Azure PowerShell version you're running this command will help:

(Get-Module Azure).Version

 

Switch to the new Azure Resource Manager by running this command:

Switch-AzureMode AzureResourceManager

       

All the PowerShell commands which will follow will be running under the Azure Resource Manager module.

First we need to find the Website Template and after that we'll update it with the needed settings.

In order to get all the templates run this script:

Get-AzureResourceGroupGalleryTemplate -Publisher Microsoft

 

Find which template you need and save it locally:

Save-AzureResourceGroupGalleryTemplate -Identity Microsoft Microsoft.WebSite.0.4.1 -path C:\azure\WebSiteWithSettings.json

 

UPDATE:
If you get the following error then it means that Get-AzureResourceGroupGalleryTemplate was deprecated. You can read more about it on GitHub.

Get-AzureResourceGroupGalleryTemplate : Missing api version query string. Allowed values are
[2014-09-01,2015-04-01,2015-10-01]
At line:1 char:1
+ Get-AzureResourceGroupGalleryTemplate -Publisher Microsoft
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzureResourceGroupGalleryTemplate], CloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.Templates.GetAzureResourceGroupGalleryTemplateCommand

 

As this command is not supported any more, we'll need to run a set of PowerShell command to get the template.

Run this command:

$webSiteTemplates = (Invoke-WebRequest -Uri "https://gallery.azure.com/Microsoft.Gallery/GalleryItems?api-version=2015-04-01&includePreview=true" | ConvertFrom-Json) | Where-Object { $_.identity -like "Microsoft.WebSite.*" }

 

Check the $webSiteTemplate variable and look for Microsoft.WebSite.[version] identity in the returned list. At the time of writing I get Microsoft.WebSite.0.4.2 and this is the only object in the collection.

Now that we've identified the template -  Microsoft.WebSite.0.4.2 we need to download it.

Check the artifacts value for the selected template. In my case it looks like this:

{@{name=website_ExistingHostingPlan; uri=https://gallery.azure.com/artifact/20151001/Microsoft.WebSite.0.4.2/DeploymentTemplates/Website_ExistingHostingPlan.json; type=template}, 
@{name=website_NewHostingPlan; uri=https://gallery.azure.com/artifact/20151001/Microsoft.WebSite.0.4.2/DeploymentTemplates/Website_NewHostingPlan.json; type=template}, 
@{name=website_NewHostingPlan_BasicStandard; uri=https://gallery.azure.com/artifact/20151001/Microsoft.WebSite.0.4.2/DeploymentTemplates/Website_NewHostingPlan_BasicStandard.json; type=template}, 
@{name=website_NewHostingPlan-Default; uri=https://gallery.azure.com/artifact/20151001/Microsoft.WebSite.0.4.2/DeploymentTemplates/Website_NewHostingPlan-Default.json; type=template}}

 

There are four templates. Choose the one which is appropriate in your case and copy the uri.
I chose website_NewHostingPlan-Default. The difference between this and website_NewHostingPlan is that for the latter you have to provide other details, like resource group, site mode, etc.

Now that we've selected the template we'll run the below command. Replace uri and fileName.

(Invoke-WebRequest -Uri [uri]).Content | Out-File -FilePath [fileName]

 

For example:

(Invoke-WebRequest -Uri https://gallery.azure.com/artifact/20151001/Microsoft.WebSite.0.4.2/DeploymentTemplates/Website_NewHostingPlan-Default.json).Content | Out-File -FilePath c:\azure\WebSiteWithSettings.json


We’ll need to edit the saved json file. First of all we’ll add a new parameter – StorageAccountKey. Open the json file in an editor and add the following in the parameters:

"storageAccountKey": {
    "type": "string"
}

Next we need to provide the configuration for the application settings. Look for siteName parameter in the resources array. It should be similar to this: "name": "[parameters('siteName')]"

In the siteName resource, add the below after the hostingEnvironment of the properties object. Don’t forget to add comma after the hostingEnvironment.

"siteConfig": {
  "appSettings": [
    {
      "name": "StorageAccountKey",
      "value": "[parameters('storageAccountKey')]"
    }]
}

Save the file and now we’re ready to generate a new website with new settings.

New-AzureResourceGroup -Name myWebSite -TemplateFile "WebsiteWithSettings.json" -Location "North Europe" -siteName myWebSite -hostingPlanName myWebSite -siteLocation "North Europe" -serverFarmResourceGroup myWebSite -StorageAccountKey "key value"

 

Open the Azure Portal and check the settings for the new created website.

You can find the full resource template file on the GitHub Gist

Comments

comments powered by Disqus