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
| Component | Description | Representative Tools |
|---|---|---|
| Configuration File | Code file defining the desired infrastructure state | Terraform (.tf), Ansible (.yml), CloudFormation |
| State File | Records and synchronizes the actual current state of infrastructure | terraform.tfstate, Pulumi state |
| Plan | Compares code against current state to preview changes | terraform plan, ansible --check |
| Apply | Applies the planned changes to actual infrastructure | terraform 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 Item | Declarative | Imperative |
|---|---|---|
| Core question | “What” should be built? | “How” should it be built? |
| State management | Automatically converges to the desired state | Each step is re-executed sequentially |
| Idempotency | Re-running the same code guarantees the same result | Re-running may cause side effects |
| Readability | Easy to grasp the final infrastructure structure | Focused on execution flow and order |
| Representative tools | Terraform, Pulumi, Kubernetes YAML | Ansible, Chef, Puppet, shell scripts |
3. Expected Benefits and Practical Application of IaC
| Category | Expected Benefits | Application and Practical Use |
|---|---|---|
| Consistency and accuracy | Eliminates drift between environments and prevents human error | The same code guarantees identical dev, staging, and production environments |
| Operational speed | Automated provisioning shortens deployment time | Modular templates enable servers and networks to be scaled up within minutes |
| Version control | Tracks infrastructure change history and enables rapid rollback | GitOps pipelines review infrastructure changes through pull requests |
| Cost optimization | Code-based detection and cleanup of unnecessary resources | Terraform drift detection automatically identifies unused resources |