Lesson 4 of 5 Managing and Deploying Azure Resources

Understanding Infrastructure as Code

This lesson explains infrastructure as code, its benefits, the declarative and imperative approaches, and the IaC tools used with Azure.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe infrastructure as code and its benefits.
  • Distinguish declarative from imperative infrastructure as code.
  • Identify Azure's infrastructure-as-code options.

What infrastructure as code is

Infrastructure as code is the management of infrastructure, such as networks, virtual machines, load balancers and databases, in a descriptive model using code files rather than manual configuration. Just as the same application source code produces the same application, an IaC file produces the same environment every time it is deployed. IaC files are stored in source control, such as a Git repository, alongside application code.

Benefits of IaC

Managing infrastructure as code brings several benefits:

  • Consistency: every environment, such as development, test and production, is created from the same definition, which avoids configuration drift, the gradual differences that arise from manual changes.
  • Repeatability and speed: complete environments can be created or recreated in minutes, for example after a failure or for a temporary test.
  • Version control and review: changes to infrastructure are tracked, reviewed and approved like code changes, and can be rolled back.
  • Automation: deployments can run in continuous integration and continuous delivery (CI/CD) pipelines without manual steps.
  • Documentation: the code itself describes what the infrastructure is.
Match the pairs

Benefits of IaC

Exam skills 3.3.4

Match each benefit of infrastructure as code with what it means.

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

Declarative and imperative approaches

IaC tools follow one of two approaches. The following comparison shows the difference.

Declarative
You describeThe desired end state: what should exist
Order of operationsWorked out by the platform
Running againUsually idempotent: produces the same result
Azure examplesARM templates, Bicep, Terraform
Imperative
You describeThe steps to perform: how to create it
Order of operationsWritten by you
Running againMay fail or duplicate resources unless scripted carefully
Azure examplesAzure CLI and Azure PowerShell scripts

With a declarative template, if a resource already exists with the described configuration, deploying the template again leaves it unchanged; if a setting differs, the deployment updates it. This property, called idempotency, makes declarative IaC well suited to keeping environments consistent over time.

Sort into groups

Declarative or imperative?

Exam skills 3.3.4 · 3.3.5

Decide whether each statement describes declarative or imperative IaC.

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

IaC tools for Azure

Azure's native declarative IaC options are ARM templates, written in JSON, and Bicep, a simpler language that compiles to ARM templates; both are covered in the next lesson. Terraform, from HashiCorp, is a popular third-party declarative tool that works with Azure and other clouds. Scripts with the Azure CLI or Azure PowerShell provide an imperative option. Configuration inside VMs, such as installing software, can also be automated, but for AZ-900 the focus is on deploying Azure resources.

Worked example

Avoiding configuration drift

A company's test and production environments were built by hand and have gradually become different, causing bugs that appear only in production. The company wants every environment to be created from the same reviewed definition and to be able to recreate an environment quickly. What approach should it adopt?

Adopt infrastructure as code with a declarative template, such as Bicep or ARM templates, stored in source control. Every environment is deployed from the same file, so they stay consistent; changes are reviewed before deployment; and an environment can be recreated by redeploying the template. Documenting the manual steps in a runbook would still rely on people following them exactly, which is how the drift began.

Check yourself

What does idempotent mean for a declarative template?

Show answer

Deploying it again produces the same result instead of duplicating or failing on existing resources.

Exam Tips

Remember for the exam

  • IaC defines infrastructure in code files that are version-controlled and deployed repeatably.
  • Benefits: consistency (no configuration drift), speed, review and rollback, automation.
  • Declarative IaC describes the desired state and is idempotent; imperative IaC lists the steps.
  • Azure's native declarative options are ARM templates and Bicep.