Loading...

Resource naming reloaded: Azure Policy and Bicep for the winner!

Resource naming reloaded: Azure Policy and Bicep for the winner!

Let’s solidify naming conventions with Azure Policy

In one of my last blog posts, How to use Azure Policy to enforce resource naming conventions in your DevOps pipelines I explained how one could leverage Azure Policy to standardize naming of your Azure resources. But it would be too easy, if it was that easy. We have a few issues with that:

  • some resources can’t handle the pattern that Microsoft suggests - for example Storage Accounts can only have lowercase letters and numbers in the name and only up to 24 characters
  • if we want people to stick to a certain pattern it’s easier if they can choose from a list of valid values

So we will parameterize the resource name with in Organization Unit, the appName that the requester provides us with, the location, the appType, the environment and the instance. This is how that looks like in Bicep:

param orgUnit string = 'Contoso' // Organization Unit
param appName string // Name provided by the requester
@allowed([
'weu' // Short name for West Europe
'eus' // Short name for East US
])
param shortLocation string // Selectable location for naming purposes
@allowed([
'storageaccount'
'webapp'
'functionapp'
'logicapp'
'sqlserver'
'cosmosdb'
])
param appType string // Selectable app type from a predefined array
@allowed([
'Dev'
'Demo'
'Test'
'Prod'
])
param environment string // Environment (e.g., 'Prod', 'Dev', etc.)
@allowed([
'01', '02', '03', '04', '05'
])
param instanceNumber string // Instance number or other identifier
// Mapping for short location codes to full Azure region names
var locationMapping = {
weu: 'westeurope'
eus: 'eastus'
}
// Get the full Azure location name for deployment
var fullLocation = locationMapping[shortLocation]
// Base resource name using short location code
var baseResourceName = '${orgUnit}-${appName}-${shortLocation}-${appType}-${environment}-${instanceNumber}'
// Exception for storage account
var storageAccountName = toLower('${appName}${environment}${instanceNumber}')
var truncatedStorageAccountName = length(storageAccountName) > 24 ? substring(storageAccountName, 0, 24) : storageAccountName
// Final resource name with exception handling
var resourceName = appType == 'storageaccount' ? truncatedStorageAccountName : baseResourceName
// Resource declaration for an Azure Storage Account
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = if (appType == 'storageaccount') {
name: resourceName
location: fullLocation // Use full location name for deployment
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
}
// Resource declaration for an Azure Web App
resource webApp 'Microsoft.Web/sites@2021-02-01' = if (appType == 'webapp') {
name: resourceName
location: fullLocation // Use full location name for deployment
kind: 'app'
properties: {
serverFarmId: 'your-app-service-plan-id'
}
}
// Resource declaration for an Azure Function App
resource functionApp 'Microsoft.Web/sites@2021-02-01' = if (appType == 'functionapp') {
name: resourceName
location: fullLocation // Use full location name for deployment
kind: 'functionapp'
properties: {
serverFarmId: 'your-app-service-plan-id'
}
}
// Resource declaration for an Azure Logic App
resource logicApp 'Microsoft.Logic/workflows@2019-05-01' = if (appType == 'logicapp') {
name: resourceName
location: fullLocation // Use full location name for deployment
properties: {
definition: {
'$schema': 'https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#'
actions: {}
contentVersion: '1.0.0.0'
outputs: {}
parameters: {}
triggers: {}
}
parameters: {} // Add your logic app parameters here
}
}
// Resource declaration for an Azure SQL Server
resource sqlServer 'Microsoft.Sql/servers@2021-05-01-preview' = if (appType == 'sqlserver') {
name: resourceName
location: fullLocation // Use full location name for deployment
properties: {
administratorLogin: 'your-admin-login'
administratorLoginPassword: 'your-admin-password'
}
}
// Resource declaration for an Azure Cosmos DB Account
resource cosmosDb 'Microsoft.DocumentDB/databaseAccounts@2021-04-15' = if (appType == 'cosmosdb') {
name: resourceName
location: fullLocation // Use full location name for deployment
kind: 'GlobalDocumentDB'
properties: {
databaseAccountOfferType: 'Standard'
locations:fullLocation
}
}
// Output the generated resource name and location for verification
output resourceName string = resourceName
output fullLocation string = fullLocation

Save this as main.bicep, We can now deploy this using Azure CLI:

$resourceGroup = "<your resourcegroup>"
az deployment group create --resource-group $resourceGroup --template-file main.bicep

As a result, we get nicely named resources:

Logic app in Azure Portal

In a future blog post, let’s make this work in Azure DevOps :-)

Published on:

Learn more
Luise Freese: Consultant & MVP
Luise Freese: Consultant & MVP

Recent content on Luise Freese: Consultant & MVP

Share post:

Related posts

Announcing Azure Cosmos DB Integration with LangChain.js!

Announcing Azure Cosmos DB Vector Store Integration with LangChain.js! We’re simplifying AI app development by integrating Azure Cosmos ...

2 days ago

Scale Your Database Workloads with Multishard Clusters in vCore-based Azure Cosmos DB for MongoDB

We’re excited to introduce significant enhancements to vCore-based Azure Cosmos DB for MongoDB with the release of multishard clusters preview...

2 days ago

Transform Your Azure Container Apps with Bulletproof Security

In this post, we explore how to transform your Azure Container Apps with unshakable security. Learn how to master secrets management, optimize...

2 days ago

Optimize Azure Landing Zone with Azure Virtual Network Manager IP Address Management

Optimize Azure Landing Zone with Azure Virtual Network Manager IP Address Management What you will learn from this blog This blog explores how...

3 days ago

Announcing UNLIMITED Public Preview of Metadata Caching for Azure Premium SMB/REST File Shares

Azure Files is excited to announce the Unlimited public preview of Metadata Caching for the premium SMB/REST file share tier.  Unlimited ...

3 days ago

Dominate your industry and boost performance using Azure AI tools and skilling

As AI technologies continue to rapidly evolve, developers have the exciting opportunity to stay ahead by continually learning and adapting. Wi...

3 days ago

Azure DevOps – EPICS vs FEATURES vs USER STORIES vs Tasks vs Bugs

Today, we’re diving into Epics, Features, User Stories, Tasks and Bugs and the main differences between them. You will learn when to use...

3 days ago

Discover the New Azure Cosmos DB Samples Gallery!

We are thrilled to introduce the Azure Cosmos DB Samples Gallery —your ultimate destination for top-tier Azure Cosmos DB samples, technical gu...

4 days ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy