Search code examples
azure-resource-managerazure-rm-templateazure-bicep

Bicep name property and modules


I have some difficulty understanding the very prolific name property in bicep files. I've setup some modules of my own, and it looks like this in main.bicep when calling one of those:

module vas_resource_group '../common/resource_group.bicep' = {
  name: 'vas_resource_group'
  params: {
    runningNumber: runningNumber
    descriptionShort: descriptionShort
    function: function
    regionShort: regionShort
    environment: environment
    location: defaultLocation
  }
}

The module in question looks like this:

resource resource_group 'Microsoft.Resources/resourceGroups@2021-01-01' = {
  name: 'rg-${runningNumber}-${descriptionShort}-${function}-${regionShort}-${environment}'
  location: location
}

output name string = resource_group.name

Now, in main.bicep I want to know the name declared in the module, e.g. I want to know that the module decided to string together my input parameters to rg-${runningNumber}-${descriptionShort}-${function}-${regionShort}-${environment}.

In main.bicep, if I write this I am certain I will get what I'm after:

vas_resource_group.outputs.name

But this has the downside that it cannot be used everywhere, for instance this is not allowed:

scope: resourceGroup(vas_resource_group.outputs.name)

And shows the error:

This expression is being used in an assignment to the "scope" property of the "module" type, which requires a value that can be calculated at the start of the deployment. Properties of vas_resource_group which can be calculated at the start include "name".bicep(BCP120)

So, since all building blocks that make up the name are known from the beginning, I don't need the resource to be created first so the name can be calculated before deployment.

I then choose to reference the name of the resource group as follows:

scope: resourceGroup(vas_resource_group.name)

The error is gone, but I don't know if this will result in the name declared main.ps which would be vas_resource_group (invalid) or if it results in the name declared by the module which would be rg-${runningNumber}-${descriptionShort}-${function}-${regionShort}-${environment}.

Which is it, and how should I think about the name property? It seems to sometimes represent the actual name of a resource being created, while at other times (like in calling modules) not being very useful at all.

If it is the name in the current file, how can I get the name declared in the module in the best way?


Solution

  • Perhaps you could use a naming.bicep template if you don't want to define the naming patterns in PowerShell. My current approach is to use shared PowerShell script to define all the subscription, resource group, and resource names. In the past, I have used a template and that works fine but it does take a little more time to deploy.

    The following is something similar to how I used a naming template. It also means you have to bury the resource group and resource (e.g. storage account) module deployments a layer deeper in the nesting.

    main.bicep

    targetScope = 'subscription'
    
    var location = 'East US 2'
    
    module naming 'naming.bicep' = {
      name: 'naming'
      params: {
        descriptionShort: 'app'
        environment: 'n'
        function: 't'
        regionShort: 'use2' 
        runningNumber: 'aa'
      }
    }
    
    module resource_group 'resource_group.bicep' = {
      name: 'resource_group'
      params: {
        name: naming.outputs.resourceGroupName
        location: location
        resourceSuffix: naming.outputs.resourceNameSuffix
      }
    }
    

    naming.bicep

    targetScope = 'subscription'
    
    param runningNumber string
    param descriptionShort string
    param function string
    param regionShort string
    param environment string
    
    output resourceGroupName string = 'rg-${runningNumber}-${descriptionShort}-${function}-${regionShort}-${environment}'
    output resourceNameSuffix string = '${runningNumber}${descriptionShort}${function}${regionShort}${environment}'
    

    resource_group.bicep

    targetScope = 'subscription'
    
    param name string
    param location string
    param resourceSuffix string
    
    resource resource_group 'Microsoft.Resources/resourceGroups@2021-01-01' = {
      name: name
      location: location
    }
    
    module storage_account 'storage_account.bicep' = {
      scope: resourceGroup(name)
      name: 'storage_account'
      params: {
        location: location
        resourceSuffix: resourceSuffix
      }
      dependsOn: [
        resource_group
      ]
    }
    

    storage_account.bicep

    param resourceSuffix string
    param location string
    
    resource storage_account 'Microsoft.Storage/storageAccounts@2021-09-01' = {
      name: 'sa${resourceSuffix}'
      location: location
      sku: {
        name: 'Standard_LRS'
      }
      kind: 'StorageV2'
    }
    

    Result: enter image description here