Skip to content

Infrastructure as Code (IaC)

Infrastructure as Code (IaC)

Managing Infrastructure Through Code

1. Overview of IaC: A System That Achieves Automation, Consistency, and Version Control by Defining Infrastructure Configuration as Code

    flowchart LR
    A["Manual infrastructure setup<br/>(human error, inconsistency)"] --"Codify and automate,<br/>version control"--> B["IaC pipeline<br/>Plan → Apply"] --"Consistency, reproducibility,<br/>eliminate drift"--> C["Reliable<br/>infrastructure operations"]

    style A fill:#FFEBEE,stroke:#D32F2F,color:#000
    style B fill:#E3F2FD,stroke:#1976D2,color:#000
    style C fill:#E8F5E9,stroke:#388E3C,color:#000
  

Definition: An operational approach in which infrastructure configuration — servers, networking, storage, and more — is specified as machine-readable code (definition files) instead of manual work, and infrastructure is provisioned, changed, and decommissioned through version control and automation tools.

Characteristics: (Declarative or imperative) Infrastructure state is defined using either a declarative or an imperative approach. (Environment consistency) The same code reproduces development, staging, and production environments, eliminating drift and ensuring environment consistency. (Change history management) Integration with Git-based version control enables tracking of infrastructure changes, code review, and rollback.


2. How IaC Works and Its Core Tooling

a. IaC Workflow (Provisioning Pipeline)

    flowchart LR
    CODE["Write infrastructure code<br/>(HCL, YAML, JSON)"]
    GIT["Version control<br/>(Git Push)"]
    PLAN["Review the change plan<br/>(Plan)"]
    APPLY["Apply infrastructure<br/>(Apply)"]
    INFRA["Cloud / On-premises<br/>Infrastructure"]

    CODE --> GIT --> PLAN --> APPLY --> INFRA
    INFRA --"State feedback"--> PLAN

    style CODE  fill:#E3F2FD,stroke:#1976D2,color:#000
    style PLAN  fill:#FFF3E0,stroke:#F57C00,color:#000
    style APPLY fill:#E8F5E9,stroke:#388E3C,color:#000
    style INFRA fill:#1E3A5F,stroke:#1E3A5F,color:#fff
  
ComponentDescriptionRepresentative Tools
Configuration FileCode file defining the desired infrastructure stateTerraform (.tf), Ansible (.yml), CloudFormation
State FileRecords and synchronizes the actual current state of infrastructureterraform.tfstate, Pulumi state
PlanCompares code against current state to preview changesterraform plan, ansible --check
ApplyApplies the planned changes to actual infrastructureterraform apply, ansible-playbook

b. Declarative vs. Imperative Approach Compared

    flowchart TD
    subgraph R1[" "]
        direction LR
        DEC["Declarative approach<br/>Define the desired end state<br/>The tool decides how<br/>Terraform / Kubernetes"]
        IMP["Imperative approach<br/>Define the execution steps<br/>The user controls the process<br/>Ansible / Chef"]
    end
    subgraph R2[" "]
        direction LR
        ADV["Declarative advantages<br/>Idempotency guaranteed<br/>Automatic drift convergence<br/>Safe to reapply"]
        DIS["Imperative advantages<br/>Fine-grained execution control<br/>Complex sequencing logic<br/>Expressing conditional branches"]
    end

    style DEC fill:#E3F2FD,stroke:#1976D2,color:#000
    style IMP fill:#F3E5F5,stroke:#7B1FA2,color:#000
    style ADV fill:#E8F5E9,stroke:#388E3C,color:#000
    style DIS fill:#FFF3E0,stroke:#F57C00,color:#000
    style R1 fill:none,stroke:none
    style R2 fill:none,stroke:none
  
Comparison ItemDeclarativeImperative
Core question“What” should be built?“How” should it be built?
State managementAutomatically converges to the desired stateEach step is re-executed sequentially
IdempotencyRe-running the same code guarantees the same resultRe-running may cause side effects
ReadabilityEasy to grasp the final infrastructure structureFocused on execution flow and order
Representative toolsTerraform, Pulumi, Kubernetes YAMLAnsible, Chef, Puppet, shell scripts

3. Expected Benefits and Practical Application of IaC

CategoryExpected BenefitsApplication and Practical Use
Consistency and accuracyEliminates drift between environments and prevents human errorThe same code guarantees identical dev, staging, and production environments
Operational speedAutomated provisioning shortens deployment timeModular templates enable servers and networks to be scaled up within minutes
Version controlTracks infrastructure change history and enables rapid rollbackGitOps pipelines review infrastructure changes through pull requests
Cost optimizationCode-based detection and cleanup of unnecessary resourcesTerraform drift detection automatically identifies unused resources