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

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

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"
}

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"
}

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

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

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"
    }
  }
}

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 {}
}

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
}

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"
}

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
}

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
}

Document intentional removal of resource addresses when refactoring.