Post

Terrafrom

🌱 Terraform Cheatsheet

Terraform is an Infrastructure as Code (IaC) tool that allows you to define and provision infrastructure using declarative configuration files. This cheatsheet covers essential Terraform commands, configurations, and best practices.


πŸ”„ Installation

Linux / MacOS

Install Terraform using a package manager:

1
2
brew install terraform  # MacOS
sudo apt install terraform  # Ubuntu/Debian

Windows

Install Terraform using Chocolatey:

1
choco install terraform

Verify Installation

Check the installed Terraform version:

1
terraform version

πŸ“š Basic Commands

Initialize Terraform in the directory:

1
terraform init

Show execution plan without applying changes:

1
terraform plan

Apply changes to infrastructure:

1
terraform apply

Destroy all managed infrastructure:

1
terraform destroy

Format Terraform files to standard:

1
terraform fmt

Validate configuration syntax:

1
terraform validate

Show outputs defined in configuration:

1
terraform output

List used providers:

1
terraform providers

πŸ“‚ Project Structure

A typical Terraform project is structured as follows:

1
2
3
4
5
6
7
project-folder/
β”œβ”€β”€ main.tf       # Main configuration file
β”œβ”€β”€ variables.tf  # Input variables
β”œβ”€β”€ outputs.tf    # Output values
β”œβ”€β”€ provider.tf   # Provider configuration
β”œβ”€β”€ terraform.tfstate  # State file (after apply)
β”œβ”€β”€ terraform.tfvars   # Variable definitions

πŸ“– Writing a Basic Configuration

Define an AWS provider and an EC2 instance:

1
2
3
4
5
6
7
8
provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

πŸ“Š Variables & Outputs

Defining Variables (variables.tf)

Define an instance type variable:

1
2
3
4
variable "instance_type" {
  type    = string
  default = "t2.micro"
}

Using Variables

Reference the variable inside a resource:

1
2
3
resource "aws_instance" "example" {
  instance_type = var.instance_type
}

Outputs (outputs.tf)

Retrieve instance public IP:

1
2
3
output "instance_ip" {
  value = aws_instance.example.public_ip
}

πŸ“œ State Management

List all resources in state:

1
terraform state list

Show details of the current state:

1
terraform show

Sync state file with real-world resources:

1
terraform refresh

Move resources in state:

1
terraform state mv

Remove resources from state without deleting them:

1
terraform state rm

🏠 Remote State Storage

Storing Terraform state remotely helps in team collaboration.

1
2
3
4
5
6
7
8
terraform {
  backend "s3" {
    bucket         = "my-terraform-bucket"
    key            = "state/terraform.tfstate"
    region         = "us-west-2"
    encrypt        = true
  }
}

πŸ”§ Modules

Modules allow you to reuse Terraform configurations.

Creating a Module (modules/vm/main.tf)

1
2
3
4
variable "instance_type" {}
resource "aws_instance" "vm" {
  instance_type = var.instance_type
}

Using a Module

1
2
3
4
module "web" {
  source         = "./modules/vm"
  instance_type  = "t3.small"
}

πŸ”’ Secrets & Sensitive Data

Avoid hardcoding secrets in .tf files. Use environment variables:

1
export TF_VAR_password="my-secret-password"

Or use a secrets manager like AWS Secrets Manager, HashiCorp Vault, or SSM Parameter Store.


πŸ”„ Terraform Lifecycle Hooks

Control resource updates with lifecycle rules:

1
2
3
4
5
6
resource "aws_instance" "example" {
  lifecycle {
    create_before_destroy = true
    ignore_changes = [ tags ]
  }
}

πŸ” Debugging & Troubleshooting

Enable debugging:

1
tf debug

Save execution plan for debugging:

1
terraform plan -out=tfplan

Set the log level:

1
export TF_LOG=DEBUG

πŸ’‘ Best Practices

  • Use remote state storage for collaboration.
  • Keep Terraform configurations modular.
  • Use .ignore to exclude unnecessary files.
  • Never commit terraform.tfstate to version control.

This cheatsheet provides a quick reference for working with Terraform effectively. Happy coding! πŸš€

This post is licensed under CC BY 4.0 by the author.