Understanding Infrastructure as Code
This lesson explains the core ideas of IaC, and introduces Bicep, ARM templates and Terraform.
Learning outcomes
After completing this lesson, you will be able to:
- Explain the benefits of IaC.
- Distinguish declarative from imperative approaches.
- Read a Bicep file.
- Compare Bicep with Terraform.
Why infrastructure as code
Manually built environments cause a familiar set of problems. Test and production differ in small ways, nobody remembers how a server was configured, and rebuilding after a disaster takes days. Infrastructure as code (IaC) solves these problems by describing infrastructure in text files, which are deployed by tools.
The benefits follow directly. The same definition creates identical environments, which removes the excuse that a problem occurs only in production. Pull requests review changes, so history shows who changed what and why. Environments can be created on demand and removed afterwards, which lowers cost.
Declarative and imperative approaches
A declarative definition states the desired result, and the tool works out how to reach it. An imperative definition lists the commands to run. Azure CLI scripts are imperative: running az vm create twice may fail or duplicate work unless the script handles this. Bicep, ARM templates and Terraform are declarative.
Idempotence is the key property that declarative tools give: applying the same definition many times has the same effect as applying it once. If nothing has changed, nothing happens, and this makes retries safe and allows drift to be corrected by redeploying.
Declarative or imperative?
Azure Resource Manager and Bicep
All Azure resource deployments pass through Azure Resource Manager (ARM). An ARM template is a JSON file describing resources. Bicep is a domain-specific language that compiles to ARM JSON, with a far cleaner syntax, and no state file, since Azure itself holds the state. New work should normally use Bicep. az bicep decompile converts existing JSON to Bicep as a starting point.
@description('Name of the web app')
param appName string
@allowed(['test', 'production'])
param environment string = 'test'
param location string = resourceGroup().location
var skuName = environment == 'production' ? 'P1v3' : 'B1'
resource plan 'Microsoft.Web/serverfarms@2024-04-01' = {
name: 'plan-${appName}'
location: location
sku: {
name: skuName
}
properties: {
reserved: true
}
kind: 'linux'
}
resource site 'Microsoft.Web/sites@2024-04-01' = {
name: appName
location: location
properties: {
serverFarmId: plan.id
httpsOnly: true
}
}
output hostName string = site.properties.defaultHostNameThe reference plan.id creates an implicit dependency, so the plan deploys first. Parameters vary between environments, variables hold calculated values, and outputs pass values to later pipeline steps. Modules are reusable Bicep files, and a .bicepparam file holds the values for one environment.
Deployment scopes and modes
Deployments can target a resource group, a subscription, a management group or a tenant. At resource group scope there are two modes. Incremental mode, the default, adds and updates resources and leaves others alone. Complete mode also deletes resources in the group that are not in the template, which is powerful and hazardous, and deployment stacks are now the preferred way to manage deletion.
The following diagram compares three infrastructure as code tools against three conditions.

Parts of a Bicep deployment
Terraform
Terraform by HashiCorp is a declarative, multi-cloud tool. It keeps a state file that records what it manages, and in a pipeline that state belongs in shared remote storage with locking, such as an Azure Storage account. The workflow is terraform init, plan and apply. It suits organizations that manage several clouds or use one tool across providers.
Bicep or Terraform?
Choosing an approach
A team deploys only to Azure, and wants a declarative tool that gives access to new Azure features immediately and has no state file to manage. Which tool should it use?
Bicep meets all three conditions. Terraform is declarative too, but it requires state management, and it can lag behind new resource types. An Azure CLI script is imperative, and so requires hand-written logic for idempotence.
Which deployment mode deletes resources that are not in the template?
Show answer
Complete mode. Incremental, the default, leaves them alone.
Exam Tips
Remember for the exam
- Declarative tools describe an end state; imperative scripts list commands.
- Idempotence makes a repeated deployment safe.
- Bicep compiles to ARM JSON and needs no state file.
- Incremental mode is the default; Complete mode deletes unlisted resources.
- Terraform needs shared remote state with locking.