Kahibaro
Discord Login Register

6.4 Infrastructure as Code

Why Infrastructure as Code Matters

Infrastructure as Code (IaC) means defining and managing your servers, networks, and services using machine-readable files instead of manual clicks in a web console.

At a high level, IaC gives you:

IaC is a foundational DevOps practice; many CI/CD pipelines assume that infrastructure is provisioned and updated automatically using IaC tools.

Declarative vs Imperative IaC

IaC tools usually follow one of two models:

    resource "aws_instance" "web" {
      ami           = "ami-123456"
      instance_type = "t3.micro"
    }
    - name: Create subnet
      ...
    - name: Create instance in subnet
      ...

Many tools blend both styles, but:

For infrastructure provisioning (VMs, networks, load balancers, managed services), the declarative model is more common and easier to reason about at scale.

Core Concepts in Infrastructure as Code

Regardless of tool, you’ll see similar concepts:

Resources

A resource is a unit of infrastructure defined in code, such as:

Each resource has:

Providers and Modules (High-Level Idea)

You’ll see these terms frequently:

This course has a separate chapter that dives into “Providers and resources” and “Modules”; here you only need to recognize that IaC is organized into building blocks and reusable components.

State

Many IaC tools keep track of state:

Losing or corrupting state can be dangerous (the tool might try to recreate or destroy the wrong resources), so:

You’ll see concrete Terraform examples of this in the dedicated chapter.

Idempotency and Drift

Two important properties of well-designed IaC:

Idempotency

An IaC run should be idempotent:

This prevents “configuration snowballs” and makes your automation safe to run repeatedly (e.g., as part of CI).

Configuration Drift

Drift happens when the real infrastructure no longer matches the IaC definitions, typically because:

Drift causes:

Controlling drift:

GitOps and IaC

IaC integrates naturally with GitOps practices:

Benefits:

IaC vs Configuration Management

IaC usually refers to defining cloud/platform resources, but there is an overlap with configuration management tools that manage OS-level configuration.

Typical division of responsibilities:

Hybrid patterns:

Environments and Reuse

A common requirement is managing multiple environments (dev, staging, prod) with IaC while avoiding copy-paste.

Strategies include:

These concepts generalize across IaC tools even though the details differ (naming, file layout, CLI commands).

Testing and Validating IaC

Treat IaC like application code:

Testing helps prevent misconfigurations that could cause outages, security gaps, or unexpected costs.

Cost and Security Considerations

Because IaC can create a lot of infrastructure quickly, you must consider:

Cost Control

Security

Typical IaC Workflow

A minimal, tool-agnostic workflow looks like:

  1. Edit infrastructure definitions in files (.tf, .yaml, etc.).
  2. Format and validate:
    • Run the tool’s fmt and validate or equivalent.
  3. Plan / diff:
    • Preview changes: see what will be created, updated, or destroyed.
  4. Review:
    • Use a pull/merge request.
    • Teammates review the code and the plan.
  5. Apply:
    • CI/CD (or a controlled environment) applies the changes.
  6. Monitor:
    • Verify that the infrastructure behaves as expected.
    • Watch logs, metrics, and drift detection.

This workflow is the foundation you’ll refine as you move into specific tools and more advanced patterns.

Where to Go Next in This Course

In the following chapters, you’ll see this theory put into practice:

Understanding the general principles in this chapter will make those tool-specific topics easier to follow and apply.

Views: 77

Comments

Please login to add a comment.

Don't have an account? Register now!