,

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

Posted by

🧾 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.


Leave a Reply

Your email address will not be published. Required fields are marked *

0
Would love your thoughts, please comment.x
()
x