Tutorial: Add outputs to Azure Resource Manager Bicep file
In this tutorial, you learn how to return a value from your deployment. You use outputs when you need a value from a deployed resource. This tutorial takes 7 minutes to complete.
Note
This article contains Bicep examples. Bicep is currently in preview. For more information, see Project Bicep.
Prerequisites
We recommend that you complete the tutorial about variables, but it's not required.
You must have Visual Studio Code with the Bicep extension, and either Azure PowerShell or Azure CLI. For more information, see Bicep tools.
Review Bicep file
At the end of the previous tutorial, your Bicep file had the following contents:
@minLength(3)
@maxLength(11)
param storagePrefix string
@allowed([
'Standard_LRS'
'Standard_GRS'
'Standard_RAGRS'
'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'
param location string = resourceGroup().location
var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
name: uniqueStorageName
location: location
sku: {
name: storageSKU
}
kind: 'StorageV2'
properties: {
supportsHttpsTrafficOnly: true
}
}
It deploys a storage account, but it doesn't return any information about the storage account. You might need to capture properties from a new resource so they're available later for reference.
Add outputs
You can use outputs to return values from the deployment. For example, it might be helpful to get the endpoints for your new storage account.
The following example shows the change to your Bicep file to add an output value. Copy the whole file and replace your Bicep file with its contents.
@minLength(3)
@maxLength(11)
param storagePrefix string
@allowed([
'Standard_LRS'
'Standard_GRS'
'Standard_RAGRS'
'Premium_LRS'
])
param storageSKU string = 'Standard_LRS'
param location string = resourceGroup().location
var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'
resource stg 'Microsoft.Storage/storageAccounts@2019-04-01' = {
name: uniqueStorageName
location: location
sku: {
name: storageSKU
}
kind: 'StorageV2'
properties: {
supportsHttpsTrafficOnly: true
}
}
output storageEndpoint object = stg.properties.primaryEndpoints
There are some important items to note about the output value you added.
The type of returned value is set to object
, which means it returns a template object.
To get the primaryEndpoints
property from the storage account, you use the storage account symbolic name. The autocomplete feature of the Visual Studio Code presents you a full list of the properties:
Deploy Bicep file
You're ready to deploy the Bicep file and look at the returned value.
If you haven't created the resource group, see Create resource group. The example assumes you've set the bicepFile
variable to the path to the Bicep file, as shown in the first tutorial.
New-AzResourceGroupDeployment `
-Name addoutputs `
-ResourceGroupName myResourceGroup `
-TemplateFile $bicepFile `
-storagePrefix "store" `
-storageSKU Standard_LRS
In the output for the deployment command, you'll see an object similar to the following example only if the output is in JSON format:
{
"dfs": "https://storeluktbfkpjjrkm.dfs.core.chinacloudapi.cn/",
"web": "https://storeluktbfkpjjrkm.z19.web.core.chinacloudapi.cn/",
"blob": "https://storeluktbfkpjjrkm.blob.core.chinacloudapi.cn/",
"queue": "https://storeluktbfkpjjrkm.queue.core.chinacloudapi.cn/",
"table": "https://storeluktbfkpjjrkm.table.core.chinacloudapi.cn/",
"file": "https://storeluktbfkpjjrkm.file.core.chinacloudapi.cn/"
}
Note
If the deployment failed, use the verbose
switch to get information about the resources being created. Use the debug
switch to get more information for debugging.
Review your work
You've done a lot in the last six tutorials. Let's take a moment to review what you have done. You created a Bicep file with parameters that are easy to provide. The Bicep file is reusable in different environments because it allows for customization and dynamically creates needed values. It also returns information about the storage account that you could use in your script.
Now, let's look at the resource group and deployment history.
Sign in to the Azure portal.
From the left menu, select Resource groups.
Select the resource group you deployed to.
Depending on the steps you did, you should have at least one and perhaps several storage accounts in the resource group.
You should also have several successful deployments listed in the history. Select that link.
You see all of your deployments in the history. Select the deployment called addoutputs.
You can review the inputs.
You can review the outputs.
You can review the JSON template.
Clean up resources
If you're moving on to the next tutorial, you don't need to delete the resource group.
If you're stopping now, you might want to clean up the resources you deployed by deleting the resource group.
- From the Azure portal, select Resource group from the left menu.
- Enter the resource group name in the Filter by name field.
- Select the resource group name.
- Select Delete resource group from the top menu.
Next steps
In this tutorial, you added a return value to the Bicep file. In the next tutorial, you'll learn how to export a JSON template and use parts of that exported template in your Bicep file.