Terraform Modules and Providers Cheat Sheet

Commands and snippets for modules, providers, version constraints, registry access, and dependency management.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Modules
Install/update remote modules
terraform get -update

# Download and update remote module sources.

Init from module source
terraform init -from-module=git::https://github.com/acme/networking.git

# Copy a module into an empty working directory and initialize it.

Module source registry snippet
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 5.0"
}

# Use a Terraform Registry module with a version constraint.

Module source Git snippet
module "networking" {
  source = "git::https://github.com/acme/networking.git?ref=v1.2.3"
}

# Use a module source from Git with a specific ref.

## Providers
Generate provider lock info for platform
terraform providers lock -platform=darwin_amd64 -platform=linux_amd64

# Update the dependency lock file with checksums for target platforms.

Mirror providers locally
terraform providers mirror ./terraform-providers

# Download required providers into a local mirror directory.

Required providers snippet
terraform {
  required_version = ">= 1.6.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# Pin Terraform version and provider source/version constraints.

CLI config provider mirror snippet
provider_installation {
  filesystem_mirror {
    path    = "/usr/share/terraform/providers"
    include = ["registry.terraform.io/hashicorp/*"]
  }
  direct {}
}

# Configure provider installation behavior in CLI config file.

## Registry and Reuse
README module example pattern
output "vpc_id" {
  description = "Created VPC ID"
  value       = aws_vpc.this.id
}

# Example of documenting reusable module outputs clearly.

Version variables pattern
variable "name" {
  type        = string
  description = "Resource name prefix"
}

# Example of typed input variables for reusable modules.

## Dependency and Refactoring Patterns
Moved block snippet
moved {
  from = aws_instance.old
  to   = aws_instance.web
}

# Use moved blocks to preserve state during resource renames in configuration.

Removed block snippet
removed {
  from = aws_instance.legacy
}

# Document intentional removal of resource addresses when refactoring.