The new and shiny - Azure Template Specs

5 minute read

Azure Template specs is a new resource within Azure that is used for saving Azure Resource Manager Templates (ARM). With these template specifics you can later create resources based on the template within the specification.

Why use Azure Template Specs

Azure Template Specs are really useful for situations where you or the organization would like to create resources based on a specific template. This can for example be a VM resources in a specific VNET configuration or a reference architecture created fully in ARM templates.

The specs can be shared within the Azure platform as they are kept within a Resource Group. The templates can then be deployed to any subscription you have access to as everything is based on RBAC.

Setting up your first template spec

During this guide we will make use of the following ARM template, it contains a small architecture that is often used together: Azure App Service, Hosting Plan and Application Insights.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "apiName": {
      "type": "string"
    },
    "apiHostingPlan": {
      "type": "string"
    },
    "apiSkuName": {
      "type": "string",
      "defaultValue": "S1",
      "allowedValues": [
        "F1",
        "D1",
        "B1",
        "B2",
        "B3",
        "S1",
        "S2",
        "S3",
        "P1",
        "P2",
        "P3",
        "P4"
      ]
    },
    "insightsname": {
      "type": "string"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourcegroup().location]"
    }
  },
  "variables": {
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2018-11-01",
      "name": "[parameters('apiName')]",
      "location": "[parameters('location')]",
      "tags": {},

      "dependsOn": [
        "[resourceId('microsoft.insights/components/', parameters('insightsname'))]",
        "[resourceId('Microsoft.Web/serverfarms/', parameters('apiHostingPlan'))]"
      ],
      "identity": {
        "type": "SystemAssigned"
      },
      "kind": "web",
      "resources": [],
      "properties": {
        "name": "[parameters('apiName')]",
        "siteConfig": {
          "appSettings": [
            {
              "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
              "value": "[reference(resourceId('microsoft.insights/components/', parameters('insightsname')), '2015-05-01').InstrumentationKey]"
            }
          ]
        },
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('apiHostingPlan'))]",
        "clientAffinityEnabled": true
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2018-11-01",
      "name": "[parameters('apiHostingPlan')]",
      "location": "[parameters('location')]",
      "tags": {
      },
      "sku": {
        "name": "[parameters('apiSkuName')]"
      },
      "properties": {
        "name": "[parameters('apiHostingPlan')]"
      }
    },
    {
      "type": "microsoft.insights/components",
      "apiVersion": "2020-02-02-preview",
      "name": "[parameters('insightsname')]",
      "location": "[parameters('location')]",
      "tags": {},

      "properties": {
        "ApplicationId": "[parameters('apiname')]",
        "Request_Source": "IbizaWebAppExtensionCreate"
      }
    }
  ],
  "outputs": {
  }
}

Getting started

To get started with Azure Template specs a resource group needs to be added to Azure for saving the specifications. From this resource group you will be able to share the Azure Template Spec by using the default Role Based Access Model of Azure.

Let’s create a new resource group by using the following command

New-AzResourceGroup -Name rg-templatespec -Location WestEurope

With the command “New-AzTemplateSpec” a template specification is added to Azure. Now lets use it with the template from the example.

As the template specs are still in public preview we need to make sure we have the latest version of the “Az.Resources” module. Use the following command to install the latest prerelease version.

Install-Module -Name Az.Resources -AllowPrerelease -Force -AllowClobber -SkipPublisherCheck

Note: At the time of writing this article Azure Templates Specs are in preview. To make use of PowerShell make sure to install version 5.0.0 or later. When using Azure CLI use version 2.14.2 or later.

With the correct version of the PowerShell module installed the function “New-AzTemplateSpec” can be used.

New-AzTemplateSpec -Name "aztempwebsitespec" -Version 1.0 -ResourceGroupName rg-templatespec -Location WestEurope -TemplateFile ./azuredeploy.json
New-AzTemplateSpec

The function includes a “Version” parameter. This parameter is a string value that specifies the version of the file. By running the command a second time you are able to deploy a new version (when you for example have done a bug fix). You off course then have to specify a different value for the version or you would override the existing version.

The Azure Portal

After you have run the command the Azure Template Spec is created and visible via PowerShell, CLI and off course also the Azure Portal.

Get-AzTemplateSpec
Resource Group Template Spec

Via the Azure portal you are able to deploy the template to any subscription you have access to. By selecting the deploy button.

Azure Template Spec

Updating the Template Spec

As mentioned in the previous paragraph the template spec can be updated by running the exact same command as before and changing the version parameter to a different version.

Azure Template Spec Update

In the portal you are then also able to see the version and for example track the changes made to the template spec.

Azure Template Spec versions

Deploying via PowerShell

As you may also guess the template spec can also be used in PowerShell to deploy the resources. Microsoft has enhanced the “New-AzResourceGroupDeployment” function with the ability to deploy from a template specification. For this you will need to have a reference to the Azure Template Spec id as shown in the below script.

The parameters are asked during execution but the can also be added by using a parameters file and add that to the function parameter “TemplateParameterFile”.

$id = (Get-AzTemplateSpec -Name aztempwebsitespec -Version 1.1 -ResourceGroupName rg-templatespec).Versions.Id

New-AzResourceGroupDeployment -TemplateSpecId $id -ResourceGroupName template-test

Conclusion

The Azure Template Spec offers you a great way of sharing default templates with you users. This can be in company but also for example service providers.

By using the default Role Based Access model you are also able to restrict access to different kind of templates. So I would suggest that you check this new and shiny resource in Azure.