A Beginner’s Guide to Terraform: What It Is and How to Use It
Terraform is an open-source infrastructure-as-code (IaC) tool by HashiCorp that lets you define and provision cloud resources—like servers, databases, and networks—using declarative configuration files. Instead of manually clicking through a cloud console, you write code that describes your desired infrastructure, and Terraform automatically builds and manages it. This approach makes environments reproducible, version-controllable, and easier to scale.
At its core, Terraform uses a simple language called HCL (HashiCorp Configuration Language). You define resources (e.g., aws_instance, google_storage_bucket) in .tf files. Terraform compares your code against the real-world state and applies only the necessary changes—a core principle called “desired state management.”
How to Get Started with Terraform
1. Install Terraform
Download the binary from the official website or use a package manager (e.g., Homebrew on macOS: brew install terraform). Verify the installation with terraform --version.
2. Write Your First Configuration
Create a directory and inside it a file main.tf. Example for an AWS EC2 instance:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
3. Initialize and Apply
- terraform init – Downloads the required provider plugins (e.g., AWS).
- terraform plan – Previews the changes Terraform will make.
- terraform apply – Executes the plan and creates the resources. Confirm with
yes.
4. Manage State and Destroy
Terraform stores the state of your infrastructure in a terraform.tfstate file (by default). To tear down all resources, run terraform destroy. For teams, use a remote backend (e.g., S3, Terraform Cloud) to share state safely.
Terraform supports hundreds of providers—AWS, Azure, GCP, Kubernetes, GitHub, and more. Once you grasp the basic workflow (init, plan, apply), you can manage complex multi-cloud setups with a single tool.