Why infrastructure as code?
| Clicking in the portal | Bicep in Git |
|---|---|
| Dev and prod slowly differ | Same template, different parameters |
| Nobody remembers who changed what | Every change is a reviewed pull request |
| Rebuilding after a mistake takes days | Redeploy in minutes |
| Permissions granted by hand | Role assignments declared next to the resources |
ShopKart's platform in one Bicep file
An ADLS Gen2 lake, a Key Vault and a Data Factory with a managed identity, plus two least-privilege role assignments (the lesson 3 ideas, as code):
// ShopKart data platform: data lake + Key Vault + Data Factory, with least-privilege access.
// Deploy: az deployment group create -g rg-shopkart-dev -f main.bicep -p env=dev
@description('Environment name, used in resource names')
@allowed(['dev', 'test', 'prod'])
param env string = 'dev'
@description('Azure region for all resources')
param location string = resourceGroup().location
var suffix = uniqueString(resourceGroup().id) // stable, globally unique-ish
var storageName = take('stshopkart${env}${suffix}', 24) // storage names: 3-24 lowercase letters/numbers
var tags = { project: 'shopkart', env: env }
resource lake 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: storageName
location: location
tags: tags
sku: { name: env == 'prod' ? 'Standard_ZRS' : 'Standard_LRS' }
kind: 'StorageV2'
properties: {
isHnsEnabled: true // hierarchical namespace = ADLS Gen2
minimumTlsVersion: 'TLS1_2'
allowBlobPublicAccess: false
allowSharedKeyAccess: false // force Entra ID (no account keys)
accessTier: 'Hot'
}
}
resource blobService 'Microsoft.Storage/storageAccounts/blobServices@2023-05-01' = {
parent: lake
name: 'default'
}
resource lakeContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-05-01' = {
parent: blobService
name: 'lake'
}
resource vault 'Microsoft.KeyVault/vaults@2023-07-01' = {
name: 'kv-shopkart-${env}-${take(suffix, 6)}'
location: location
tags: tags
properties: {
tenantId: subscription().tenantId
sku: { family: 'A', name: 'standard' }
enableRbacAuthorization: true // permissions via Azure RBAC, not access policies
enableSoftDelete: true
enablePurgeProtection: true
}
}
resource factory 'Microsoft.DataFactory/factories@2018-06-01' = {
name: 'adf-shopkart-${env}-${take(suffix, 6)}'
location: location
tags: tags
identity: { type: 'SystemAssigned' } // the factory's own identity
}
// Built-in role IDs (the same in every Azure tenant)
var storageBlobDataContributor = 'ba92f5b4-2d11-453d-a403-e96b0029c9fe'
var keyVaultSecretsUser = '4633458b-17de-408a-b874-0445c86b69e6'
resource factoryCanUseLake 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: lake
name: guid(lake.id, factory.id, storageBlobDataContributor)
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', storageBlobDataContributor)
principalId: factory.identity.principalId
principalType: 'ServicePrincipal'
}
}
resource factoryCanReadSecrets 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: vault
name: guid(vault.id, factory.id, keyVaultSecretsUser)
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', keyVaultSecretsUser)
principalId: factory.identity.principalId
principalType: 'ServicePrincipal'
}
}
output lakeUrl string = lake.properties.primaryEndpoints.dfs
output factoryName string = factory.name
output vaultUri string = vault.properties.vaultUri| Feature | Where in the file |
|---|---|
| Parameters with allowed values | @allowed(['dev','test','prod']) param env |
| Computed names | uniqueString(resourceGroup().id), take(..., 24) for storage's 24-character limit |
| Conditions | ZRS redundancy only when env == 'prod' |
| Child resources | parent: lake β blobServices β containers |
| Security defaults | isHnsEnabled, TLS 1.2, no public blobs, no shared keys, Key Vault RBAC + purge protection |
| Role assignments | scope + guid(...) name + built-in role ID + the factory's principalId |
| Outputs | lakeUrl, factoryName, vaultUri for the next pipeline step |
Lint and build (offline)
$az bicep install # or install the Bicep CLI directly$bicep lint main.bicep$bicep build main.bicep --outfile main.json
(lint: no warnings)
resources: 7
- Microsoft.Storage/storageAccounts
- Microsoft.Storage/storageAccounts/blobServices
- Microsoft.Storage/storageAccounts/blobServices/containers
- Microsoft.KeyVault/vaults
- Microsoft.DataFactory/factories
- Microsoft.Authorization/roleAssignments
- Microsoft.Authorization/roleAssignments
parameters: env, location
outputs: lakeUrl, factoryName, vaultUriNo warnings, and the build produced an ARM template with all 7 resources. This is the role assignment as ARM JSON, generated for us:
{
"type": "Microsoft.Authorization/roleAssignments",
"apiVersion": "2022-04-01",
"scope": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]",
"name": "[guid(resourceId('Microsoft.Storage/storageAccounts', variables('storageName')), resourceId('Microsoft.DataFactory/factories', format('adf-shopkart-{0}-{1}', parameters('env'), take(variables('suffix'), 6))), variables('storageBlobDataContributor'))]",
"properties": {
"roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', variables('storageBlobDataContributor'))]",
"principalId": "[reference(resourceId('Microsoft.DataFactory/factories', format('adf-shopkart-{0}-{1}', parameters('env'), take(variables('suffix'), 6))), '2018-06-01', 'full').identity.principalId]",
"principalType": "ServicePrincipal"
},
"dependsOn": [
"[resourceId('Microsoft.DataFactory/factories', format('adf-shopkart-{0}-{1}', parameters('env'), take(variables('suffix'), 6)))]",
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]"
]
}What the linter catches
A first draft with typical mistakes:
// A first draft with common mistakes
param adminPassword string = 'P@ssw0rd123'
param unusedParam string = 'x'
resource lake 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: 'stshopkartdev'
location: 'centralindia'
sku: { name: 'Standard_LRS' }
kind: 'StorageV2'
properties: {
isHnsEnabled: true
allowBlobPublicAccess: true
}
}
output connection string = 'DefaultEndpointsProtocol=https;AccountName=${lake.name};AccountKey=${lake.listKeys().keys[0].value}'draft.bicep(2,7) : Warning no-unused-params: Parameter "adminPassword" is declared but never used. [https://aka.ms/bicep/linter-diagnostics#no-unused-params]
draft.bicep(2,7) : Warning secure-secrets-in-params: Parameter 'adminPassword' may represent a secret (according to its name) and must be declared with the '@secure()' attribute. [https://aka.ms/bicep/linter-diagnostics#secure-secrets-in-params]
draft.bicep(3,7) : Warning no-unused-params: Parameter "unusedParam" is declared but never used. [https://aka.ms/bicep/linter-diagnostics#no-unused-params]
draft.bicep(16,98) : Warning outputs-should-not-contain-secrets: Outputs should not contain secrets. Found possible secret: function 'listKeys' [https://aka.ms/bicep/linter-diagnostics#outputs-should-not-contain-secrets]Four real warnings: an unused parameter, a password not marked @secure() (and with a default value!), and an output that would print the storage account key into deployment history. But notice what it didn't flag: the hard-coded location and allowBlobPublicAccess: true. The linter is about code quality; use Azure Policy, PSRule for Azure or Checkov for security rules.
Deploy it (on real Azure)
$az group create -n rg-shopkart-dev -l centralindia$az deployment group what-if -g rg-shopkart-dev -f main.bicep -p env=dev # preview$az deployment group create -g rg-shopkart-dev -f main.bicep -p env=dev # deploy
Deployments are idempotent: run the same template again and Azure only changes what differs. In CI/CD, run bicep lint and what-if on pull requests and deploy from the main branch, the same flow as the ADF course's lesson 11.
π― Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1What is Bicep and how does it relate to ARM templates?Fresher+
Q2How do you grant a Data Factory access to storage in Bicep?1β3 yrs+
Q3How do you handle environments with Bicep?1β3 yrs+
Q4How do you keep secrets out of IaC?3+ yrs+
Key takeaways
- βOur whole platform fits in one readable Bicep file.
- βLint and build run offline; what-if before every deploy.
- βPermissions belong in code, next to the resources.
- βThe linter caught 4 issues, and missed public access: add policy scans.
Frequently asked questions
What is Bicep?
Microsoft's language for declaring Azure resources. It compiles to ARM JSON templates but is much shorter and easier to read, with type checking, a linter and editor support. Deploy with az deployment group create.
Bicep or Terraform?
Bicep is Azure-only, has day-zero support for new Azure features and needs no state file. Terraform works across clouds and SaaS tools and keeps a state file. Azure-only teams often pick Bicep; multi-cloud teams pick Terraform. The concepts (declare resources, parameters, plan/what-if) are the same.
How do I preview changes before deploying?
az deployment group what-if -g <rg> -f main.bicep shows which resources would be created, changed or deleted, like terraform plan. Run it in pull requests.
Does the linter catch security problems?
Some (secrets in parameters or outputs), but not all: it didn't flag our public blob access. Add Azure Policy, PSRule for Azure or Checkov to scan templates for security and best practices.
π§ Quick check: Bicep
1. What does bicep build produce?
2. Which linter rule flagged adminPassword?
3. Why use guid(lake.id, factory.id, roleId) as a role assignment name?
4. Preview what a deployment would change?
Checking your accessβ¦