Reading Time: 3 minutes

ICYMI, Template Specs have been out since 2021! Template Specs allow the creation of a versioned library consisting of ARM Templates on Azure. Then using any standard Azure tool, like ARM Templates, Portal, PowerShell, or Azure CLI you can use these Template Specs to issue a deployment of Azure resources.

But why Template Specs?

  • Template Specs allows for saving of templates for future use
  • Allows for repeatable & consistent deployments
  • Create a library of reusable and approved templates
  • Templates are published based on approved configurations
  • Template Specs represent a complete template
  • Allow for versioning and access control using Azure RBAC
  • Stored as a separate resource type in Azure
  • Support for Modules and CI/CD integration

Getting started

Create a Bicep Template

Create a standard Bicep template, and use variables, decorators, parameters, or even modules. I would say it’s a matter of taste how you want to structure the Template, and also a matter of development standards.

Create and Publish a Template Spec

To create and publish a Template Spec that contains Bicep code, you need to use either Azure PowerShell or CLI. These are the only available options at the moment for the Bicep code. The reason is that using the Azure Portal, the only option we get is ARM JSON. When publishing a Template Spec that contains Bicep code, what actually happens is that the Bicep code transpile down to JSON.

Publishing/deployment flow for Template Specs

Create and Publish a Template Spec using Azure PowerShell

When publishing a Template Spec, some input is required, such as the destination Resource Group, a Resource name, a Display name, a brief Description, the version, and last but not least the bicep template file.

New-AzTemplateSpec `
  -Name StorageAccountWithPrivateEndpoint `
  -Location westeurope `
  -ResourceGroupName myrg `
  -DisplayName 'Storage Account with Private Endpoint' `
  -Description 'This template spec creates a storage account configured with a private endpoint' `
  -Version '1.0' `
  -TemplateFile main.bicep

Create and publish a Template Spec using Azure CLI

az ts create \
  --name StorageAccountWithPrivateEndpoint \
  --location westeurope \
  --resource-group myrg \
  --display-name "Storage Account with Private Endpoint" \
  --description "This template spec creates a storage account configured with a private endpoint" \ `
  --version 1.0 \
  --template-file storage.bicep

Verify Template Spec creation

Upon completion, head back to the Azure Portal, and over to the Resource Group in which the Template Specs were created within. You should now be able to see the published Template Spec.

Template Specs resources in the Azure Portal

Link Template Specs in Bicep modules

After creating a Template Spec, you can link to that in a module. Specify the template spec in the following format:

Here’s what it looks like.

module modulename 'ts:<subscription-id>/<resource-group-name>/<template-spec-resource-name>:<version>' = {
}

The resource id of the Template Spec can be found using Azure Portal, PowerShell, or CLI.

Updating a Published Template Spec

To update a Template Spec in case of refactoring or changes occur in the Bicep template, follow the same instructions as mentioned above and only change the version number e.g -Version ‘2.0’.

Limitations

The size of a template spec is limited to an approximated 2 MB. If a Template Spec size exceeds the limit, you will get the TemplateSpecTooLarge error code.

Thanks for reading my blog!

Feel free to drop your comment or question below.