What is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool by HashiCorp. It lets you define cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share.
Core Concepts
Providers
Providers are plugins that let Terraform interact with cloud platforms and services. AWS, Azure, GCP, and hundreds of others are supported.
provider "aws" {
region = "ap-south-1"
}
Resources
Resources are the most important element in Terraform. Each resource block describes one or more infrastructure objects.
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
tags = {
Name = "web-server"
Environment = "production"
}
}
Variables
Variables make your configurations flexible and reusable.
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t3.micro"
}
The Terraform Workflow
terraform init— Initialize the working directoryterraform plan— Preview changes before applyingterraform apply— Apply the planned changesterraform destroy— Tear down all managed resources
State Management
Terraform maintains a state file that maps real-world resources to your configuration. For team collaboration, store state remotely:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "ap-south-1"
}
}
Conclusion
Terraform transforms infrastructure management from manual, error-prone processes into reproducible, version-controlled code. Start small, build modules, and gradually adopt it across your entire infrastructure.