DevOps · Operations

Infrastructure as Code

Infrastructure as code describes servers, databases and networks in files — so environments are repeatable, reviewable and versioned like application code.

Intermediate 5 min Lesson 21 of 30
Before you start:CI/CD Fundamentals
0/30 in DevOps
On this page · 5 min · Intermediate

Learning objectives#

  • Explain IaC and its benefits
  • Compare declarative tools
  • Understand plan, apply, state and drift
  • Review infrastructure changes safely

Explanation#

Declarative IaC describes the end state (“a storage account with these settings”); the tool works out the steps.

Popular tools: Terraform/OpenTofu (multi-cloud), Bicep (Azure), CloudFormation/CDK (AWS), Pulumi (general-purpose languages).

A plan previews changes; apply makes them. State records what the tool manages. Drift is when someone changes resources by hand.

Infrastructure changes go through pull requests with the plan output attached for review.

Code example: The IaC workflow#

TextThe IaC workflow Illustrative
1. Edit main.tf / main.bicep in a branch
2. Open PR → CI runs: fmt check · validate · plan
3. Reviewer reads the plan: "+ create 1 storage account, ~ update 1 app setting"
4. Merge → CD runs apply with an approved plan
5. Nightly job runs plan again → any difference = drift alert
Expected output
Every infrastructure change is reviewed, repeatable and recorded in Git history.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. Infrastructure is edited like code, in a branch.
  2. CI shows exactly what will change.
  3. Apply runs only after review.
  4. Scheduled plans detect manual drift.

Real-world use case#

Recreating an identical staging environment in minutes, or rebuilding production in another region after a disaster.

Common mistakes#

Mixing manual portal changes with IaC

Manual changes are overwritten or cause drift. Change the code instead.

Applying without reading the plan

A rename can destroy and recreate a database. Read every plan.

Best practices#

  • All infra in Git
  • Plan in PRs, apply after approval
  • Remote, locked state
  • Small modules reused across environments

Remember this#

Declare the end state; the tool does the steps.

plan → review → apply.

Drift = reality ≠ code.

Practice exercise#

Your task: Name the native IaC language for Azure and for AWS.

Show solution
Text Illustrative
Azure: Bicep (ARM templates). AWS: CloudFormation (or the CDK).

Mini quiz#

Infrastructure as Code quizMultiple choice Question 1 of 1

What is configuration drift?

Score so far: 0/0

Want more? Practise all DevOps MCQs · DevOps interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.