You use outputs in ARM JSON templates to return value(s) from the deployment. Returning output from a template is not mandatory but useful in scenarios such as when deploying templates through a CI/CD pipeline or when creating templates as reusable modules. To support this, Bicep language has an outputs element.
Output
Syntax for adding output element to Bicep is:
1
output <output-identifier> <output-type> = <literal> or <expression>
You can add any number of outputs in a Bicep file. The output-identifier along with the literal value or the value from the expression will be returned after a successful deployment. As you will learn in the next part of this series, Bicep modules make use of outputs from a template deployment. The type of data that you return from output can be an integer, a string, an array, an object, or a boolean value.
Here is a simple example that returns the storage account Id once the template deployment is complete.
You can use the resource accessor to assign the value of storage account resource Id to the output. In this example, sa is the resource instance name within the deployment. So, sa.id will retrieve the resource Id of the storage account.
You can deploy this template using az deployment group create --template-file=C:\sandbox\bicep\main.bicep --resource-group bicep. The output from this command will include an outputs object.
Similar to resources and resource properties, you can use iterations with output as well. There are three types of iterations that you learned about in the previous part of this series. The syntax for using any of those three methods is similar for outputs as well.
1
2
3
output <output-identifier> <output-type> = [for index in range(<start>, <stop>):
<output-properties>
]
In the last part of this series, you learned how to use loop index based iteration. The same example can be extended to add output of all storage account resource Ids. Here is how you do that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
param saCount int = 3
resource sa 'Microsoft.Storage/storageAccounts@2019-04-01' = [for index in range(0, saCount): {
name: '${index}stg${uniqueString(resourceGroup().id)}'
location: resourceGroup().location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {
accessTier: 'Hot'
}
}]
output saId array = [for i in range(0, saCount): resourceId('Microsoft.Storage/storageAccounts', '${i}stg${uniqueString(resourceGroup().id)}')]
In the ARM template deployment output, you should see: