What is Terraform?
Terraform by HashiCorp lets you define infrastructure in code files. Instead of clicking through web dashboards, you write declarative configuration that can be version-controlled, reviewed, and reproduced.
Installation
wget https://releases.hashicorp.com/terraform/1.7.0/terraform_1.7.0_linux_amd64.zip
unzip terraform_*.zip
sudo mv terraform /usr/local/bin/
terraform versionBasic Structure
A Terraform project typically has these files:
main.tf— resource definitionsvariables.tf— input variablesoutputs.tf— output valuesterraform.tfvars— variable values (don't commit secrets)
Example Configuration
# main.tf
terraform {
required_providers {
local = {
source = "hashicorp/local"
}
}
}
resource "local_file" "example" {
content = "Hello, Terraform!"
filename = "${path.module}/hello.txt"
}Workflow
# Initialize (download providers)
terraform init
# Preview changes
terraform plan
# Apply changes
terraform apply
# Destroy resources
terraform destroyState Management
Terraform tracks resource state in terraform.tfstate. For team use, store state remotely:
- AWS S3 + DynamoDB for locking
- Terraform Cloud (free tier available)
- GitLab-managed state
Key Concepts
- Resources — infrastructure objects (servers, networks, DNS records)
- Providers — plugins for each cloud/service
- Modules — reusable groups of resources
- State — Terraform's record of what it manages