Overview and What You Will Learn
The Terraform Registry at registry.terraform.io is a public catalogue of reusable modules and providers, and a module is a packaged set of Terraform resources you call with a module block. Someone has very likely already written and hardened a module for what you are about to build, such as a VPC, an EKS cluster or an RDS database. In this lab, you will judge whether a module is trustworthy, call one with the right source and version, read its documentation, use a private registry, and publish and version your own module.
- Evaluating a community module before you trust it
- Writing a
moduleblock with a three-part source and a version constraint - Reading the Inputs, Outputs and Resources tabs
- Using modules from a private registry in Terraform Cloud
- Publishing a module to the public registry
- Versioning your own module with semantic versioning
Why This Matters in Production
A platform engineer at a company like Shopify is asked to build an EKS cluster. Hand-writing the IAM roles, node groups, security groups and OIDC provider takes weeks and tends to miss obscure AWS requirements. A widely used community module encodes those details, and when AWS changes an API, the maintainers ship a fix that you receive by raising a version constraint. The saving is not only time. You inherit thousands of other engineers' bug reports.
Core Principles
A module call has two parts that matter for the registry: a source address in the form namespace/name/provider, and a version constraint. During terraform init, Terraform resolves the source, picks the newest release that satisfies the constraint, and downloads it into .terraform/modules/.
source + version] --> I[terraform init] I --> R[(registry.terraform.io
resolves newest match)] R --> D[Download module code] D --> C[Cache in
.terraform/modules/] classDef cfg fill:#DBEAFE,color:#1E293B classDef reg fill:#326CE5,color:#FFFFFF classDef cache fill:#475569,color:#FFFFFF class M cfg class I,R reg class D,C cache
Version constraints decide which releases are acceptable. The ~> operator is the one to learn, because it allows increases only in the rightmost version number you wrote:
| Constraint | Allowed versions |
|---|---|
"5.8.1" |
Exactly 5.8.1 |
"~> 5.8.1" |
5.8.1 or later 5.8.x, never 5.9 |
"~> 5.8" |
5.8 or later 5.x, never 6.0 |
"~> 5.0" |
Any 5.x, never 6.0 |
">= 5.0" |
Anything from 5.0 up, including future majors |
The .terraform.lock.hcl file locks provider versions only. It does not lock modules, so a module constraint such as ~> 5.0 can resolve to a newer release on the next terraform init -upgrade.
Detailed Step-by-Step Practical Lab
The version numbers in this lab are examples. Check each module's registry page for its current major version before you copy a constraint.
Step 1 - Evaluate a Module Before Using It
Open the module's registry page and check these signals:
1. Verified badge the publisher's identity is confirmed by HashiCorp2. Download count very large numbers mean broad real-world use3. Last published date recent releases mean active maintenance4. Source repository check open issues, response times and maintainers5. Release history many tagged releases suggest a mature moduleTipA verified badge confirms who published the module, not that the code is free of bugs. Still read the Resources tab to see exactly what it will create in your account.
Step 2 - Call a Registry Module
Create main.tf and call the community VPC module:
module "vpc" { source = "terraform-aws-modules/vpc/aws" # namespace/name/provider version = "~> 5.0" name = "mumbai-prod-vpc" cidr = "10.0.0.0/16" azs = ["ap-south-1a", "ap-south-1b", "ap-south-1c"] private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] enable_nat_gateway = true single_nat_gateway = true # one shared NAT gateway lowers cost}Step 3 - Download It and See What Was Selected
terraform initcat .terraform/modules/modules.jsonterraform planThe modules.json file shows the exact module version Terraform selected. Review the plan to see every resource the module will create.
Step 4 - Move to a Newer Version Deliberately
Changing a constraint, or wanting a newer release inside the same constraint, requires -upgrade:
terraform init -upgradeCommon MistakeUsing
>= 5.0with no upper bound. The nextterraform init -upgradecan pull in a new major release that renames variables and breaks your plan.
Step 5 - Read the Module Documentation
Each registry page has three tabs that matter:
Inputs variables with no default are REQUIRED, and defaults you may want to override for productionOutputs values you can reference as module.NAME.OUTPUT_NAMEResources what the module actually creates, useful when debugging an unexpected object in your accountChain module outputs into your own resources:
resource "aws_security_group" "app" { name = "mumbai-prod-app-sg" vpc_id = module.vpc.vpc_id # an output documented on the module's page} output "private_subnet_ids" { value = module.vpc.private_subnets}Step 6 - Pin Exact Versions Where Stability Matters
For production, pin an exact release and upgrade on purpose. You can also pin a Git tag directly:
module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "5.8.1" # exact, upgraded by pull request} module "vpc_from_git" { source = "git::https://github.com/terraform-aws-modules/terraform-aws-vpc.git?ref=v5.8.1"}Tools such as Renovate or Dependabot can open pull requests when a newer module release appears, so upgrades are reviewed instead of silent.
Step 7 - Use a Private Registry for Internal Modules
Company-specific patterns belong in a private registry in Terraform Cloud or Terraform Enterprise. The source address adds your hostname and organisation:
module "lambda_with_logging" { source = "app.terraform.io/zerodha-org/lambda-standard/aws" version = "2.1.0"}You get the same version constraints and documentation pages as the public registry, scoped to modules your organisation publishes.
Step 8 - Publish Your Own Module
The public registry reads modules from public GitHub repositories. The repository name must be exactly terraform-<PROVIDER>-<NAME>:
Correct: terraform-aws-ecs-serviceIncorrect: ecs-service-moduleIncorrect: aws-terraform-ecs-serviceBefore publishing, make sure the repository follows the standard module structure, with main.tf, variables.tf, outputs.tf and a README.md at the root. Then create a semantic version tag:
git tag v1.0.0git push origin v1.0.0Sign in to registry.terraform.io with GitHub and choose Publish. From then on, each new vX.Y.Z tag you push appears as a new version automatically.
Step 9 - Version Your Module with Semantic Versioning
v1.0.0 to v1.0.1 PATCH bug fix, no change for existing callersv1.0.0 to v1.1.0 MINOR new optional variable, backward compatiblev1.0.0 to v2.0.0 MAJOR a required variable renamed, removed or retypedCallers protect themselves with a constraint such as version = "~> 1.5", which accepts all 1.x updates from 1.5 on and never jumps to 2.0.0.
Production Best Practices & Common Pitfalls
Common MistakeTreating the lock file as protection for modules. It locks providers only, so an open-ended module constraint can change what you deploy between two runs of
terraform init -upgrade.
SecurityA module runs with the permissions of your Terraform credentials. Read what it creates, prefer verified and widely used modules, and pin exact versions in production pipelines.
- Read the changelog before crossing a major version. A jump such as
~> 4.0to~> 5.0often renames inputs or changes defaults. - Do not fork a module to add one variable. A fork makes you responsible for every future security fix and cloud API change. Check whether the module already exposes an override first.
- Raise provider constraints when a module needs them. A newer module release can require a newer provider than your root
required_providersallows. - Keep module APIs stable. Renaming a variable is a breaking change, so release it as a new major version.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
terraform init |
Download modules and providers |
terraform init -upgrade |
Re-resolve modules and providers to the newest allowed versions |
terraform get -update |
Refresh module sources only |
terraform providers |
Show provider requirements, including those from modules |
cat .terraform/modules/modules.json |
See the exact module versions selected |
| Error | Cause | Fix |
|---|---|---|
No available releases match |
Constraint too narrow or mistyped | Check available versions on the registry page |
Module not found |
Wrong namespace, name or provider order | Re-copy the three-part address from the registry |
| Unexpected resources after upgrade | Crossed a major version | Read the changelog before widening the constraint |
Unsupported argument |
Input renamed in a newer release | Compare your inputs with the Inputs tab |
| Provider version conflict | New module needs a newer provider | Update required_providers in the root module |