🧾 Terraform Cheat Sheet: The Ultimate Reference for Infrastructure as Code (2025)

Learn and master Terraform with this complete cheat sheet β€” from basics to advanced automation.


πŸš€ What is Terraform?

Terraform by HashiCorp is a declarative Infrastructure as Code (IaC) tool that allows you to provision, manage, and destroy infrastructure using human-readable configuration files.

In short: you write .tf code to define your cloud infrastructure, and Terraform makes it real β€” across AWS, Azure, GCP, and more.


πŸ“ Typical Terraform Project Structure

my-project/
β”œβ”€β”€ main.tf          # Core infrastructure code
β”œβ”€β”€ variables.tf     # Variable declarations
β”œβ”€β”€ outputs.tf       # Outputs (e.g., public IPs)
β”œβ”€β”€ terraform.tfvars # Actual variable values
β”œβ”€β”€ backend.tf       # Remote backend config (S3, Terraform Cloud)

βš™οΈ Common Terraform Commands

CommandDescription
terraform initInitializes working directory (downloads provider plugins)
terraform planPreviews what Terraform will change
terraform applyApplies changes to reach desired state
terraform destroyDeletes all resources created
terraform validateChecks for syntax correctness
terraform fmtAuto-formats .tf files
terraform outputDisplays output values
terraform showShows current state
terraform taint <resource>Marks resource for recreation on next apply
terraform state listLists all managed resources

πŸ“˜ Terraform Configuration Syntax (HCL)

πŸ”Ή 1. Providers

provider "aws" {
  region = var.region
}

πŸ”Ή 2. Resources

resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = "t2.micro"
}

πŸ”Ή 3. Variables

variable "region" {
  description = "AWS Region"
  type        = string
  default     = "us-east-1"
}

Use in code:

region = var.region

πŸ”Ή 4. Outputs

output "instance_ip" {
  value = aws_instance.web.public_ip
}

πŸ”Ή 5. Locals

locals {
  name_tag = "web-${var.environment}"
}

πŸ“¦ Remote Backends (S3 Example)

terraform {
  backend "s3" {
    bucket         = "my-tf-state"
    key            = "env/dev/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "tf-locks"
  }
}

πŸ“¦ Modules (Reusable Blocks)

module "vpc" {
  source     = "terraform-aws-modules/vpc/aws"
  name       = "my-vpc"
  cidr_block = "10.0.0.0/16"
}

πŸ” Security Best Practices

TaskTool
Lint & secure configsTFLint, Checkov, TFSec
Secrets managementAvoid hardcoding – use Vault, SOPS, environment variables
Policy enforcementOPA, Sentinel (in Terraform Cloud)

πŸ§ͺ Testing Terraform

ToolDescription
terraform validateSyntax checks
TFLintLinting and best practices
CheckovStatic security scanning
TerratestIntegration testing using Go

πŸ”„ Useful Patterns & Snippets

πŸ”Έ Conditional Logic

resource "aws_instance" "web" {
  count = var.create_instance ? 1 : 0
}

πŸ”Έ Dynamic Blocks

resource "aws_security_group" "example" {
  dynamic "ingress" {
    for_each = var.ingress_rules
    content {
      from_port   = ingress.value.from_port
      to_port     = ingress.value.to_port
      protocol    = ingress.value.protocol
      cidr_blocks = ingress.value.cidr_blocks
    }
  }
}

πŸ”Έ Workspaces

terraform workspace list
terraform workspace new staging
terraform workspace select staging

🧠 Best Practices

βœ… Use terraform plan before every apply
βœ… Format code using terraform fmt
βœ… Commit .tf files, NOT .tfstate
βœ… Store state in a remote backend with locking
βœ… Use modules to avoid repetition
βœ… Protect secrets (NEVER hardcode passwords in .tf files)


πŸ“š Recommended Resources

ResourceLink
Official Docshttps://developer.hashicorp.com/terraform
Terraform Registryhttps://registry.terraform.io
DevOpsSchool Terraform Tutorialshttps://www.devopsschool.com/blog/category/terraform
Hands-on Labshttps://learn.hashicorp.com

🏁 Final Thoughts

Terraform isn’t just an IaC tool β€” it’s an ecosystem.
Learning to use it well will let you:

  • Automate cloud provisioning
  • Create reproducible environments
  • Build infrastructure pipelines
  • Work in real enterprise cloud projects

Treat infrastructure like code, version it, review it, test it β€” that’s Terraform magic.


Category: 
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments