Exploring Azure Bicep Outputs: Retrieving Resource Properties

Introduction

Hello everyone! We’ve been on an exciting journey exploring Azure Bicep. We’ve seen what it is, and how it compares to ARM templates, set up our environment, dived into its syntax, and explored parameters, resources, and variables. Today, we’re going to take another step forward. We’ll explore one of the key components of Azure Bicep - Outputs. So, let’s get started!

Logging in to Azure

Before we start, let’s ensure we’re logged into Azure. Here’s how you can do it.

  1. Open your terminal or command prompt: You can use any terminal or command prompt that you’re comfortable with.
  2. Log in to Azure: Use the az login command to log in to Azure.
    # Log in to Azure
    az login
    
  3. Set your subscription: Use the az account set command to set your subscription.
    # Set your subscription
    az account set --subscription "YourSubscriptionName"
    

Azure Bicep Outputs

Outputs in Azure Bicep are the results that are returned after deployment. They allow you to retrieve the value of a property from a deployed resource. This makes your Bicep files more flexible and reusable.

You can define an output using the output keyword. Here’s an example.

output storageAccountId string = storageAccount.id

In this example, storageAccountId is an output of a type string. The value of this output is the id property of the storageAccount resource.

Here’s an example of a Bicep file that uses an output.

param storageAccountName string = 'mystorageaccount'
var location = 'westus'
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
}
output storageAccountId string = storageAccount.id

In this example, the storageAccountId output returns the id of the storageAccount resource after deployment.

Conclusion

Well done! You’ve just learned how to use outputs in Azure Bicep. Outputs are a powerful feature that allows you to retrieve the value of a property from a deployed resource. In our next session, we’ll explore modules in Azure Bicep. So, stay tuned and keep learning.