Terraform Cheatsheets & Examples - Infrastructure as Code Guide

Comprehensive Terraform cheatsheets and examples for infrastructure as code. Learn CLI commands, merge functions, validations, and best practices for managing cloud resources.

Terraform Cheatsheets
Terraform CLI Commands and Usage

This section provides essential Terraform CLI commands for managing your infrastructure as code.

Terraform Formatting

Ensure consistent code style with the terraform fmt command.

terraform fmt
Terraform Validation

Validate Terraform configuration syntax before applying changes.

terraform validate

Validate syntax while skipping backend configuration checks.

terraform validate -backend=false
Terraform Initialization

Initialize your Terraform working directory, downloading necessary providers.

terraform init
Terraform Planning

Generate an execution plan to review proposed changes to your infrastructure.

terraform plan

Create a targeted plan for specific resources or modules.

terraform plan -target=module.platform -out=platform.tfplan
Terraform Application

Apply the execution plan to create, update, or delete infrastructure resources.

terraform apply

Apply a specific plan file.

terraform apply "platform.tfplan"
Terraform Language Features Merge Function for Dynamic Configuration

Utilize the merge function to combine default configurations with environment-specific overrides, such as tags.

variable "default_tags" {
  description = "Default tags to apply to resources."
  default = {
    ManagedBy   = "terraform"
    Environment = "local"
  }
}

variable "name" {
  description = "A name for the resource."
  default = "testing"
}

variable "environment" {
  description = "The deployment environment."
  default = "dev"
}

output "merged_tags" {
  description = "Tags after merging defaults with environment-specific values."
  value = merge(var.default_tags, tomap({"Name" = "prefix-${var.name}", "Environment" = var.environment}))
}
Input Variable Validations

Implement validation rules for input variables to ensure data integrity and prevent misconfigurations.

Length Validation

Enforce a maximum length for string variables.

variable "service_name" {
  description = "The name of the service."
  type = string

  validation {
    condition     = length(var.service_name) < 40
    error_message = "The service_name value cannot be more than 40 characters."
  }
}
Regex Match Validation

Use regular expressions to validate string formats for variables like environment names.

variable "env_name" {
  description = "The name of the environment."
  type = string
  default = null
  validation {
    condition  = can(regex("^(dev|staging|production|ephemeral-.*)+$", var.env_name))
    error_message = "For the env_name value, only dev, staging, production, and ephemeral-* are allowed."
  }
}
Conditional Logic with Booleans

Control resource configurations dynamically using boolean variables.

Example: Enabling or disabling a warm pool based on a boolean flag.

variable "warm_pool_enabled" {
  description = "Enable or disable the warm pool."
  type        = bool
  default     = true
}

resource "aws_autoscaling_group" "example" {
  # ... other configurations ...

  warm_pool {
    pool_state                  = "Running"
    min_size                    = var.warm_pool_enabled ? 1 : 0
    max_group_prepared_capacity = var.warm_pool_enabled ? 1 : 0
  }
}
Terraform Learning Resources and Examples

Explore these resources to deepen your understanding and find practical examples for Terraform.

Terraform Tutorials and Guides Terraform Examples and Workshops Terraform with Ansible Integration Terraform State Management Terraform Provider Examples Terraform AWS Workshops