What Is Terraform?
Terraform is an infrastructure-as-code tool that lets you define cloud resources in declarative configuration files. It supports multiple providers (AWS, GCP, Azure, Proxmox, and more).
Installation
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install -y terraformBasic Configuration
Create main.tf:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
tags = {
Name = "web-server"
}
}Core 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:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
}
}Variables and Outputs
variable "instance_type" {
description = "EC2 instance type"
default = "t3.micro"
}
output "public_ip" {
value = aws_instance.web.public_ip
}Key Concepts
- Declarative — you describe what you want, Terraform figures out how
- Plan before apply — always review changes before applying
- State file — never edit manually, use
terraform statecommands - Modules — reusable components for common patterns