Lesson 1 of 6 Infrastructure as Code

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.

Sort into groups

Declarative or imperative?

Exam skills 3.5.3

Decide whether each definition states the result or the commands.

Log in to try this activity Interactive activities are free with an account.

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.defaultHostName

The 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.

Matching IaC tools to three conditions: declarative, new Azure features at once, and no state file. Bicep meets all three. Terraform is declarative but needs state management and can lag behind new resource types; an Azure CLI script is imperative and needs hand-written idempotence.
The scenario is illustrative.
Match the pairs

Parts of a Bicep deployment

Exam skills 3.5.3

Match each Bicep element with its purpose.

Log in to try this activity Interactive activities are free with an account.

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
ScopeAzure only
StateHeld by Azure
New Azure featuresAvailable on the day of release
Preview of changesWhat-if
Choose when You deploy only to Azure and want no state file.
Terraform
ScopeMany cloud and SaaS providers
StateState file that you must store
New Azure featuresDepends on the provider update
Preview of changesterraform plan
Choose when You manage several providers with one tool.
Sort into groups

Bicep or Terraform?

Exam skills 3.5.3 · 3.5.1

Decide which IaC tool each statement describes.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.