how to reference a naming convention module that doesn't require a scope a subscription scope?? #17659
Replies: 1 comment
-
I migrated away from using modules or importing variables from modules that contain scope functions to avoid the issue of being restricted to a target scope for utility modules. Instead, I created a series of user-defined functions which I can import() whenever needed that, when combined with a solid predicable naming convention, allow me to easily generate consistent resource strings based on the resource group name, either via initially creating it from values pulled in externally or via I've been thinking about a more efficient replacement for a while, either inside Bicep or a via a pre-deployment script, but it's been serving me well for a while now. // modules/naming.bicep
metadata name = 'Naming Convention Functions'
metadata description = 'User-defined functions for generating strings based on a set naming convention.'
metadata owner = 'Engineering'
targetScope = 'subscription' // irrelevant as the module isn't run and there are no scope functions
@export()
@description('Get naming convention strings from a resource group name.') // I tend to only include what I need
func getConventionStrings(name string) object => {
product: split(name, '-')[1]
environment: split(name, '-')[2]
geoCode: split(name, '-')[3]
workload: split(name, '-')[?4]
hyphenated: join(skip(split(name, '-'), 1), '-') // for resources supporting hyphens
hyphenatedCore: join(take(skip(split(name, '-'), 1), 3), '-') // minus workload for greater use cases
name: name // the original resource group name passed, just for testing
unhyphenated: join(skip(split(name, '-'), 1), '')
}
@export()
@description('Define resource name prefixes based on a resource group name.')
func getResourcePrefixes(name string) object => {
appInsights: 'appi-${getConventionStrings(name).hyphenated}'
applicationGateway: 'agw-${getConventionStrings(name).hyphenated}'
keyVault: 'kv${getConventionStrings(name).unhyphenated}' // even though it supports hyphens, same length limit as storage accounts
managedCluster: 'aks-${getConventionStrings(name).hyphenated}'
sqlServer: 'sql-${getConventionStrings(name).hyphenated}'
storageAccount: 'st${getConventionStrings(name).unhyphenated}'
userAssignedIdentity: 'id-${getConventionStrings(name).hyphenated}'
virtualNetwork: 'vnet-${getConventionStrings(name).hyphenated}'
}
@description('Naming convention structure, purely for reference.')
type namingConvention = {
@description('''
Azure resource type abbreviation.
Aligned with https://learn.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/resource-abbreviations
''')
resource: string
@description('Organisational or project identifier.')
product: string
@description('Environment identifier.')
environment: ('dev' | 'test' | 'prod')
@description('''
Geographical location identifier.
Aligned with https://github.com/Azure/terraform-azurerm-caf-enterprise-scale/blob/main/modules/connectivity/locals.geo_codes.tf.json
''')
geoCode: ('eus' | 'wus' | 'uks')
@description('Optional workload code indicating the function or application.')
workload: string?
@description('Optional numeric or descriptive identifier for differentiation.')
instance: string?
} And here's a basic demo of it in action, which can be for any target scope as the naming module isn't being run. // main.bicep
targetScope = 'resourceGroup'
@description('Import naming user-defined functions from the naming module.')
import {
getConventionStrings
getResourcePrefixes
} from './modules/naming.bicep'
@description('''
Hardcoded resource group name for demonstration.
If scoped to the resource group, then I'd simply pass `resourceGroup().name` to the function.
''')
var resourceGroupName = 'rg-acme-prod-eus-app'
@description('Array of app names.')
var apps = [
'web'
'api'
]
@description('Get the prefix name for the AKS cluster.')
output aksName string = getResourcePrefixes(resourceGroupName).managedCluster
@description('Get the environment name.')
output environmentName string = getConventionStrings(resourceGroupName).environment
@description('Create a custom string using the naming convention.')
output customString string = 'group-${getConventionStrings(resourceGroupName).hyphenated}-support'
@description('Generate a Storage Account name for each app.')
output storageAccounts array = [for app in apps: '${getResourcePrefixes(resourceGroupName).storageAccount}${app}'] Which provides the following output: {
"aksName": {
"type": "String",
"value": "aks-acme-prod-eus-app"
},
"customString": {
"type": "String",
"value": "group-acme-prod-eus-app-support"
},
"environmentName": {
"type": "String",
"value": "prod"
},
"storageAccounts": {
"type": "Array",
"value": [
"stacmeprodeusappweb",
"stacmeprodeusappapi"
]
}
} Hope it's of some use to you or anyone else. Also very happy for any/all feedback. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is your feature request related to a problem? Please describe.
i have a naming convention module that only exists to set names for other resource but i cant reference it at subscripcion scope, its fails
Describe the solution you'd like
what are the workaround that we can do to fix this ? this is my current workaround, im getting a random rg to deploy this naming module
Beta Was this translation helpful? Give feedback.
All reactions