Outputs in Bicep

This article describes how to define output values in a Bicep file. You use outputs when you need to return values from the deployed resources. You are limited to 64 outputs in a Bicep file. For more information, see Template limits.

Define output values

The syntax for defining an output value is:

output <name> <data-type> = <value>

An output can have the same name as a parameter, variable, module, or resource. Each output value must resolve to one of the data types.

The following example shows how to return a property from a deployed resource. In the example, publicIP is the symbolic name for a public IP address that is deployed in the Bicep file. The output value gets the fully qualified domain name for the public IP address.

output hostname string = publicIP.properties.dnsSettings.fqdn

The next example shows how to return outputs of different types.

output stringOutput string = deployment().name
output integerOutput int = length(environment().authentication.audiences)
output booleanOutput bool = contains(deployment().name, 'demo')
output arrayOutput array = environment().authentication.audiences
output objectOutput object = subscription()

If you need to output a property that has a hyphen in the name, use brackets around the name instead of dot notation. For example, use ['property-name'] instead of .property-name.

var user = {
  'user-name': 'Test Person'
}

output stringOutput string = user['user-name']

Conditional output

When the value to return depends on a condition in the deployment, use the the ? operator.

output <name> <data-type> = <condition> ? <true-value> : <false-value>

Typically, you use a conditional output when you've conditionally deployed a resource. The following example shows how to conditionally return the resource ID for a public IP address based on whether a new one was deployed.

To specify a conditional output in Bicep, use the ? operator. The following example either returns an endpoint URL or an empty string depending on a condition.

param deployStorage bool = true
param storageName string
param location string = resourceGroup().location

resource myStorageAccount 'Microsoft.Storage/storageAccounts@2019-06-01' = if (deployStorage) {
  name: storageName
  location: location
  kind: 'StorageV2'
  sku:{
    name:'Standard_LRS'
    tier: 'Standard'
  }
  properties: {
    accessTier: 'Hot'
  }
}

output endpoint string = deployStorage ? myStorageAccount.properties.primaryEndpoints.blob : ''

Dynamic number of outputs

In some scenarios, you don't know the number of instances of a value you need to return when creating the template. You can return a variable number of values by using the for expression.

output <name> <data-type> = [for <item> in <collection>: {
  ...
}]

The following example iterates over an array.

param nsgLocation string = resourceGroup().location
param orgNames array = [
  'Contoso'
  'Fabrikam'
  'Coho'
]

resource nsg 'Microsoft.Network/networkSecurityGroups@2020-06-01' = [for name in orgNames: {
  name: 'nsg-${name}'
  location: nsgLocation
}]

output deployedNSGs array = [for (name, i) in orgNames: {
  orgName: name
  nsgName: nsg[i].name
  resourceId: nsg[i].id
}]

For more information about loops, see Iterative loops in Bicep.

Outputs from modules

To get an output value from a module, use the following syntax:

<module-name>.outputs.<property-name>

The following example shows how to set the IP address on a load balancer by retrieving a value from a module.

publicIPAddress: {
  id: publicIP.outputs.resourceID
}

Get output values

When the deployment succeeds, the output values are automatically returned in the results of the deployment.

To get output values from the deployment history, you can use Azure CLI or Azure PowerShell script.

(Get-AzResourceGroupDeployment `
  -ResourceGroupName <resource-group-name> `
  -Name <deployment-name>).Outputs.resourceID.value

Object sorting in outputs

In JSON, an object is an unordered collection of zero or more key/value pairs. The ordering can be different depending on the implementations. For example, the Bicep items() function sorts the objects in the alphabetical order. In other places, the original ordering can be preserved. Because of this non-determinism, avoid making any assumptions about the ordering of object keys when writing code, which interacts with deployments parameters & outputs.

Next steps