Skip to content

Instantly share code, notes, and snippets.

@russellds
Created May 25, 2023 20:40
Show Gist options
  • Save russellds/b7b2d56197535004067a35a47e548981 to your computer and use it in GitHub Desktop.
Save russellds/b7b2d56197535004067a35a47e548981 to your computer and use it in GitHub Desktop.
Bicep Standards Module
targetScope = 'subscription'
@description('The environment abbreviation.')
@allowed([
'prod'
'prev'
'demo'
'test'
'dev'
'infra'
'sbx'
'poc'
])
param environment string
@description('The index of the environment.')
param environmentIndex string = ''
@description('The application abbreviation.')
param application string
@description('[Optional] The component of the application abbreviation.')
param component string = ''
@description('The index for the component.')
param componentIndex string = ''
@description('The location for the name. Note: Use a standard Azure name for the location.')
@allowed([
'centralus'
'eastus'
'eastus2'
'northcentralus'
'southcentralus'
'westcentralus'
'westus'
'westus2'
'westus3'
])
param location string
@description('The instance of the application.')
param instance string = ''
@description('Fully Qualified Domain Name for DNS.')
param fqdn string = ''
// Naming Standard: <environment>[_index]-<application>-[component][_index]-<location>-<resource type abbreviation>-[instance]
var locationAbbr = {
centralus: 'usc'
eastus: 'use'
eastus2: 'use2'
northcentralus: 'usnc'
southcentralus: 'ussc'
westcentralus: 'uswc'
westus: 'usw'
westus2: 'usw2'
westus3: 'usw3'
}
var nameEnvironment = empty(environmentIndex) ? trim(environment) : trim(replace('${environment} ${environmentIndex}', ' ', '_'))
var nameLocation = trim(locationAbbr[location])
var nameComponent = empty(componentIndex) ? trim(component) : trim(replace('${component} ${componentIndex}', ' ', '_'))
var nameStandard = '${toLower(replace('${trim('${nameEnvironment} ${application} ${nameLocation} ${nameComponent}')}', ' ', '-'))}-[ABBR]'
var nameStandardInstance = '${nameStandard}-${instance}'
var nameAlphaNumericOnly = replace(replace(nameStandard, '_', ''), '-', '')
var nameAlphaNumericOnlyInstance = '${nameAlphaNumericOnly}${instance}'
var nameResourceGroupName = replace(nameStandard, '[ABBR]', 'rg')
var nameResourceGroupNameInstance = '${nameResourceGroupName}-${instance}'
var globalStandard = '${toLower(replace('${trim('${nameEnvironment} ${application} glbl ${nameComponent}')}', ' ', '-'))}-[ABBR]'
var globalStandardInstance = '${globalStandard}-${instance}'
var globalAlphaNumericOnly = replace(replace(globalStandard, '_', ''), '-', '')
var globalAlphaNumericOnlyInstance = '${globalAlphaNumericOnly}${instance}'
var globalResourceGroupName = replace(globalStandard, '[ABBR]', 'rg')
var globalResourceGroupNameInstance = '${globalResourceGroupName}-${instance}'
var fqdnAlphaNumericOnly = empty(fqdn) ? '' : '${trim(replace(fqdn, '.', ''))}'
var fqdnDashes = empty(fqdn) ? '' : '${trim(replace(fqdn, '.', '-'))}'
var fqdnUnderscores = empty(fqdn) ? '' : '${trim(replace(fqdn, '.', '_'))}'
var fqdnSegments = empty(fqdn) ? [] : split(fqdn, '.')
var fqdnHostName = empty(fqdnSegments) ? '' : first(fqdnSegments)
var fqdnDomain = empty(fqdn) ? '' : join(skip(fqdnSegments, 1), '.')
var tagsRequired = {
Environment: nameEnvironment
Application: application
Location: nameLocation
}
var tagsGlobalRequired = {
Environment: nameEnvironment
Application: application
Location: 'glbl'
}
var tagComponent = empty(component) ? {} : { Component: nameComponent }
var tagInstance = empty(instance) ? {} : { Instance: instance }
var tagsRegion = union(tagsRequired, tagComponent)
var tagsRegionInstance = union(tagsRegion, tagInstance)
var tagsGlobal = union(tagsGlobalRequired, tagComponent)
var tagsGlobalInstance = union(tagsGlobal, tagInstance)
output naming object = {
fqdn: {
alphaNumericOnly: fqdnAlphaNumericOnly
dashes: fqdnDashes
domain: fqdnDomain
fqdn: fqdn
hostName: fqdnHostName
underscores: fqdnUnderscores
}
global: {
alphaNumericOnly: globalAlphaNumericOnly
alphaNumericOnlyInstance: globalAlphaNumericOnlyInstance
resourceGroupName: globalResourceGroupName
resourceGroupNameInstance: globalResourceGroupNameInstance
standard: globalStandard
standardInstance: globalStandardInstance
}
names: {
alphaNumricOnly: nameAlphaNumericOnly
alphaNumricOnlyInstance: nameAlphaNumericOnlyInstance
resourceGroupName: nameResourceGroupName
resourceGroupNameInstance: nameResourceGroupNameInstance
standard: nameStandard
standardInstance: nameStandardInstance
}
tags: {
global: tagsGlobal
globalInstance: tagsGlobalInstance
region: tagsRegion
regionInstance: tagsRegionInstance
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment