The Complete Guide to Terraform¶
Infrastructure as Code with Terraform. Declarative, reproducible, version-controlled.
What is Terraform¶
A declarative tool for managing infrastructure. You write what you want, Terraform creates it.
Basic Workflow¶
terraform init # downloads providers terraform plan # shows what will change terraform apply # applies changes terraform destroy # deletes everything
HCL Syntax¶
provider “aws” { region = “eu-west-1” }
resource “aws_instance” “web” { ami = “ami-12345” instance_type = “t3.micro” tags = { Name = “web-server” } }
State¶
Terraform stores infrastructure state. In a team, use remote state (S3 + DynamoDB lock).
terraform { backend “s3” { bucket = “tf-state” key = “prod/terraform.tfstate” region = “eu-west-1” } }
Variables¶
variable “instance_type” { default = “t3.micro” type = string } variable “tags” { type = map(string) }
Modules¶
module “vpc” { source = “./modules/vpc” cidr = “10.0.0.0/16” }
Best Practices¶
- Remote state with locking
- Small, focused modules
- terraform plan in CI/CD
- Immutable infrastructure
- Tagging convention
Tip¶
Never change infrastructure manually. Everything through Terraform. Drift = problems.