Skip to content

Infrastructure as Code (IaC) — Introduction

This document introduces the concept of Infrastructure as Code (IaC), its lifecycle, and related DevOps concepts like provisioning, deployment, orchestration, and configuration drift.


What Is Infrastructure as Code (IaC)?

Infrastructure as Code (IaC) is a method of managing and provisioning infrastructure through machine-readable definition files, rather than through manual processes. IaC can be implemented using two main approaches:

Declarative (What to Do)

  • Describes what the final state should be.
  • The system determines how to reach that state.
  • Common formats: JSON, YAML, XML.

Examples:

  • Azure: ARM Templates, Azure Blueprints
  • AWS: CloudFormation
  • GCP: Cloud Deployment Manager
  • Multi-Cloud (Cloud Agnostic): Terraform

Imperative (How to Do It)

  • Describes how to reach a desired state step by step.
  • Usually uses programming languages.

Examples:

  • AWS: Cloud Development Kit (CDK)
  • AWS, Azure, GCP, Kubernetes: Pulumi

Terraform in the IaC Ecosystem

Terraform is declarative, but supports imperative-like features, including:

  • Loops and conditional logic
  • Dynamic blocks
  • Local variables
  • Complex data structures

Terraform’s declarative model means you define the desired infrastructure state, and Terraform ensures reality matches it.


Infrastructure Lifecycle

A typical infrastructure lifecycle includes the following phases:

  1. Plan – Define requirements and design the system.
  2. Design – Architect and model the infrastructure.
  3. Build – Implement and configure infrastructure resources.
  4. Test – Validate functionality, performance, and security.
  5. Deliver – Deploy to production environments.
  6. Maintain – Operate and monitor ongoing infrastructure.
  7. Retire – Decommission resources when no longer needed.

Benefits of IaC

Aspect Description
Reliability Idempotent — repeated runs always result in the same state.
Manageability Easily modify and version changes with minimal effort.
Sensibility Reduces risks of financial, reputational, or operational losses.

Provisioning, Deployment & Orchestration

Provisioning

The process of preparing a server with systems, data, and software, making it ready for operation (provisioning = launching and configuring cloud resources).

Configuration Tools: Ansible, Puppet, Chef, Bash, PowerShell, Cloud-Init

Deployment

Delivering and running an application version on a provisioned server.

Tools: AWS CodePipeline, Jenkins, GitHub Actions, CircleCI, Harness

Orchestration

Coordinating multiple systems or services — especially common in microservices and Kubernetes environments.


Configuration Drift

Configuration Drift occurs when the provisioned infrastructure diverges from the expected configuration.

Causes

  • Manual changes
  • Malicious activity
  • Side effects from APIs, SDKs, or CLIs

Detection

  • Compliance Tools: AWS Config, Azure Policies
  • Built-in Detection: AWS CloudFormation Drift Detection
  • State Tracking: Terraform state files

Correction

  • Use compliance tools that can remediate misconfigurations (e.g., AWS Config).
  • Run terraform refresh and terraform plan to realign the actual and desired states.

Prevention

I. Immutable Infrastructure

  • Never modify servers post-deployment.
  • Use Blue-Green Deployments to replace infrastructure instead of modifying it.
  • Build images using AWS Image Builder or HashiCorp Packer.

II. GitOps

  • Version-control all infrastructure changes.
  • Use pull requests for peer review and change approval.

Mutable vs. Immutable Infrastructure

Type Description Example
Mutable Can be modified after deployment Virtual Machine
Immutable Never modified after deployment — replaced entirely AMI image, Docker container

Terraform encourages immutable infrastructure, ensuring:

  • 1-to-1 consistency with deployed resources
  • Enhanced security and reliability
  • Faster deployments (AMIs vs VMs + configuration tools)

GitOps Overview

GitOps integrates IaC with Git-based workflows:

  • Every infrastructure change is proposed, reviewed, and merged via Git.
  • Once approved, automation pipelines apply those changes to the cloud environment. This approach ensures traceability, auditing, and consistency across environments.