Covers what infrastructure as code is, declarative versus imperative tools, idempotency, drift, environment parity and the pull-request workflow for infrastructure, compared with manual portal changes.
Infrastructure as code (IaC) means the cloud resources an app needs (App Service, Azure SQL, Key Vault, role assignments) are described in version-controlled files and created by a tool such as Bicep or Terraform, not by clicking in the portal. Good IaC is declarative (you describe the desired end state and the tool works out the API calls) and idempotent (running it again changes nothing if nothing changed). That gives you repeatable environments, a reviewable history in Git, and a way to rebuild after a disaster.
what-if or terraform plan shows it.The trade-off: IaC costs learning time and discipline (no more "quick fixes" in the portal), but it is the only way to keep more than one environment consistent.
"Infrastructure as code means we describe our cloud resources in files that live in Git next to the application, and a tool turns those files into real resources. On Azure that is usually Bicep, which compiles to ARM JSON and is sent to Azure Resource Manager, or Terraform with the azurerm provider. The important property is that it is declarative: I write 'there is a Key Vault with RBAC enabled and purge protection on', not 'run these five CLI commands'. The tool compares that desired state with what exists and only makes the difference, so running it twice is idempotent.
Click-ops works for one developer and one environment. It falls apart when you have dev, test and prod, three teams and an auditor. Nobody remembers that someone turned on 'Allow Azure services' on the SQL firewall in prod, so test behaves differently. There is no pull request explaining why a setting changed, and if a region has a bad day you cannot rebuild the environment from memory. With IaC the same template plus a parameter file per environment gives you parity, a Git history of every change, and disaster recovery that is a pipeline run.
The workflow is the same as for code: a branch, a pull request with the
what-iforplanoutput attached, review, and then a pipeline identity applies it, ideally using OIDC federation and only the RBAC it needs. The mistake I see is teams that adopt Bicep but keep fixing things in the portal 'just this once'. That is drift, and the next deployment either silently reverts the fix or surprises you. IaC only pays off if the code is the single source of truth."
| Style | Example | You describe | Re-run behaviour |
|---|---|---|---|
| Declarative | Bicep, ARM JSON, Terraform, OpenTofu | The end state | Converges; no change if nothing changed |
| Imperative | az CLI or PowerShell scripts |
The steps | May fail ("already exists") or duplicate |
| General-purpose code, declarative engine | Pulumi (C#, TypeScript) | End state, built with loops and classes | Converges like Terraform |
| Concern | Click-ops | IaC |
|---|---|---|
| Repeatability | Depends on memory and wiki pages | Same template, any environment |
| Review | None, or a change ticket | Pull request with what-if / plan output |
| Audit | Activity Log says who, not why | Git history says who, what and why |
| Disaster recovery | Rebuild by hand under pressure | Re-run the pipeline into Australia Southeast |
| Drift | Invisible | Shown by what-if / terraform plan |
| Situation | Reasonable choice |
|---|---|
| A throwaway spike in a sandbox subscription | Portal is fine; delete the resource group after |
| Learning what settings a service has | Click it once, then export or write the Bicep |
| Anything shared, long-lived or in production | Always IaC |
A typical case: a team with three environments created by hand finds their test SQL database on a different service tier and with different firewall rules from prod, so a performance test passes and prod falls over. After moving to one Bicep template with three .bicepparam files, a new environment takes about 15 minutes to create from a pipeline instead of two days of tickets, and every difference between environments is a visible parameter.
Checks that the candidate understands why IaC exists (repeatability, review, recovery), not just that it is "scripts for the cloud".
Green flags
Red flags
On Azure everything goes through Azure Resource Manager (ARM), a REST API. The portal, az CLI, Bicep and Terraform are all clients of it.
main.bicep ──bicep build──► ARM JSON ──PUT deployment──► Azure Resource Manager ──► resource providers
(Microsoft.Web, Microsoft.Sql, ...)
main.tf ──terraform plan/apply──► azurerm provider ──REST calls──► Azure Resource Manager
▲
└── state file (what Terraform thinks it manages)
The two tools track state differently, and the difference matters:
azurerm_key_vault.main) to a real Azure resource ID. It compares code, state and reality, so the state file must be stored remotely (an Azure Storage container with locking) and protected, because it can contain sensitive values.A declarative tool takes desired state D and current state C and computes a diff. Applying the diff gives D. Applying again finds no diff. That is idempotency, and it is what makes IaC safe to run on every merge. An imperative script such as az keyvault create ... has no diff step: on the second run it either errors or tries to change things it should not.
Drift happens when someone changes a resource outside the code (the portal, a CLI one-liner, an Azure Policy remediation). Consequences:
You detect drift with az deployment group what-if (Bicep) or terraform plan / terraform plan -refresh-only (Terraform), ideally on a schedule. You prevent it with RBAC: humans get Reader in production, and only the pipeline identity can write.
One template, many parameter files:
infra/
main.bicep
modules/ (keyvault.bicep, sql.bicep, containerapp.bicep)
env/
dev.bicepparam (small SKUs, public access)
prod.bicepparam (zone redundancy, private endpoints)
The differences between environments are now explicit, reviewable values instead of forgotten clicks.
feature branch ─► PR ─► CI: bicep lint / build, what-if against test ─► review
─► merge ─► deploy test ─► what-if prod ─► approval ─► deploy prod
The pipeline authenticates with OIDC workload identity federation (no stored client secret) and its service principal only has the roles it needs on the target resource groups. Templates are versioned with the app (same repo or a tagged module registry), so every release knows which infrastructure version it expects.
CanNotDelete locks, soft delete and backups, and require approval for any plan that deletes.az group export and compare the generated template with what you would write by hand.main.bicep with a storage account, deploy it twice with az deployment group create, and confirm the second run changes nothing.az deployment group what-if and read the drift it reports.dev.bicepparam and a prod.bicepparam that differ only in SKU and deploy both.A minimal Bicep file and the commands that preview and apply it. The same file serves every environment through parameters.
// main.bicep: one storage account, configured the same way everywhere
@allowed([ 'dev', 'test', 'prod' ])
param environmentName string
param location string = resourceGroup().location // e.g. australiaeast
resource storage 'Microsoft.Storage/storageAccounts@2023-05-01' = {
// uniqueString keeps the global name unique but stable between runs (idempotent)
name: 'st${environmentName}${uniqueString(resourceGroup().id)}'
location: location
sku: { name: environmentName == 'prod' ? 'Standard_ZRS' : 'Standard_LRS' }
kind: 'StorageV2'
properties: {
minimumTlsVersion: 'TLS1_2'
allowBlobPublicAccess: false
allowSharedKeyAccess: false // apps use managed identity, not account keys
}
}
output blobEndpoint string = storage.properties.primaryEndpoints.blob
// env/prod.bicepparam
using '../main.bicep'
param environmentName = 'prod'
# Preview first: shows create / modify / delete, and any drift from the portal
az deployment group what-if \
--resource-group rg-orders-prod \
--template-file main.bicep \
--parameters env/prod.bicepparam
# Apply (in a pipeline, after review). Running it again makes no changes.
az deployment group create \
--resource-group rg-orders-prod \
--template-file main.bicep \
--parameters env/prod.bicepparam
az scripts and calling it IaC. Why it is wrong: Scripts are not idempotent and cannot preview changes. Instead: Use a declarative tool (Bicep, Terraform) and keep scripts for glue such as data seeding.ForceNew attribute) makes the tool delete and recreate it. For a database or storage account that is a production outage and possibly data loss, and it can hide in a long diff. Instead: Always run what-if or terraform plan, attach it to the PR, and make the pipeline fail or require extra approval when the plan contains any delete or replace.@secure() parameters supplied at deploy time.The value of IaC is not the syntax, it is the operating model: the code is the only way changes reach production, every change is reviewed with a preview, and humans have read-only access to prod. Without that last part, a team gets the cost of IaC (learning, slower one-off changes) and none of the benefit, because drift creeps back in.
Start where it pays most: shared, long-lived and production resources, plus anything security-sensitive (role assignments, network rules, Key Vault settings). Infrastructure changes ride the same CI/CD path as the app, but ahead of it: add the new queue or setting first, ship the code that uses it, and remove the old resource in a later release, so either version of the app can run and rollback is a redeploy of the previous Git tag.
Measure the impact over months, not just on day one: time to create a new environment, the number of drift findings from a scheduled what-if or plan (should trend to zero), the change failure rate and incident count caused by configuration differences between environments, and mean time to recover when a region or resource group has to be rebuilt.
| Option | When it fits | Cost | When not to |
|---|---|---|---|
| Portal | Learning a service, sandbox spikes | Zero up front, high later | Anything shared or in production |
Imperative az scripts |
Glue steps, data seeding, one-off migrations | Low, but not idempotent | Defining long-lived resources |
| Declarative IaC (Bicep / Terraform) | Every real environment | Learning curve, pipeline setup | Throwaway experiments |
The workflow has a build job (restore, build, test, dotnet publish /t:PublishContainer to push an image tagged with the commit SHA to Azure Container Registry) and deploy jobs per environment that reuse that same image.
For Azure access I use OpenID Connect: the job declares permissions: id-token: write, GitHub issues a short-lived token, and azure/login exchanges it for an Entra token through a federated credential on an app registration or user-assigned managed identity. No client secret is stored anywhere.
Production is gated with a GitHub environment that has required reviewers, and the federated credential's subject is scoped to environment:production, so only an approved job can get production access.
The trade-off: OIDC needs one-off Entra setup per repo and environment, but removes secret rotation and leaked-secret risk entirely.
My rule is to remove secrets rather than protect them. The pipeline authenticates to Azure with OIDC / workload identity federation: the CI platform issues a short-lived token that Entra trusts, so no client secret is stored. The app authenticates to SQL, Storage and Key Vault with managed identity. Remaining secrets (third-party API keys) live in Key Vault; the pipeline deploys only a Key Vault reference such as @Microsoft.KeyVault(VaultName=...;SecretName=...) as an app setting, and the platform resolves it with the app's identity, so the value never passes through the pipeline.
Pipeline identities are least privilege: one per environment, roles on a resource group, protected by environment approvals. Secret scanning with push protection blocks committed secrets.
If a secret leaks, I revoke and rotate first, then clean logs, then check access logs. The trade-off is up-front identity setup in exchange for nothing worth stealing in the pipeline.
A typical .NET team uses a small, repeatable set of Azure services:
Everything is wired together with managed identity and Entra ID RBAC, not connection strings, and deployed with infrastructure as code. The trade-off: managed PaaS services remove patching and plumbing, but you accept each service's limits, pricing model and region availability.
Last updated 23 Nov 2026.