INSIGHTS
DevOps & Automation

What Is Infrastructure as Code (IaC)? Tools, Examples, and Best Practices

In this article
  1. Declarative versus imperative changes
  2. A small no-cloud local Terraform example
  3. Review a plan before applying it
  4. State is sensitive operational data
  5. Testing and policy are part of IaC
  6. Modules and reuse require clear interfaces
  7. Choose IaC boundaries by real ownership

Infrastructure as code (IaC) describes infrastructure state and changes in version-controlled definitions rather than relying on undocumented console clicks. The value is not that every operation becomes perfectly deterministic; it is that teams can review intent, compare proposed changes and audit how an environment was configured. A useful first example distinguishes the desired-state file from the plan and the eventual mutation of real infrastructure.

Declarative versus imperative changes

A declarative system such as Terraform describes which resources should exist and lets the provider compare the desired state with managed objects. Imperative scripts issue commands in a chosen order. Both can be automated, but declarative configuration makes the intended end state easier to reason about; it does not eliminate dependencies, drift or provider failures.

A small no-cloud local Terraform example

Terraform can demonstrate the workflow without provisioning a public cloud account. The following local example writes a file for an isolated lab. It is still a side effect: apply creates or changes local state, so work in a disposable directory and do not point it at a sensitive filesystem.

terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.5"
    }
  }
}
resource "local_file" "message" {
  content  = "ready\n"
  filename = "${path.module}/ready.txt"
}
terraform init
terraform fmt -check
terraform validate
terraform plan -out=proposed.tfplan
# Review plan; then in the lab only:
terraform apply proposed.tfplan

Review a plan before applying it

The plan compares configuration with known state and provider observations, then proposes actions. terraform plan alone does not create cloud resources. Plans are contextual to state and time; rerun or regenerate a plan after major concurrent changes. A saved plan is a release artifact and may contain sensitive details, so protect it appropriately.

The Terraform core workflow describes write, plan and apply as distinct phases.

State is sensitive operational data

Terraform state maps managed objects to real infrastructure identifiers and can contain secrets or sensitive attributes. Use an appropriate encrypted remote backend with restricted access and locking where supported. Do not commit state files, credentials or plan artifacts into public repositories. Back up state and practice recovery before changing critical environments.

Testing and policy are part of IaC

Run formatting, validation, static policy checks and a planned change review in CI. Test modules in a disposable account or sandbox when the consequences are not obvious from a plan. Restrict who can approve a production apply and document how to revert a dangerous configuration.

Modules and reuse require clear interfaces

Extract a reusable module when multiple callers share a stable resource pattern. Define input types, outputs, provider constraints and ownership. Excess abstraction can hide which resources will be changed, so review expanded plans and avoid creating a module for a one-off configuration.

Choose IaC boundaries by real ownership

Terraform, configuration managers and Kubernetes controllers operate at different layers. Decide whether a database, VM image, OS package or Kubernetes workload belongs to each tool and avoid two independent controllers fighting over the same property. The best IaC practice is a reviewable and reversible operating procedure, not simply the existence of a configuration file.

Filed under DevOps & Automation