Template Spec for bicep This quick start describes how to create and deploy a template spec with a Bicep file.
Template specs let you share deployment templates without needing to give users access to change the Bicep file. This template spec example uses a Bicep file to deploy a storage account.
When you create a template spec, the Bicep file is transpiled into JavaScript Object Notation (JSON). The template spec uses JSON to deploy Azure resources. Currently, you can't use the Microsoft Azure portal to import a Bicep file and create a template spec resource.
Using Powershell:
Create a new resource group to contain the template spec.
Copy New-AzResourceGroup `
- Name templateSpecRG `
- Location westus2
Create the template spec in that resource group. Give the new template spec the name storageSpec .
Copy New-AzTemplateSpec `
- Name storageSpec `
- Version "1.0" `
- ResourceGroupName templateSpecRG `
- Location westus2 `
- TemplateFile "C:\templates\main. Bicep"
Using Bicep:
Copy the following template and save it to your computer as main.bicep .
Copy param templateSpecName string = 'storageSpec'
param templateSpecVersionName string = '1.0'
@ description( 'Location for all resources.' )
param location string = resourceGroup() .location
resource createTemplateSpec 'Microsoft.Resources/templateSpecs@2021-05-01' = {
name: templateSpecName
location: location
}
resource createTemplateSpecVersion 'Microsoft.Resources/templateSpecs/versions@2021-05-01' = {
parent: createTemplateSpec
name: templateSpecVersionName
location: location
properties: {
mainTemplate: {
'$schema' : 'https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#'
'contentVersion' : '1.0.0.0'
'metadata' : {}
'parameters' : {
'storageAccountType' : {
'type' : 'string'
'defaultValue' : 'Standard_LRS'
'metadata' : {
'description' : 'Storage account type.'
}
'allowedValues' : [
'Premium_LRS'
'Premium_ZRS'
'Standard_GRS'
'Standard_GZRS'
'Standard_LRS'
'Standard_RAGRS'
'Standard_RAGZRS'
'Standard_ZRS'
]
}
'location' : {
'type' : 'string'
'defaultValue' : '[resourceGroup().location]'
'metadata' : {
'description' : 'Location for all resources.'
}
}
}
'variables' : {
'storageAccountName' : '[format(\'{0}{1}\', \'storage\', uniqueString(resourceGroup().id))]'
}
'resources' : [
{
'type' : 'Microsoft.Storage/storageAccounts'
'apiVersion' : '2021-08-01'
'name' : '[variables(\'storageAccountName\')]'
'location' : '[parameters(\'location\')]'
'sku' : {
'name' : '[parameters(\'storageAccountType\')]'
}
'kind' : 'StorageV2'
'properties' : {}
}
]
'outputs' : {
'storageAccountNameOutput' : {
'type' : 'string'
'value' : '[variables(\'storageAccountName\')]'
}
}
}
}
}
Use Azure PowerShell or Azure CLI to create a new resource group.
Copy New-AzResourceGroup `
- Name templateSpecRG `
- Location westus2
Copy az group create \
-- name templateSpecRG \
-- location westus2
Create the template spec in that resource group. The template spec name storageSpec and version number 1.0
are parameters in the Bicep file.
Copy New-AzResourceGroupDeployment `
- ResourceGroupName templateSpecRG `
- TemplateFile "C:\templates\main.bicep"
Copy az deployment group create \
-- resource - group templateSpecRG \
-- template - file "C:\templates\main.bicep"
Deploy template spec
Use the template spec to deploy a storage account. This example uses the resource group name storageRG
. You can use a different name, but you'll need to change the commands.
Create a resource group to contain the new storage account.
Copy New-AzResourceGroup `
- Name storageRG `
- Location westus2
Get the resource ID of the template spec.
Copy $id = ( Get-AzTemplateSpec - ResourceGroupName templateSpecRG - Name storageSpec - Version "1.0" ).Versions.Id
Deploy the template spec.
Copy New-AzResourceGroupDeployment `
- TemplateSpecId $id `
- ResourceGroupName storageRG
You provide parameters exactly as you would for a Bicep file deployment. Redeploy the template spec with a parameter for the storage account type.
Copy New-AzResourceGroupDeployment `
- TemplateSpecId $id `
- ResourceGroupName storageRG `
- storageAccountType Standard_GRS
Using Bicep:
To deploy a template spec using a Bicep file, use a module. The module links to an existing template spec. For more information, see file in template spec .
Copy the following Bicep module and save it to your computer as storage.bicep .
Copy module deployTemplateSpec 'ts:<subscriptionId>/templateSpecRG/storageSpec:1.0' = {
name: 'deployVersion1'
}
Replace <subscriptionId>
in the module. Use Azure PowerShell or Azure CLI to get your subscription ID.
Copy ( Get-AzContext ).Subscription.Id
Copy az account show -- query "id" -- output tsv
Use Azure PowerShell or Azure CLI to create a new resource group for the storage account.
Copy New-AzResourceGroup `
- Name storageRG `
- Location westus2
Copy az group create \
-- name storageRG \
-- location westus2
Deploy the template spec with Azure PowerShell or Azure CLI.
Copy New-AzResourceGroupDeployment `
- ResourceGroupName storageRG `
- TemplateFile "C:\templates\storage.bicep"
Copy az deployment group create \
-- resource - group storageRG \
-- template - file "C:\templates\storage.bicep"
You can add a parameter and redeploy the template spec with a different storage account type. Copy the sample and replace your storage.bicep file. Then, redeploy the template spec deployment.
Copy module deployTemplateSpec 'ts:<subscriptionId>/templateSpecRG/storageSpec:1.0' = {
name: 'deployVersion1'
params: {
storageAccountType: 'Standard_GRS'
}
}
Update template spec version
Using PowerShell:
Create a new version of the template spec.
Copy New-AzTemplateSpec `
- Name storageSpec `
- Version "2.0" `
- ResourceGroupName templateSpecRG `
- Location westus2 `
- TemplateFile "C:\templates\main.bicep"
To deploy the new version, get the resource ID for the 2.0
version.
Copy $id = ( Get-AzTemplateSpec - ResourceGroupName templateSpecRG - Name storageSpec - Version "2.0" ).Versions.Id
Deploy the new version and use the storageNamePrefix
to specify a prefix for the storage account name.
Copy New-AzResourceGroupDeployment `
- TemplateSpecId $id `
- ResourceGroupName storageRG `
- storageNamePrefix "demo"