Terraform is an infrastructure as code tool. Instead of creating servers by clicking in a dashboard, you describe what you need in text files, and Terraform creates everything for you.

How it works

You write the configuration in a declarative language. In it, you say what you want to have in the end, not the steps to get there. Terraform compares what you described with what actually exists and calculates what needs to change.

The typical workflow uses three commands:

Terminal
terraform init
terraform plan
terraform apply

Init prepares the project and downloads the providers. Plan shows what will be created, changed or removed, without executing anything. Apply executes the change.

Terraform keeps a record of what it created, called state. It is through that record that it knows what already exists on the next run.

Practical example

Imagine you need a server for your application. Instead of creating it manually, you write a file.

hcl · main.tf
resource "aws_instance" "app" {
  ami           = "ID_DA_IMAGEM"
  instance_type = "t3.micro"
}

If tomorrow you need an identical environment for testing, just run the same configuration again. If something breaks, you can recreate everything from the file.

Since the file is text, it can live in Git, go through review in a pull request and have a change history.

What it is for

Terraform is used to:

  • Create identical development, test and production environments.
  • Avoid manual configuration errors.
  • Work with multiple cloud providers, like AWS, Google Cloud and Azure, with the same approach.
  • Document the infrastructure in the code itself.

For those who use AI agents to program, Terraform is useful because the agent can propose changes in files, and you review the plan before applying.

Cautions

The apply command changes real resources and can generate cost. Always read the plan result before applying. Keep the state file in a safe location, as it may contain sensitive information, and never put access keys directly in the code.

Difference between Terraform and Kubernetes

Terraform creates the infrastructure, like machines, networks and databases. Kubernetes orchestrates containers that run on top of that infrastructure. It is common to use both together: Terraform builds the ground, and Kubernetes organizes the applications.