What Is Infrastructure as Code (IaC)? Manage Infrastructure Changes with Code
Learn how IaC reconciles versioned definitions with real infrastructure, and how plans, state, drift, and review help control changes.
What Is Infrastructure as Code (IaC)? Manage Infrastructure Changes with Code
Infrastructure as Code (IaC) defines the desired setup of servers, networks, storage, and other infrastructure in files, then uses tools to compare that definition with real resources and create or change them. Versioning and reviewing those files makes changes repeatable and traceable. It does not guarantee identical results every time: provider behavior, inputs, current state, and permissions also matter.
What problem does IaC solve?
Manually building development, test, and production environments through a console depends on people remembering the same steps. Over time, settings can diverge, and it becomes hard to tell who changed what. Microsoft describes these differences as configuration drift. IaC records the intended environment so teams can reproduce it and review changes to its source.
A declarative definition says what should exist, such as two web servers, a private subnet, and a firewall that permits only selected ports. The tool determines the operations needed to reach that state. An imperative script instead spells out commands and their order. These approaches can be combined, and IaC tools do not all use the same syntax.
How does a definition change real infrastructure?
Terraform illustrates a write, plan, apply loop. First, define resources; then initialize the working directory and providers. A plan compares configuration, saved state, and managed infrastructure, and previews additions, changes, or removals. Planning alone does not change infrastructure. Apply executes a plan through provider APIs and normally asks for approval first.
If a plan shows an unexpected deletion or replacement, stop and investigate before applying. A saved plan can be passed to apply, but later changes to infrastructure or state can make a previous preview stale. Automatic approval skips a review step and needs controls on who can run it and what changes it may apply.
AWS CloudFormation also defines infrastructure in templates and manages it as stacks. AWS recommends reviewing change sets before updates, keeping templates under version control, and never embedding credentials in templates. Each tool has its own resource model, state handling, preview, and rollback behavior.
Why do state and drift matter?
Terraform state links resource declarations to the identities of real remote objects. Terraform uses that mapping to calculate later changes. Missing or stale state can lead to incorrect proposals or duplicate management of an existing resource.
For teams, choose where to store state. HashiCorp recommends a remote backend with secure access controls and state locking for collaboration; state may contain secrets, so do not treat it like an ordinary source file or commit it to an unprotected repository. Do not edit state directly; use supported state commands.
Drift occurs when someone changes a managed resource outside the code workflow. A later plan may detect the difference, but the team must decide whether to update the code to match the approved change or restore the intended configuration. Do not blindly overwrite a manual change; first establish its reason and impact, then update the source and review a fresh plan.
Example: keeping test and production environments aligned
Hypothetical example: A team defines shared web servers, a load balancer, and a database, while supplying environment-specific size, domain, and secret values separately. Reusable definitions make common settings easier to review than two copied configurations. Production may still require different capacity, data, permissions, and availability settings. Secrets should not be placed in code or public logs. Before deployment, inspect the plan for deletion, replacement, public exposure, or expanded permissions. This is an illustration, not a deployment we performed.
IaC manages infrastructure resources such as networks, virtual machines, databases, and permissions. Configuration management may focus on packages and services inside a server. A container image packages an application and its runtime, but does not by itself create the network or cluster that runs it. Decide which layer each tool and owner is responsible for.
A safe way to start
Begin with a small, reversible part of a development environment.
Determine whether existing resources should be created anew or imported into management; declaring existing resources without importing them can create duplicates or unintended changes.
Version configuration, but provide credentials through an approved secret store or protected inputs. Restrict access to state and plan artifacts too.
Review every planned addition, update, and removal, especially data deletion, replacement, public exposure, and permission expansion.
Apply only after the code and final plan are reviewed. If infrastructure changed after planning, generate a fresh plan.
After applying, check the tool result, actual resources, service behavior, and saved state. A failure can leave partial changes, so do not assume automatic rollback; inspect current state and plan recovery.
IaC is useful because it records change intent, exposes proposed effects before execution, and gives teams a way to manage differences between code and live infrastructure. Decide how state, secrets, and approvals will be handled, then start small.
Sources
[What is infrastructure as code? (Microsoft Learn)](https://learn.microsoft.com/en-us/devops/deliver/what-is-infrastructure-as-code)
[Terraform workflow for provisioning infrastructure (HashiCorp)](https://developer.hashicorp.com/terraform/cli/run)
[Terraform state (HashiCorp)](https://developer.hashicorp.com/terraform/language/state)
[Core Terraform workflow (HashiCorp)](https://developer.hashicorp.com/terraform/intro/core-workflow)
[CloudFormation best practices (AWS)](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/best-practices.html)