How to use the existing keyword for accessing Azure resources in your Bicep files
When working with Azure Bicep, you can safely reference an existing Azure resource using the existing
keyword. This eliminates the risk of unintended modifications or deletions.
Here’s the syntax to retrieve an existing resource:
resource nameOfResourceType 'Microsoft....' existing = {
name: nameOfResource
}
The key functionality here is the existing
keyword, which ensures the script only accesses the resource without altering it.
Example: Accessing a Function App
To retrieve an existing Function App, you can use the following Bicep code:
resource existingFunctionApp 'Microsoft.Web/sites' existing = {
name: 'functionAppName'
}
Using the Function App in Configuration
Once the Function App is retrieved, you can utilize it in subsequent configurations. For instance, to set a value in the Application Settings of the Function App, use the parent
property and set it to the name of the Function App you received with existing
:
resource appSettings 'Microsoft.Web/sites/config' = {
name: 'appsettings'
parent: existingFunctionApp
properties: {
key: 'value'
}
}
This approach ensures you can get a read-only resource and pass its properties in following resources in your Bicep files.