Skip to content
Latchkey

What Is Terraform? Infrastructure as Code Explained

Terraform is an infrastructure-as-code tool that provisions and manages cloud resources from declarative configuration files, tracking what exists in a state file.

Terraform lets you describe your infrastructure (servers, databases, networks) in code and have it created and updated automatically. Instead of clicking through cloud consoles, you write configuration, review a plan of changes, and apply it, with the result tracked so future changes are incremental.

What Terraform is

Terraform is an open-source infrastructure-as-code tool from HashiCorp. You declare the desired state of your infrastructure in HCL configuration, and Terraform uses provider plugins (AWS, GCP, Azure, and hundreds more) to create, update, and delete resources to match. It records the real-world state in a state file.

Plan and apply

Terraform compares your configuration to the recorded state and the actual cloud, then "terraform plan" shows exactly what it would add, change, or destroy. "terraform apply" executes that plan. This plan-then-apply flow makes changes predictable and reviewable, and the state file lets Terraform manage resources incrementally.

A config example

A short config declares a resource.

A minimal Terraform resource
resource "aws_s3_bucket" "logs" {
  bucket = "my-app-logs"
  tags   = { Environment = "prod" }
}

Role in CI/CD

In CI/CD, Terraform changes are typically gated: a pipeline runs "terraform validate" and "terraform plan" on a pull request so reviewers see the diff, then "terraform apply" runs on merge. Remote state and state locking prevent two runs from clashing. Running plan and apply in CI gives an audit trail and keeps infrastructure changes consistent and reviewable.

Alternatives

Pulumi lets you write infrastructure in general-purpose languages. OpenTofu is a community fork of Terraform. Cloud-native options like AWS CloudFormation and CDK target a single provider. Terraform remains the most widely used multi-cloud IaC tool.

Key takeaways

  • Terraform provisions infrastructure from declarative HCL configuration.
  • plan shows the diff; apply executes it; state tracks what exists.
  • In CI, plan on PRs and apply on merge, with remote state and locking.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →