Who set this server up, and how? Managing infrastructure as code
Infrastructure as code means your servers, network rules, DNS records and cloud resources are defined in files under version control and applied by a tool, instead of being clicked together in a web console. One question measures whether you actually have it: if your production environment were wiped tonight, could you rebuild it from the repository? An answer along the lines of "yes, if the person who built it walks us through the steps" describes a memory, not an infrastructure definition. And the moment that person takes two weeks off, nobody wants to touch anything.
The symptoms look the same at almost every company. Staging behaves differently from production because the two were built by different people at different times. A security group has a rule open to the world and nobody can say who added it or when. An instance from two years ago never gets upgraded because nobody is sure what runs on it. When a software vendor changes hands, the infrastructure is the part of the handover list that tends to be missing: the code arrives, the knowledge of how its environment was assembled does not. This is the infrastructure-side version of the one person who knows the system leaving.
Provisioning, configuration management, deployment
Three separate jobs get filed under one heading, and mixing them up causes real confusion. Provisioning creates the cloud resources themselves: instances, managed databases, load balancers, DNS records, security groups. Terraform and OpenTofu are the common tools; if you are committed to a single cloud there is AWS CloudFormation and Azure Bicep, and if you would rather write infrastructure in a general-purpose language, Pulumi and the AWS CDK.
Configuration management deals with what happens inside a machine: packages, services, files, users, scheduled jobs. Ansible is the usual choice, and since it works over SSH with no agent to install, the entry cost on a small estate is low. The third job is application deployment. Building the image and shipping the release belongs to your CI/CD pipeline, not to your infrastructure tool. Keeping all three in one repository is fine. Running them in one step is not.
The list of things that are not infrastructure as code is familiar too: a template exported from the console after everything was built by hand and never run again, a page of commands in the wiki, one engineer's shell history. The test is a single question. Does that file actually build the infrastructure, or does it describe what somebody once did?
Codify what hurts, not everything
On a greenfield project this is easy. The hard case is inheriting an estate that grew by hand over several years, and that is where the "let's migrate all of it over one weekend" plan usually stalls halfway. Half-finished IaC is worse than no IaC at all, because now there are two sources of truth and no rule about which one wins.
Order the work by impact. Things that change often and hurt immediately when they are wrong go first: DNS records, CDN and firewall rules, security groups, bucket access policies, environment variables. A VPN tunnel you touch once a year can wait at the bottom of the list.
You do not have to delete and recreate anything to get started. The import block introduced in Terraform 1.5 brings an existing resource under management, and the -generate-config-out option writes a draft configuration for it, which is far quicker to correct than to write from scratch. There is exactly one proof that you imported something correctly: the next plan comes back empty. Every "a few small differences are left, we'll look at them later" line turns into an unexpected change months down the road.
The state file, which nobody warns you about
Terraform and OpenTofu track what they manage in a state file. It is the bridge between what the code says and what exists in the cloud, and most of the accidents teams run into start there. Three rules cover it.
- Remote backend, with locking. State does not live on a laptop. Put it in S3, Azure Blob Storage or GCS and turn locking on, or two people running
applyat the same time will corrupt it. Terraform 1.11 made native S3 locking (use_lockfile = true) generally available, and per the upgrade guide the DynamoDB locking arguments are deprecated in its favour. - State contains your secrets. HashiCorp's own documentation is blunt about it: state is stored in plain text, including any secret values your configuration defines. Marking a value
sensitiveonly redacts it from CLI output, not from the file. Ephemeral values, available since Terraform 1.10, exist at runtime and are never written to state or plan files. On the OpenTofu side, release 1.7 added end-to-end state encryption, with the key supplied through an environment variable or a key manager such as AWS KMS, GCP KMS or OpenBao. AWS's prescriptive guidance reaches the same conclusion from the other direction: restrict access to the file, and rotate any secret that has landed in it. - Keep it out of the repository.
.tfstateand.tfstate.backupbelong in.gitignore. The rest of the discipline is in our piece on secrets in source code, and the state file is the place where those rules get skipped most often.
Drift: that console change at two in the morning
Infrastructure built from code still gets broken by hand. During an incident somebody resizes an instance from the console, adds a rule, opens a port. By morning the system is up and nobody goes back to update the code. Three months later an unrelated apply quietly reverts the change, and this time the outage comes from the fix.
That gap is called drift, and detecting it is not the hard part. You run a refresh-only plan on a schedule to compare live resources against state. HCP Terraform's health assessments do exactly this, CloudFormation has built-in drift detection, and a scheduled job in your own pipeline works too.
The hard part is organisational. You need a written rule for what happens when drift is found. The version that works in practice: console changes during an incident are allowed, on two conditions. The change gets recorded somewhere (who, when, why) and it gets written back into code within a fixed window, say five working days. Teams that never write that rule down watch their IaC turn into decoration within six months. The record is also the only way to answer "who changed this" later, which is the same reasoning behind audit trail design applied to infrastructure.
Environments: modules instead of copy-paste
The most common reason staging behaves differently from production is that the two were built separately. Infrastructure as code does not fix that by itself. Copy the prod folder, paste it as staging, and you arrive at the same problem faster.
What works is putting shared structure in a module and letting environments differ only through variables: instance size, replica count, domain, log retention. Differences of the "exists in production, missing in staging" kind then show up in a diff, where somebody can question them. We covered why that gap gets expensive in staging and production parity.
Review and automated policy checks
The most underrated benefit here is that an infrastructure change becomes reviewable like any other code change. A pull request opens, the plan output lands in a comment, and a second person asks why a resource is being destroyed. A reviewer who sees destroy lines in a plan catches most accidents before they are applied.
Add automated checks on top. Scanners that read IaC files flag public buckets, unencrypted volumes and management ports open to the internet at merge time rather than at audit time. Development of tfsec has stopped and its rules now live in Trivy's trivy config scanner; Checkov remains a separate option. If you want rules of your own, OPA and Conftest let you express them directly: no resource in production without an owner tag, for instance.
Access matters as much as content. Run apply from the pipeline rather than from laptops, and give that pipeline a cloud role scoped to least privilege. We wrote about who owns cloud misconfiguration in the shared responsibility model; infrastructure as code is what makes that ownership traceable instead of theoretical.
Tools, licences and provider surprises
Picking a tool is no longer a purely technical decision. On 10 August 2023 HashiCorp moved Terraform from MPL 2.0 to the Business Source License 1.1: the source stayed readable, but the right to offer a competing product was carved out. OpenTofu forked from 1.5.7, the last MPL release, and is developed under the Linux Foundation. IBM completed its acquisition of HashiCorp on 27 February 2025.
For a company using Terraform to run its own infrastructure, the licence restriction changes nothing in daily work; it targets productising Terraform and selling it. The configuration language is still largely shared between the two tools, so you have a genuine exit option, and choosing with that in mind makes the open source licence compliance conversation easier later.
The surprises usually come from providers rather than licences. Cloudflare's Terraform provider was rewritten for version 5 in February 2025, with resources now generated from Cloudflare's OpenAPI schemas and breaking changes on several resource types. The tf-migrate CLI that automates much of the upgrade only arrived in April 2026. The lesson is plain enough: pin provider versions, commit .terraform.lock.hcl, and treat upgrades as scheduled work rather than something you discover mid-incident.
What if you only have three servers?
Treating infrastructure as code as a big-cloud concern is a mistake, and so is the opposite: building a layered module hierarchy for three servers in one region is usually wasted effort. Two questions set the right level. How often does this resource change, and how many hours would pass before you noticed it was misconfigured?
There is a lighter version that works well on small estates. Keep DNS, CDN and firewall rules as code, because those change often and fail visibly. Build the inside of each machine with a single Ansible playbook instead of by hand. Keep environment variables and secret references in a committed example file. Even that much makes a hosting migration predictable and makes the "why does this resource exist" line on your cloud bill answerable. The other question worth asking at this scale is in do you actually need Kubernetes.
What you can do this week
One: pick your most critical production system and write down, on one page, what you would rebuild and in what order if it vanished tonight. The gaps in that page are your starting list. Two: import DNS and CDN rules into the repository, with an empty plan as the finish line. Three: move state to a remote backend, enable locking, confirm encryption at rest, and check that no .tfstate file is committed. Four: add an IaC scanner to the pipeline and close what the first run reports. Five: schedule a weekly refresh-only plan and write down what happens when it finds drift. Six: take apply away from laptops and give it to a least-privilege pipeline role.
Need help with this topic?