bicep working with existing resources and creating additional resources out of it

We can use bicep with existing resource. Let's say we have a storage account called "mystorage2026jw

To do that can use existing block. Bicep is very particular about placement of the curly braces after the "equal" = sign.  Otherwise you will get BCP-007 error 


storage-account.bicep

param resourceName string = 'mytest-kv-rg'
param name string = 'mystorage2026jw'

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' existing = {
  name: name
  scope: resourceGroup(resourceName)
}

output id string = storageAccount.id
output currentStorageAccountName string = storageAccount.name


Running az bicep to validate our code is good

az bicep build --file storage-account.bicep

To see your what-if plan 

 az deployment group what-if -g mytest-kv-rg --template-file storage-account.bicep --query "properties.outputs"

And to run the actual deployment, you do 

 az deployment group create -g mytest-kv-rg --template-file storage-account.bicep --query "properties.outputs"

Let's switch gear and we would like to create a blob container from an existing storage. If that's the case, what does the code looks like? 

As you can see storageAccount is an existing resource. Then we simple create other resources using bicep declarative language

param resourceName string = 'mytest-kv-rg'
param name string = 'mystorage2026jw'

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' existing = {
  name: name
}

resource blobService 'Microsoft.Storage/storageAccounts/blobServices@2023-01-01' = {
  parent: storageAccount
  name: 'default'
  properties: {
    // Advanced Enterprise Settings
    isVersioningEnabled: true
    changeFeed: {
      enabled: true
    }
    containerDeleteRetentionPolicy: {
      enabled: true
      days: 7
    }
    deleteRetentionPolicy: {
      enabled: true
      days: 7
    }
  }
}

resource container 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-01-01' = {
  parent: blobService
  name: name
  properties: {
    publicAccess: 'None'
    metadata: {
      project: 'audit-2026'
    }
  }
}

output id string = storageAccount.id
output currentStorageAccountName string = storageAccount.name






Comments

Popular posts from this blog

vllm : Failed to infer device type

android studio kotlin source is null error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20