2

I have following issue: I want to deploy storage account, only if it does not exist.

I check the existence with az cli deployment script:

resource checkStorageAccountExistence 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
  name: 'checkStorageAccountExistenceScript'
  location: location
  kind: 'AzurePowerShell'
  identity: identity
  properties: {
    arguments: '-storageAccountName \'${string(name)}\' -resourceGroupName \'${string(resourceGroupName)}\''
    azPowerShellVersion: '3.0'
    scriptContent: '''
      param([string]$storageAccountName, [string]$resourceGroupName)
      $provisioningState = az storage account show --name $storageAccountName --resource-group $resourceGroupName --output tsv --query 'provisioningState' 
      if ($provisioningState -eq "Succeeded") {$output = $true} else {$output = $false}
      Write-Output $output
      $DeploymentScriptOutputs = @{}
      $DeploymentScriptOutputs['storageAccountExists'] = $output
    '''
    forceUpdateTag: utcValue
    retentionInterval: 'P1D'
  }
}

and I want to deploy conditionally storage account:

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = if (checkStorageAccountExistence.properties.outputs.storageAccountExists == true){
}

But I get error:

This expression is being used in the if-condition expression, which requires a value that can be calculated at the start of the deployment. Properties of checkStorageAccountExistence which can be calculated at the start include "apiVersion", "id", "name", "type".bicep(BCP177)

Thomas
  • 24,234
  • 6
  • 81
  • 125
Wojtas.Zet
  • 636
  • 2
  • 10
  • 30

1 Answers1

1

You could create the storage account in a module:

// storage-account.bicep

param exists bool
param name string
param location string

...

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = if (!exists) {
  name: name
  location: location
  ...
}

Then invoke it like that:

// main.bicep

resource checkStorageAccountExistence 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
  name: 'checkStorageAccountExistenceScript'
  location: location
  kind: 'AzurePowerShell'
  identity: identity
  properties: {
    arguments: '-storageAccountName \'${string(name)}\' -resourceGroupName \'${string(resourceGroupName)}\''
    azPowerShellVersion: '3.0'
    scriptContent: '''
      param([string]$storageAccountName, [string]$resourceGroupName)
      $provisioningState = az storage account show --name $storageAccountName --resource-group $resourceGroupName --output tsv --query 'provisioningState' 
      if ($provisioningState -eq "Succeeded") {$output = $true} else {$output = $false}
      Write-Output $output
      $DeploymentScriptOutputs = @{}
      $DeploymentScriptOutputs['storageAccountExists'] = $output
    '''
    forceUpdateTag: utcValue
    retentionInterval: 'P1D'
  }
}

module storageAccount 'storage-account.bicep' = {
  name: 'storage-account'
  params: {
    exists: checkStorageAccountExistence.properties.outputs.storageAccountExists
    location: location
    name: name
    ...
  }
}
Thomas
  • 24,234
  • 6
  • 81
  • 125