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, and author reusable modules.

Install/update remote modules

Download and update remote module sources.

bashANYterraformgetmodules
bash
terraform get -update
Notes

Download and update remote module sources.

Init from module source

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

bashANYterraforminitfrom-modulemodules
bash
terraform init -from-module=git::https://github.com/acme/networking.git
Notes

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

Module source registry snippet

Use a Terraform Registry module with a version constraint.

hclANYterraformmoduleregistry
hcl
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 5.0"
}
Notes

Use a Terraform Registry module with a version constraint.

Module source Git snippet

Use a module source from Git with a specific ref.

hclANYterraformmodulegit
hcl
module "networking" {
  source = "git::https://github.com/acme/networking.git?ref=v1.2.3"
}
Notes

Use a module source from Git with a specific ref.

Providers

Work with provider requirements and mirrors.

Generate provider lock info for platform

Update the dependency lock file with checksums for target platforms.

bashANYterraformproviderslock
bash
terraform providers lock -platform=darwin_amd64 -platform=linux_amd64
Notes

Update the dependency lock file with checksums for target platforms.

Mirror providers locally

Download required providers into a local mirror directory.

bashANYterraformprovidersmirror
bash
terraform providers mirror ./terraform-providers
Notes

Download required providers into a local mirror directory.

Required providers snippet

Pin Terraform version and provider source/version constraints.

hclANYterraformrequired_providersversions
hcl
terraform {
  required_version = ">= 1.6.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}
Notes

Pin Terraform version and provider source/version constraints.

CLI config provider mirror snippet

Configure provider installation behavior in CLI config file.

hclANYterraformprovider_installationmirror
hcl
provider_installation {
  filesystem_mirror {
    path    = "/usr/share/terraform/providers"
    include = ["registry.terraform.io/hashicorp/*"]
  }
  direct {}
}
Notes

Configure provider installation behavior in CLI config file.

Registry and Reuse

Common registry-oriented tasks and patterns.

README module example pattern

Example of documenting reusable module outputs clearly.

hclANYterraformmoduleoutputsdocumentation
hcl
output "vpc_id" {
  description = "Created VPC ID"
  value       = aws_vpc.this.id
}
Notes

Example of documenting reusable module outputs clearly.

Version variables pattern

Example of typed input variables for reusable modules.

hclANYterraformmodulevariablesdocumentation
hcl
variable "name" {
  type        = string
  description = "Resource name prefix"
}
Notes

Example of typed input variables for reusable modules.

Dependency and Refactoring Patterns

Patterns for cleaner module and provider management.

Moved block snippet

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

hclANYterraformmovedrefactor
hcl
moved {
  from = aws_instance.old
  to   = aws_instance.web
}
Notes

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

Removed block snippet

Document intentional removal of resource addresses when refactoring.

hclANYterraformremovedrefactor
hcl
removed {
  from = aws_instance.legacy
}
Notes

Document intentional removal of resource addresses when refactoring.