IaC: declarative vs imperative explained

Updated October 9, 2026

Infrastructure as code (IaC) means describing infrastructure in text files kept in version control, so a change is a commit instead of a series of clicks. A declarative definition states the end result and lets the tool work out the steps; an imperative script lists the steps and leaves you to handle the starting state. If an exam question says “desired state”, “repeatable” or “the same result no matter how many times it runs”, the answer is declarative and idempotent. Bicep, ARM templates and Terraform are declarative. A Bash or PowerShell script that calls az commands in order is imperative.

The same task written both ways

The task: a resource group must contain one storage account with a given name and SKU.

The imperative version is a script: create the resource group, create the storage account, set the tier. Run it twice and the result depends on how each command treats an object that already exists: some fail, some create a duplicate, some silently overwrite settings. Safe reruns need the author to know each case and add checks. Run it against an environment that has drifted and it does nothing about the drift unless the author anticipated that case. Every “what if it is already there” branch is code the author has to write and maintain.

The declarative version is a Bicep file that contains the storage account resource with its name, SKU and kind. Deploy it to an empty resource group and the account is created. Deploy it again and nothing changes. Change the SKU in the file and deploy again, and the account is updated. The file always describes the final state, and Azure Resource Manager (ARM) decides what to create, update or leave alone. Microsoft’s Bicep documentation says exactly this: Bicep files are idempotent, so deploying the same file many times gives the same resource types in the same state, and you maintain one file that represents the desired state instead of many files for updates.

Decision table

QuestionDeclarativeImperative
What do you write?The end stateThe steps
Running it twiceSafe: no change if already correctSafe only where the author checked or guarded each step
Ordering of dependenciesThe tool works it outYou order the commands
Preview before applyingBuilt in (what-if, terraform plan)Only by writing a dry-run yourself
Handles driftRedeploy brings the target back to the definitionOnly the cases the script anticipates
Typical examplesBicep, ARM templates, Terraform, Kubernetes manifestsBash, PowerShell, az and aws command sequences
Best forProvisioning resourcesOne-off tasks, glue, tasks with no declarative resource type

Microsoft’s guidance on IaC states a preference for declarative definition files where possible: they describe what an environment needs, not how to build it, and they reduce the debt of maintaining imperative deployment scripts. The same page defines idempotence as an operation that always produces the same result, so a deployment sets the target to the same configuration whatever its starting state.

Where state lives and why exams ask

Declarative tools must compare “what I want” with “what exists”. They differ in where they keep what exists.

  • Bicep and ARM templates keep no state file you manage. Azure itself is the record. The Bicep overview lists “no state or state files to manage” as a benefit, and the what-if operation checks the live environment to preview changes (az deployment group what-if).
  • Terraform records real infrastructure in a state file and uses it to decide what must change. Its plan step lists what will be created, changed or destroyed, and apply carries it out. The state file is a managed asset: shared, locked and protected, because it can contain sensitive values.
  • Ansible describes the desired state of a machine or service through modules. The glossary defines idempotent as an operation whose result is the same performed once or repeatedly. Ansible is agentless and push-based and is used more for configuring what runs inside machines than for creating cloud resources (see Ansible vs Terraform).

Declarative does not mean “cannot delete anything by surprise”. ARM has two deployment modes. Incremental is the default: resources in the template are added or updated and other resources in the group are left alone. Complete deletes resources in the resource group that are not in the template. Microsoft now recommends incremental mode and says to use deployment stacks when deletion is wanted, because complete mode will be gradually deprecated. Whatever mode you use, run what-if first.

Incremental mode has a second trap. A redeployed resource has all properties reapplied: properties you leave out of the template are reset to defaults, not preserved. The template is the final state, not a patch.

Which exams test this

