Ankit KanojiyaIntermediate
Evergreen guide by Ankit Kanojiya · Intermediate · DevOps & Cloud
Infrastructure as Code with Terraform
How Terraform manages cloud infrastructure declaratively through providers, state, and plan-and-apply workflows. Covers modules, state safety, and common pitfalls.
- Terraform
- Infrastructure As Code
- AWS
- DevOps
- Cloud
Infrastructure as Code means defining servers, networks, and other resources in version-controlled files rather than clicking through a console. Terraform is a widely used tool for this: you declare the desired infrastructure, and it figures out the API calls needed to make reality match.
The Core Workflow
Terraform centers on three commands. Plan shows what will change, apply executes those changes, and destroy tears resources down. The plan step is the safety mechanism, because it reveals the exact set of creations, updates, and deletions before anything happens.
terraform init # download providers and set up the backend
terraform plan # preview changes
terraform apply # make reality match the configuration
Configuration is written in HCL. A minimal example provisions a storage bucket:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "assets" {
bucket = "my-app-assets-prod"
}
Pinning the provider version with a version constraint prevents an unexpected upgrade from changing behavior on the next init.
Providers
Providers are plugins that translate Terraform resources into API calls for a specific platform: a cloud, a SaaS product, or even a database. The same declarative workflow applies regardless of provider, which is part of Terraform's appeal across heterogeneous environments.
State: The Thing to Understand First
Terraform records what it manages in a state file that maps resources in the configuration to real-world objects. On each plan, it compares the configuration, the state, and the live infrastructure. Misunderstanding state causes most Terraform accidents.
Two rules matter above all:
- Never edit infrastructure out of band. Manual console changes create drift, where state and reality disagree. The next apply may try to revert or clobber those changes.
- Store state remotely and lock it. The default local state file does not work for teams. A remote backend keeps a shared copy and uses locking so two people cannot apply at once.
terraform {
backend "s3" {
bucket = "my-tf-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "tf-locks"
}
}
State often contains secrets such as generated passwords in plain text, so the backend must be encrypted and access-controlled.
Modules
A module is a reusable group of resources with inputs and outputs. Modules keep configurations DRY and let teams standardize patterns such as a network or a database.
module "network" {
source = "./modules/network"
cidr_block = "10.0.0.0/16"
env = "prod"
}
Variables parameterize a module, and outputs expose values for other code to consume:
variable "cidr_block" {
type = string
}
output "vpc_id" {
value = aws_vpc.main.id
}
A useful discipline is one state per environment. Keeping production and staging in separate states limits the blast radius of a mistaken apply.
Common Pitfalls and Trade-offs
- Reading the plan carefully. A change that forces replacement destroys and recreates a resource. For a database, that can mean data loss. The plan flags this; ignoring it is dangerous.
- Count and for_each indexing. Removing an item from the middle of a count-based list can cause Terraform to recreate unrelated resources, because they are tracked by position. Using for_each with stable keys avoids this.
- Drift management. Terraform only knows about resources it manages. Mixing manual changes with Terraform-managed ones leads to surprises.
There is a broader trade-off between declarative infrastructure and imperative scripts. Terraform excels at provisioning and converging to a desired state, but it is not a configuration management tool for in-place server tuning, nor an application deployment tool. Using it for jobs it was not designed for leads to brittle setups.
Summary
Terraform turns infrastructure into reviewed, version-controlled code. The plan-and-apply loop makes changes previewable, providers abstract each platform, and modules make configurations reusable. The single most important skill is respecting state: store it remotely, lock it, keep it per environment, and never let manual changes drift away from it.