cookbook.geuer-pollmann.de
  • Introduction
  • Command line utilities
    • bash scripting
    • cURL command line utility
    • ffmpeg - Processing Media
    • JOSE from the command line
    • jq
    • Misc. command line tools
    • Zettelkasten / Markdown
  • Azure
    • Logging in to Azure
    • Working with the REST API
    • Tracing HTTP requests with Fiddler
    • Upload a file from bash
    • Azure CLI
    • terraform
    • Azure Logic Apps
    • Azure Web Apps
    • Azure Python code snippets
    • SSH keys in ARM
    • Minimal "Azure AD Workload identity federation"
    • Federated credentials from GitHub and GitLab pipelines to Azure
    • Azure Marketplace Metered Billing- Picking the correct ID when submitting usage events
    • Manually submitting values to the Azure Metering API
    • How can a publisher/ISV access the data plane of an Azure managed application?
    • The checkZonePeers API: Is your availability zone "1" equal to my "1"?
    • Token authentication with "Azure Verizon Premium CDN"
    • Getting the right storage container name in a Bicep template
    • Event-sourcing into working memory to improve data access latency
    • Postgrex on Azure - Connecting to Azure PostgreSQL from Elixir
  • Productivity
    • Excel
    • Desktop Setup
    • Time handling and Scheduling
    • Elgato from the shell
    • Typora
Powered by GitBook
On this page
Edit on GitHub
  1. Azure

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 container.name 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: '${storageAccount.name}/default/${toLower(names.containerName)}'
}

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

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

PreviousToken authentication with "Azure Verizon Premium CDN"NextEvent-sourcing into working memory to improve data access latency

Last updated 2 years ago

2023-01-30--storage-container-names