ExamHow it appearsGuide on this site
AZ-104Deploy and manage Azure compute resources (20–25%): interpret and modify ARM templates and Bicep files, deploy resources with them, export a deployment as a template and convert an ARM template to BicepCompute resources
AZ-400Design and implement build and release pipelines (50–55%): define an IaC strategy with source control and automated testing and deployment, implement desired state configuration with ARM, Bicep, Azure Automation State Configuration and Azure Machine Configuration, and use Azure Deployment EnvironmentsBuild and release pipelines
AZ-900Describe Azure management and governance (30–35%): describe infrastructure as code, and Azure Resource Manager with ARM templatesManagement and governance
AZ-305Indirect: Design infrastructure solutions (30–35%) asks you to recommend an automated deployment solution for applications. IaC is not named in the objectiveInfrastructure
AI-300Design and implement an MLOps infrastructure (15–20%): deploy Machine Learning workspaces and resources with Bicep and the Azure CLI. Design and implement a GenAIOps infrastructure (20–25%): deploy infrastructure with Bicep templatesMLOps infrastructure, GenAIOps infrastructure
SC-500Manage identity, access, and governance (20–25%): implement and configure security controls by using infrastructure as codeIdentity, access and governance
200-901Infrastructure and Automation (20%): describe the principles of infrastructure as code (5.5) and the capabilities of Ansible, Terraform and Cisco NSO (5.6)Infrastructure and automation
CCNA 200-301v2.0 (from February 3, 2027), AI, and Network Operations and Management (10%): infrastructure as code as one of the network management approaches (5.3). v1.1 (until February 2, 2027) does not name IaC; it only touches it through recognizing configuration management tools such as Ansible and Terraform (6.6)AI and network operations (v2.0)
N10-009Networking Concepts (23%): objective 1.8 lists infrastructure as code with playbooks, templates, reusable tasks, configuration drift and compliance, and source controlN10-009 networking concepts explained
SY0-801Minor, and only from the V8 launch on November 17, 2026: Security Architecture (19%) lists IaC among architecture concepts (3.1), and Security Operations (27%) lists desired state management as an automation use case (4.6)Security architecture

How exam questions are built around it

Questions rarely ask for a definition. They describe a team problem and ask which approach fixes it.

  • “Environments drift”, “snowflake”, “must be reproducible” point to IaC kept in source control, edited in the source rather than in the target.
  • “Desired state”, “regardless of current state”, “run repeatedly” point to declarative and idempotent. A script with if checks can be idempotent too, but it is the author’s work; the declarative tool gives it by design.
  • “Preview the changes” points to what-if for Bicep and ARM, plan for Terraform.
  • “A resource disappeared after redeploying” points to complete mode, or to a property left out of the template.
  • “No state file to maintain” points to Bicep over Terraform. “Multiple clouds” points to Terraform. For the Azure-only choice see ARM vs Bicep vs Terraform.
  • State inside a machine (packages, services, files) points to configuration management such as Ansible or Azure Machine Configuration, not to a resource template.

The usual wrong answer is a script chosen for “automation”. A script automates, but only a declarative definition gives the repeatability and preview the stem asks for.

Practice questions

Question 1. A team deploys an ARM template to a resource group every night. Last night an engineer added a storage account to the group in the portal. After the nightly deployment the storage account was gone, and the template has not changed. What most likely caused this?

  • A. The deployment ran in incremental mode, which removes resources missing from the template
  • B. The deployment ran in complete mode, which deletes resources missing from the template
  • C. The template is not idempotent
  • D. The what-if operation applied its predicted changes
Show answer

Answer: B

Complete mode deletes resources in the resource group that are not in the template, so the portal-created account was removed. Incremental mode, the default, leaves such resources alone. Idempotence means a repeated deployment gives the same result for the resources in the template; it does not delete extras in incremental mode. What-if only predicts changes and never makes any.

Question 2. A team must provision the same Azure environment for dev, test and production from one source of truth. Rerunning the deployment must leave an already correct environment unchanged, and engineers want a preview of changes before every run. Which approach meets the requirements?

  • A. A Bash script that runs az commands in order
  • B. Manual changes in the portal, documented in a wiki
  • C. A Bicep file deployed with what-if before each run
  • D. A runbook that deletes and recreates the resource group on every run
Show answer

Answer: C

A declarative Bicep file describes the desired state, is idempotent, and supports the what-if preview before deployment. A Bash script of az commands is imperative: safe repeat runs depend on how each command behaves plus hand-written guards, and there is no built-in preview. Portal changes are manual and cause drift. A runbook that recreates the group each time is destructive and does not preview anything.

Go deeper