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
Launch Visual Studio Code and create two new files, main.bicep and bicepconfig.json, making sure that they are in the same folder.
Enable some preview features by configuring bicepconfig.json:
{
"experimentalFeaturesEnabled": {
"extensibility": true
}
}
- In main.bicep, add the following Bicep code, which uses the
existing
keyword to reference the group by its unique nameTestGroup-2024-05-10
and the application byTestApp-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
- 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>"
Related content
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for