Reference existing resources

Bicep allows you to reference an existing resource by its unique name, using the existing keyword. Microsoft Graph resources (like applications or groups) that were originally created outside of a Bicep file deployment are unlikely to have their unique name property set. This article describes how to backfill these resources' unique name property so that they can be redeployed or referenced in a Bicep file.

Important

Microsoft Graph Bicep is currently in PREVIEW. See the Supplemental Terms of Use for Microsoft Azure Previews for legal terms that apply to Azure features that are in beta, preview, or otherwise not yet released into general availability.

Prerequisites

  • Ensure you have the least privileged permissions or roles to update existing resources, or that you are an owner of the resource. Consult Least privileged roles by task and Default user permissions to see what roles you need to be assigned.
  • Install either Azure CLI or Azure PowerShell.

Backfill the unique name

The following example shows how to backfill unique name for a group and an application using the following Azure CLI or Azure PowerShell, assuming that you have already signed-in.

az rest --method patch --url 'https://graph.microsoft.com/v1.0/groups/<replace-with-ID-of-your-group>' --body '{\"uniqueName\": \"TestGroup-2024-05-10\"}' --headers "content-type=application/json"
az rest --method patch --url 'https://graph.microsoft.com/v1.0/applications/<replace-with-ID-of-your-application>' --body '{\"uniqueName\": \"TestApp-2024-05-10\"}' --headers "content-type=application/json"

Use an existing resource in Bicep

  1. Launch Visual Studio Code and create two new files, main.bicep and bicepconfig.json, making sure that they are in the same folder.

  2. Enable some preview features by configuring bicepconfig.json:

  {
      "experimentalFeaturesEnabled": {
      "extensibility": true
      }
  }
  1. In main.bicep, add the following Bicep code, which uses the existing keyword to reference the group by its unique name TestGroup-2024-05-10 and the application by TestApp-2024-05-10:
  provider microsoftGraph

  @description('Group to use')
  param groupName string = 'TestGroup-2024-05-10'

  @description('App to use')
  param appName string = 'TestApp-2024-05-10'

  resource group 'Microsoft.Graph/groups@v1.0' existing = {
      uniqueName: groupName
  }

  resource application 'Microsoft.Graph/applications@v1.0' existing = {
      uniqueName: appName
  }

  output groupId string = group.id
  output applicationId string = application.id
  1. Deploy the Bicep file using Azure CLI or Azure PowerShell
  az deployment group create --resource-group exampleRG --template-file main.bicep
  DeploymentName          : main
  ResourceGroupName       : exampleRG
  ProvisioningState       : Succeeded
  Timestamp               : 18/04/2024 16:16:42
  Mode                    : Incremental
  TemplateLink            :
  Parameters              :
                          Name             Type                       Value
                          ===============  =========================  ==========
                          groupName        String                     "TestGroup-2024-05-10"
                          appName          String                     "TestApp-2024-05-10"


  Outputs                 :
                          Name             Type                       Value
                          ===============  =========================  ==========
                          group-id         String                     "<ID-of-your-group>"
                          app-id           String                     "<ID-of-your-app>"