Search code examples
azureazure-resource-managerazure-bicep

Azure Bicep\ARM - Get shortened region name for resource naming


I am trying to write a generic Bicep file that creates a storage account. I am trying to following the standard naming convention when creating the resource, eg: it would be something like st<storage name><location-code><###>. What I want to do is parameterize the 'location' value. If I do this though, how can I get the abbreviated 'region code' to put in the name. Eg: If I pass in Central US as the region, the name would be sttestcus001. If I put in East US, the name would be sttesteus001.

Thanks,


Solution

  • You could always maintain an object that will do the mapping for you:

    param location string = 'Central US'
    
    // Object containing a mapping for location / region code
    var regionCodes = {
      centralus: 'cus'
      eastus: 'eus'
    }
    
    // remove space and make sure all lower case
    var satinatizedLocation = toLower(replace(location, ' ', ''))
    
    // get the region code
    var regionCode = regionCodes[satinatizedLocation]