Getting the right storage container name in a Bicep template

When creating a blob storage container in an ARM/Bicep template, as a non-nested resource, you create a top-level resource like Microsoft.Storage/storageAccounts/blobServices/containers . However, the name property of that container cannot simply be something like somecontainer, because it needs to indicate the storage account, and the blob service. So the name must be something like mystorageaccount/default/somecontaine.

If you want to use the container in other places of the Bicep template, you certainly want to refer to the container object and take the name from there, rather then using the literal container name string (and them preventing Bicep from creating the appropriate dependsOn). But referring to unfortunately gives us the 3-segment name, and we only want the last segment.

Therefore, we need to use ARM string functions to retrieve the last segment, i.e. the text after the last / character in the name.

Assuming you have a string x, you could use this Bicep expression to extract the name, i.e. for a/b/c get c:

substring(x, lastIndexOf(x,'/') + 1, length(x) - lastIndexOf(x, '/') - 1)

See below for a quick Bicep sample:

@description('The location for the deployment')
param location string = resourceGroup().location

var names = {
  uniqueHostPrefix: 'x${uniqueString(resourceGroup().id)}'
  containerName: 'SomeContainer' 

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-05-01' = {
  name: names.uniqueHostPrefix
  location: location
  kind: 'StorageV2'
  sku: { name: 'Standard_LRS' }

resource container 'Microsoft.Storage/storageAccounts/blobServices/containers@2022-05-01' =  {
  name: '${}/default/${toLower(names.containerName)}'

output theContainerNameVariable string = names.containerName
output naiveContainerName string =
output containerName string = substring(, lastIndexOf(, '/') + 1, length( - lastIndexOf(, '/') - 1)

Deploying this template would give you the following outputs (just with a different storage account name):

Last updated