Imagine you need to set up a test environment for your team: a few virtual servers, a network, a database and some storage. You could do it by hand in your cloud provider's web console, but a week later, when you need to rebuild the same environment for production, it's hard to remember which buttons you clicked and where. Mistakes creep in, environments drift apart, and the "it worked on my machine" problem begins. That's exactly why Terraform and the Infrastructure as Code approach have become everyday tools for DevOps engineers. In this article, we explain in plain language what Terraform is, how it works, which concepts you need to know and how to write your first configuration.
What is Infrastructure as Code?
Infrastructure as Code (IaC) means describing and managing infrastructure — servers, networks, load balancers, DNS records and other resources — through code files rather than by hand. These files are stored in Git, reviewed (code review) and versioned just like regular application code.
There are two main approaches to IaC:
- The imperative approach — you write step by step "how" to do something: first create a server, then attach a disk to it, then add a firewall rule.
- The declarative approach — you describe "what" you want: "I need two servers, one network and these rules." The tool compares the current state with the desired state and works out the necessary steps on its own.
Terraform is a declarative tool. You describe the end result, and Terraform calculates what needs to be created, changed or deleted to get there.
What is Terraform and what is it used for?
Terraform is an infrastructure-as-code tool created by HashiCorp. Configurations are written in a readable language called HCL (HashiCorp Configuration Language). Terraform's main strength is that it isn't tied to a single platform: through plugins called providers, it can work with AWS, Microsoft Azure, Google Cloud, Kubernetes, GitHub and hundreds of other services.
Terraform is typically used for:
- creating and updating cloud infrastructure (virtual machines, networks, databases);
- building consistent test, staging and production environments from the same codebase;
- tracking infrastructure changes in Git and reviewing them within the team;
- automatically validating and deploying infrastructure in CI/CD pipelines;
- tearing down unneeded environments completely with a single command, keeping costs under control.
Note that in 2023 HashiCorp switched Terraform's license to the Business Source License (BSL). In response, the community created a project called OpenTofu based on the last open-source version of Terraform. For everyday users, the syntax and core concepts are almost identical in both tools, so what you learn in Terraform will also come in handy with OpenTofu.
Key concepts: provider, resource, state
When you start learning Terraform, you'll come across a few terms again and again. Understanding them up front makes the later stages much easier.
Provider
A provider is a plugin that lets Terraform "talk" to a specific platform's API. For example, the hashicorp/aws provider works with AWS, while hashicorp/azurerm works with Azure. Providers are downloaded automatically from the Terraform Registry.
Resource
A resource is a specific object you want to manage: a virtual server, an S3 bucket, a DNS record, a user and so on. Each resource is described in a resource block and consists of a type and a name, for example aws_instance.web.
Data source
A data source lets you retrieve information about existing resources that Terraform didn't create. For example, it's used to read the ID of the latest operating system image or the parameters of a network created by another team.
Variable and output
Input variables (variable) let you run the same code with different parameters: for example, a small server in the test environment and a more powerful one in production. Outputs (output) display the information you need from created resources — an IP address, a URL and so on — or pass it to other modules.
State
State is Terraform's "memory." Terraform keeps track of which resources it has created and their current parameters in the terraform.tfstate file. The next time you run the code, it compares the code, the state and the real infrastructure to determine what needs to change. The state file can also contain sensitive data, so it's not recommended to commit it to Git.
Module
A module is a Terraform configuration grouped for reuse. If you use the same network or server setup in several projects, turning it into a module helps you write clean code that follows the DRY (Don't Repeat Yourself) principle.
The Terraform workflow: init, plan, apply, destroy
Day-to-day work with Terraform revolves around a few core commands:
terraform init— prepares the project directory and downloads the required providers and modules. Run it in a new project or when a provider changes.terraform fmtandterraform validate— format the code to the standard style and check its syntax and internal consistency.terraform plan— shows what will be created, changed or deleted without making any real changes. This is the most important safety step: you see the result before applying the change.terraform apply— executes the plan. It usually asks for confirmation and applies the changes only after you type "yes".terraform destroy— deletes all resources created by this configuration. It's useful for cleaning up test environments but should be used with great care in production.
A practical example: your first Terraform configuration
You don't need a cloud account to try Terraform. The hashicorp/local provider lets you create files on your computer, which makes it a convenient and safe way to learn the workflow. Create a new directory and add a main.tf file to it:
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.5"
}
}
}
variable "env" {
type = string
default = "test"
}
resource "local_file" "config" {
filename = "${path.module}/${var.env}.txt"
content = "This file was created with Terraform for the ${var.env} environment."
}
output "file_path" {
value = local_file.config.filename
}
Then run these commands in the terminal, one after another:
terraform init
terraform plan
terraform apply
At the plan stage, Terraform will show that one resource is going to be created. After apply, a test.txt file will appear in the directory, and the output will display its path. Now change the variable and run the same code for a different environment:
terraform apply -var="env=prod"
This time Terraform will show that the old file will be deleted and a new one created, because the file name has changed. This simple example illustrates the essence of the declarative approach: you only describe the state you want, and Terraform works out the difference itself. In a real project, instead of local_file you would have resources such as aws_instance or azurerm_virtual_network, but the workflow stays the same.
Terraform in a team: remote backends and state locking
When you work alone, keeping the state file on your local machine isn't a problem. But when several engineers work on the same infrastructure, local state creates serious risks: someone might make changes with an outdated state, two people might run apply at the same time, or the file might get lost.
To solve these problems:
- a remote backend is used — the state is stored in a central location such as AWS S3, Azure Blob Storage or HCP Terraform;
- state locking is enabled — while one person is making changes, the state is locked and no one else can run
applyat the same time; - changes are made through pull requests, and the CI/CD pipeline automatically generates a
terraform planfor each PR so the team can review the change before it's applied.
Terraform vs. Ansible: what's the difference?
Beginners often ask: if we have Terraform, why do we need Ansible? These tools don't replace each other; in fact, they're often used together.
- Terraform is mainly for provisioning and managing infrastructure: servers, networks, cloud services.
- Ansible is mainly for configuration management: installing packages, configuring services and updating files on existing servers. It's agentless and connects to servers over SSH.
In a typical scenario, Terraform creates the servers, Ansible sets up the environment the application needs on those servers, and a tool like HashiCorp Vault stores passwords and keys securely.
What do you need to know to learn Terraform?
You don't have to be a professional programmer to get started with Terraform, but some basic knowledge makes the process much easier:
- the ability to work with the Linux command line;
- networking basics: IP addresses, subnets, ports, DNS;
- a basic understanding of at least one cloud provider (AWS, Azure or GCP);
- working with Git: commits, branches, pull requests;
- reading configuration formats such as YAML and JSON.
Here's a recommended learning path: start with HCL syntax and the core commands, then move on to variables, outputs and data sources, followed by state management and remote backends, then modules and meta-arguments such as count and for_each, and finally integrating Terraform into a CI/CD pipeline. Reinforcing each stage with a small hands-on project is far more effective than just reading theory.
Common beginner mistakes
- Committing the state file to Git. This file can contain sensitive data; don't forget to add
*.tfstateto.gitignore. - Running
applywithout reading theplanoutput. Pay special attention to the words "destroy" and "replace". - Hardcoding passwords and keys. Use environment variables,
sensitivevariables or a secrets manager instead. - Not pinning the provider version. If
versionisn't specified, future updates can cause unexpected changes. - Changing resources both with Terraform and by hand. This creates a mismatch (drift) between the code and the actual state.
Conclusion
Terraform turns infrastructure into code that is repeatable, reviewable and versioned. This reduces errors and lets teams manage environments faster and more transparently. As cloud technologies and DevOps practices spread, Infrastructure as Code skills are becoming increasingly important for system administrators, DevOps engineers and cloud engineers.
If you'd like to learn Terraform with mentor support on real projects — together with HCL, state, modules, Ansible, Vault and CI/CD integration — take a look at Orient Academy's Terraform course.