Parameters, variables, expressions, and functions¶
Inputs and constraints¶
Parameters describe what callers may change. Use strings for names, booleans for explicit choices, integers with bounds for capacity, and arrays/objects for structured data. Prefer narrow types or allowed values where applicable.
@description('Organization or workload naming prefix.')
@minLength(2)
@maxLength(12)
param prefix string
@description('Deployment environment.')
@allowed(['dev', 'test', 'prod'])
param environment string
@description('Azure region.')
param location string = resourceGroup().location
var suffix = uniqueString(resourceGroup().id, prefix, environment)
var tags = { environment: environment, managedBy: 'bicep' }
module network '../../0_core-infrastructure/virtual-network/main.bicep' = {
name: 'network'
params: { name: '${prefix}-${environment}-vnet', location: location, tags: tags }
}
module storage '../../1_storage-databases/storage-account/main.bicep' = {
name: 'storage'
params: { name: 'st${suffix}', location: location, containers: ['documents'], coolAfterDays: 30 }
}
module monitoring '../../6_monitoring-management/log-analytics/main.bicep' = {
name: 'monitoring'
params: { name: '${prefix}-${environment}-logs', location: location, retentionDays: environment == 'prod' ? 90 : 30 }
}
module identity '../../4_identity-security/managed-identity/main.bicep' = {
name: 'identity'
params: { name: '${prefix}-${environment}-identity', location: location }
}
output networkId string = network.outputs.id
output storageId string = storage.outputs.id
output workspaceId string = monitoring.outputs.id
output principalId string = identity.outputs.principalId
@allowed rejects misspelled environment values. @minLength and @maxLength
protect the naming prefix. These checks complement, rather than replace,
Azure resource-specific naming rules.
Variables and expressions¶
var names a derived expression rather than another user input.
uniqueString(resourceGroup().id, prefix, environment) is deterministic, not
random and not a password. String interpolation combines readable names.
The ternary expression gives production longer log retention.
Array loops avoid copied resource blocks.
resourceGroup() and subscription() describe the current scope.
resourceId() and subscriptionResourceId() construct IDs; they do not
prove that a resource exists. guid() gives stable assignment names.
empty() supports optional configuration. Avoid hardcoded tenant,
subscription, and principal IDs.
Parameter files¶
using './main.bicep'
param prefix = 'learn'
param environment = 'dev'
param location = 'eastus2'
A .bicepparam file links to a template with using. Keep published samples
non-sensitive and environment-neutral; the sample region is a changeable lab
choice, not a guarantee of availability. Put real organization-specific values
into ignored *.local.bicepparam files.
Secrets¶
For a module that genuinely needs a secret input, use @secure() and retrieve
the value through an approved Key Vault/deployment mechanism. Do not output
it, commit it, or use a secure decorator to justify an insecure default.
These modules avoid password inputs by using SSH public keys and identities.
Exercise: add a test parameter file, compile it with az bicep build-params,
and verify that environment = 'testing' fails the allowed-values check.