Bicep diagnostic code - BCP422

This diagnostic occurs when you call a function on a resource that may or may not exist, which could cause the deployment to fail.

Description

A resource of type <resource-type> may or may not exist when this function is called, which could cause the deployment to fail.

Level

Warning

Examples

The following example raises the diagnostic because the resource may or may not exist when this function is called, which could cause the deployment to fail.

param createStorage bool
param location string = resourceGroup().location

resource stg 'Microsoft.Storage/storageAccounts@2025-01-01' = if (createStorage) {
  name: 'sa${uniqueString(resourceGroup().id)}'
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
}

@secure()
output out object = stg.listKeys()

If you're certain the resource will be created, you can use the null-forgiving operator to tell the compiler it's safe:

param createStorage bool
param location string = resourceGroup().location

resource stg 'Microsoft.Storage/storageAccounts@2023-05-01' = if (createStorage) {
  name: 'sa${uniqueString(resourceGroup().id)}'
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
}

@secure()
output out object = stg!.listKeys()

Next steps

For more information about Bicep diagnostics, see Bicep core diagnostics.