100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is Infrastructure as Code?

Learn what Infrastructure as Code is — declarative vs imperative, Terraform, idempotency and version control benefits — with DevOps interview answers.

mediumQ4 of 224 in DevOps Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Infrastructure as Code (IaC) is the practice of provisioning and managing infrastructure — servers, networks, databases, and more — through machine-readable definition files instead of manual configuration, so infrastructure is versioned, repeatable, and automated like application code.

With IaC you describe the desired infrastructure in code, commit it to version control, and a tool applies it to create matching resources. Declarative tools like Terraform let you state the end result and the tool figures out the steps, while imperative approaches spell out each step. Because the definition is code, you get repeatable environments, peer review through pull requests, an audit trail of changes, and the ability to rebuild identical environments on demand. Many tools are idempotent — applying the same configuration repeatedly converges to the same state without creating duplicates.

  • Repeatable, consistent environments
  • Version-controlled with full change history
  • Faster provisioning through automation
  • Reviewable and auditable infrastructure changes

AI Mentor Explanation

Infrastructure as Code is like having a written, versioned ground-preparation manual instead of a groundsman setting up the pitch from memory each time. The manual specifies the exact pitch, boundary, and net layout, so any venue can reproduce an identical setup on demand. Change is proposed by editing the manual and having it reviewed, giving a full history of who changed what. Re-reading and applying the same manual always yields the same ground — never a duplicate accidental pitch.

Step-by-Step Explanation

  1. Step 1

    Write the definition

    Describe the desired infrastructure — servers, networks, databases — in code.

  2. Step 2

    Version control it

    Commit the files to Git so changes are reviewed and history is tracked.

  3. Step 3

    Apply with a tool

    A tool like Terraform reads the code and creates or updates matching resources.

  4. Step 4

    Reconcile idempotently

    Re-applying converges to the declared state without creating duplicates.

What Interviewer Expects

  • Infrastructure defined in code, not configured manually
  • Version control, repeatability, and auditability benefits
  • Declarative vs imperative approaches
  • Idempotency and desired-state convergence

Common Mistakes

  • Describing IaC as just writing shell scripts by hand once
  • Ignoring version control as a core part of the practice
  • Confusing declarative and imperative approaches
  • Not mentioning idempotency or repeatable environments

Best Answer (HR Friendly)

Infrastructure as Code means writing down your servers and networks as code files instead of setting them up by hand. That code is versioned and reviewed like any other, so environments are consistent, changes are tracked, and you can rebuild the same setup reliably whenever you need it.

Code Example

Declarative infrastructure with Terraform (HCL)
# main.tf - declare the desired resource
resource "aws_instance" "web" {
  ami           = "ami-0abcd1234"
  instance_type = "t3.micro"
  tags = { Name = "web-server" }
}

# Preview and apply - idempotent, converges to desired state
# terraform plan
# terraform apply

Follow-up Questions

  • What is the difference between declarative and imperative IaC?
  • What does idempotency mean in the context of IaC?
  • How does IaC benefit from version control?
  • What are some common IaC tools?

MCQ Practice

1. What best describes Infrastructure as Code?

IaC manages infrastructure through machine-readable definition files that are versioned and applied automatically.

2. Which tool is a common declarative IaC tool?

Terraform is a widely used declarative IaC tool where you state the desired end result.

3. What does idempotency mean for an IaC tool?

Idempotent applies converge to the declared state without creating duplicates on repeated runs.

Flash Cards

What is Infrastructure as Code?Provisioning infrastructure through versioned, machine-readable code instead of manual setup.

Declarative vs imperative?Declarative states the end result; imperative spells out each step.

What is idempotency?Re-applying the same config converges to the same state without duplicates.

A common IaC tool?Terraform — declarative, versioned, provider-agnostic infrastructure provisioning.

1 / 4

Continue Learning