Home > Net >  Azure-Bicep: How to add blob container to Storage account
Azure-Bicep: How to add blob container to Storage account

Time:10-27

So, I have a StorageAccount.bicep

//StorageAccount.bicep
param storageAccountSku string = 'Standard_LRS'
param storageAccountName string
param storageAccountType string = 'StorageV2'

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-08-01' = {
  name: storageAccountName
  location: location
  tags: tags
  sku: {
    name: storageAccountSku
  }
  kind: storageAccountType
}

output name string = storageAccount.name

In the main.bicep, it calls StorageAccount.bicep to create container.

module storage_account_1 'modules/storageAccount.bicep' = {
  name: 'storage_account1'
  scope: rg
  params: {
    storageAccountSku: 'Standard_LRS'
    storageAccountType: 'StorageV2'
    storageAccountName: 'storage_account1"
}
}

2nd storage account

var storageName = toLower('${envType}${rgNameUid}${toLower(app)}')
    module storage_account_2 'modules/storageAccount.bicep' = {
      name: 'storage_account2'
      scope: rg
      params: {
        storageAccountSku: 'Standard_LRS'
        storageAccountType: 'StorageV2'
        storageAccountName: storageName 
    }
    }

Then I have another helper.bicep that can set or override any settings.

//helper.bicep
resource my_storage 'Microsoft.Storage/storageAccounts@2022-05-01' existing = {
  name: storageName 
}

My question is how to add blob container to the 2nd storage account?

I tried to create blobContainer.bicep and then call it from main.bicep.

param storageAccountName string
param storageAccount object
@description('Name of the blob container to deploy.')
param blobContainerNames array
var cleanedContainerNames = [for x in blobContainerNames: toLower(x)]

resource blobContainers 'Microsoft.Storage/storageAccounts/blobServices/containers@2022-05-01' =  [for containerName in cleanedContainerNames: {
  name: '${storageAccountName}/default/${containerName}'
  dependsOn: [
    storageAccount
  ]
  properties: {
    publicAccess: 'None'
  }
}]

output name string = blobContainers.name

But it errors in following line, can I declarer param as module to pass in?

dependsOn: [
    storageAccount
  ]

Update:

Can I do something like this?

//helper.bicep

resource my_storage 'Microsoft.Storage/storageAccounts@2022-05-01' existing = {
  name: storageName 
}

//Add the following lines
resource storagecontainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2022-05-01' = {
   name: '${storageName}/default/mycontainer'
   dependsOn: [
       my_storage 
     ]
   }

CodePudding user response:

You could always add the containers creation inside the storage module:

// StorageAccount.bicep
param storageAccountName string
param location string = resourceGroup().location
param tags object = {}
param storageAccountSku string = 'Standard_LRS'
param storageAccountType string = 'StorageV2'
param containerNames array = []

// Create storage
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-08-01' = {
  name: storageAccountName
  location: location
  tags: tags
  sku: {
    name: storageAccountSku
  }
  kind: storageAccountType
}

resource blobService 'Microsoft.Storage/storageAccounts/blobServices@2021-06-01' = {
  name: '${storageAccount.name}/default'
}

// Create containers if specified
resource containers 'Microsoft.Storage/storageAccounts/blobServices/containers@2021-06-01' = [for containerName in containerNames: {
  parent: blobService
  name: !empty(containerNames) ? '${toLower(containerName)}' : 'placeholder'
  properties: {
    publicAccess: 'None'
    metadata: {}
  }
}]

output name string = storageAccount.name
  • Related