Skip to content
DevOpsbeginner

What is Infrastructure as Code?

Updated Nov 30, 20263 min readBased on official documentation
What is Infrastructure as Code?

In short

Infrastructure as Code (IaC) is a way to create and manage servers, networks and other infrastructure by writing configuration files instead of clicking through a web console. The files can be stored, reviewed and reused like application code. Terraform is a popular IaC tool.

What IaC means

Infrastructure is everything your software runs on: virtual machines, databases, private networks, storage buckets and DNS records. Traditionally, many teams created these pieces by hand, using a web dashboard or a series of terminal commands. That works for a small project, but it becomes hard to repeat, hard to audit and easy to break as the system grows.

Infrastructure as Code, usually shortened to IaC, takes a different approach. You describe the infrastructure you want in plain text files. A tool reads those files and creates, changes or removes real resources to match them. Because the description lives in a file, you can store it in version control, review changes in a pull request, copy it to another environment and rebuild the whole setup from scratch.

The main goals are safety, consistency and repeatability. If two environments, such as a test system and a production system, are built from the same files, they are far more likely to behave the same way. If something goes wrong, you can rebuild from a known good description instead of trying to remember every manual step.

How Terraform fits in

Terraform is an IaC tool made by HashiCorp. You write its configuration in a human-readable language, define resources such as a virtual machine or a network, and Terraform manages their full lifecycle: creating, updating and deleting them.

Terraform works with many platforms through plugins called providers. A provider knows how to talk to a service through its application programming interface (API), which is the set of rules a program uses to request actions from another program. Providers exist for major clouds such as AWS, Azure and Google Cloud, and for services such as Kubernetes and GitHub. You can compose resources from different providers into reusable configurations called modules, so a common setup can be shared across projects.

Terraform's language is declarative. You describe the end state you want, for example "one server with this size and this network," and not the exact steps to reach it. Terraform works out the dependencies between resources and applies them in a safe order, so a network is created before the servers that use it.

The Terraform workflow

A typical Terraform project follows five steps:

  • Scope: decide which infrastructure the project needs.
  • Author: write the configuration files.
  • Initialize: install the providers the configuration uses.
  • Plan: preview the changes Terraform would make.
  • Apply: carry out the planned changes.

The plan step is especially useful. It shows what will be created, changed or destroyed before anything happens, so you can catch mistakes early.

How state keeps track

Terraform stores what it has built in a state file. The state acts as a record of the real infrastructure. When you run a new plan, Terraform compares your configuration with the state and with the real resources, then proposes only the changes needed to match your files.

The state file is sensitive, because it can contain details about your resources. Teams often keep it in a remote backend instead of on a single laptop. A remote backend stores the state in a shared location, so teammates can work from the same record and avoid conflicting changes when several people edit the infrastructure at once.

Common mistakes

Beginners often make a few predictable errors. Editing resources by hand in the cloud console and then running Terraform causes the state and the real world to drift apart, and the next plan may try to undo the manual change. Committing secrets such as passwords into configuration files is another risk; keep them in a secure store and pass them in as variables. Finally, skipping the plan step and applying directly can lead to surprises, so read the plan output before you apply it.

Alternatives and next steps

Terraform is not the only option. Some teams use cloud-native templates, such as the templating formats offered by individual cloud providers, while others use general programming languages to describe infrastructure. The right choice depends on your platforms and your team's skills.

If you run containers, you may also meet Helm, which packages Kubernetes applications, or Dockerfiles, which describe how to build a container image. Monitoring the infrastructure you create is another topic; see our page on observability for an overview. Once you understand the core ideas, the next step is to install Terraform and deploy a small test resource on your own machine.

FAQ

Is Infrastructure as Code only for large companies?

No. Small projects benefit too, because a configuration file makes the setup easy to repeat and document. Even a single server is easier to rebuild when its description is stored in version control.

Do I need to know a programming language to use Terraform?

Not really. Terraform uses its own human-readable configuration language, which is designed to be declarative. You describe the result you want rather than writing step-by-step code.

What happens if someone changes infrastructure by hand?

The state file and the real resources can drift apart. Terraform's next plan will show the difference and propose changes to bring the infrastructure back in line with your configuration.

Can Terraform work with more than one cloud?

Yes. Terraform uses providers for each platform, so one set of tools and one workflow can manage resources across several clouds and services.

Sources

This page is based on the official documentation below. Read it for full detail.

Infrastructure as Code — official documentation ↗

Infrastructure as Code in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.