Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
Variables & Outputs
- Input Variables
- Variable Types
- Default Values
- Environment Variables
- Output Values
- Variable Validation
State Management
- Terraform State File
- Terraform Remote State
- Terraform State Locking
- Terraform Drift Detection
- Terraform Refresh
- Terraform Import
Modules (Reusability)
- Terraform Modules
- Terraform Public Modules
- Terraform local modules
- Terraform Module Versioning
- Terraform Nested Modules
Provisioners & Lifecycle
π§© Terraform Module Versioning: Maintain Consistency Across Environments
When you manage infrastructure with Terraform, one of the biggest challenges is maintaining consistency across environments β like development, staging, and production.
Without proper version control, your team might:
- Deploy mismatched configurations
- Break production unexpectedly
- Lose traceability of changes
To solve this, Terraform introduces module versioning β a powerful mechanism to lock, track, and update your infrastructure code in a controlled way.
Just like application code uses Git tags or semantic versions, Terraform uses version numbers for its modules.
π‘ Simple Definition:
Terraform Module Versioning ensures that your infrastructure consistently uses the same module version across all environments, preventing unexpected changes.
πΉ Why Module Versioning Matters
| Challenge | Solution via Versioning |
|---|---|
| Module changes unexpectedly | Pin the version explicitly |
| Different environments use different code | Synchronize versions via tags |
| Hard to debug module errors | Trace back to exact version used |
| Breaking updates in module repo | Stay locked to stable version until tested |
| Compliance and audit issues | Track module version history |
πΉ Terraform Module Versioning in Action
Explanation: Different environments (Dev, Stage, Prod) can use specific versions of the same module β ensuring controlled, consistent deployments.
πΉ How Terraform Handles Module Versions
Terraform supports version control via:
-
Terraform Registry Versions
-
Example:
version = "5.0.1" -
Pulled directly from Terraform Registry releases.
-
-
Git Repository Tags
-
Example:
source = "git::https://github.com/company/module.git?ref=v1.2.0"
-
-
Local Modules (Manual Control)
- You manage versions manually through folders or Git branches.
πΉ Semantic Versioning (SemVer) Basics
Terraform follows Semantic Versioning (SemVer):
MAJOR.MINOR.PATCH| Segment | Meaning | Example |
|---|---|---|
| MAJOR | Breaking changes | 2.0.0 |
| MINOR | New features, backward-compatible | 2.1.0 |
| PATCH | Bug fixes or small improvements | 2.1.1 |
Example Progression:
v1.0.0 β v1.1.0 β v2.0.0
πΉ How to Specify Module Versions
β Example Syntax (from Registry)
module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "5.5.1"}Terraform will:
- Download exactly version 5.5.1
- Use it consistently unless changed
If you omit the version, Terraform defaults to the latest β which can lead to unintended changes.
πΉ 3 Unique Example Programs
π§ Example 1: AWS S3 Module (Using Version Lock)
main.tf
provider "aws" { region = "us-east-1"}
module "s3_bucket" { source = "terraform-aws-modules/s3-bucket/aws" version = "4.2.0"
bucket = "tf-demo-bucket-001" acl = "private"
tags = { Environment = "production" ManagedBy = "Terraform" }}
output "bucket_name" { value = module.s3_bucket.s3_bucket_id}Explanation:
version = "4.2.0"ensures Terraform always uses the same version of the S3 module.- If version
4.3.0is released later, your deployment wonβt change unless you manually upgrade.
β Result:
Consistent S3 bucket configuration across environments.
π§ Example 2: Azure Virtual Network (Git Source Versioning)
main.tf
provider "azurerm" { features {}}
module "vnet" { source = "git::https://github.com/company/terraform-azure-vnet.git?ref=v1.3.0"
resource_group_name = "tf-rg" location = "East US" vnet_name = "tf-vnet" address_space = ["10.0.0.0/16"]}Explanation:
ref=v1.3.0pins to a Git tag.- Ensures Azure VNet configuration uses a tested, stable version from your internal Git repository.
β Result:
Predictable VNet deployments in every environment.
π§ Example 3: GCP Compute Instance (Version Constraints)
main.tf
provider "google" { project = "gcp-demo" region = "us-central1"}
module "compute_instance" { source = "terraform-google-modules/vm/google" version = "~> 7.0.0" # Allow patch/minor updates but not major
name = "tf-instance" machine_type = "e2-medium" zone = "us-central1-a"}Explanation:
~> 7.0.0means: use any version from7.0.0up to but **not including 8.0.0`.- Allows safe minor updates but prevents breaking changes.
β Result:
Controlled flexibility β you can update safely without breaking production.
πΉ Version Constraint Operators
| Operator | Meaning | Example |
|---|---|---|
= | Exact version | version = "3.0.1" |
!= | Exclude version | version != "3.1.0" |
> | Greater than | version > "3.0.0" |
< | Less than | version < "3.0.0" |
>= | Greater than or equal | version >= "3.1.0" |
~> | Compatible with | version = "~> 3.1" (means 3.1.x) |
πΉ Best Practices for Module Versioning
-
β Always pin module versions Prevent accidental updates or environment drift.
-
π Use semantic versioning for your custom modules Helps collaborators understand update impact.
-
π¦ Automate testing before upgrading versions Use CI/CD pipelines to test infrastructure changes.
-
π Document module changes clearly Maintain a
CHANGELOG.mdfile for each module. -
π Update versions gradually Upgrade Dev β Stage β Prod after successful testing.
-
π·οΈ Tag your Git releases Each stable version should have a corresponding Git tag.
πΉ How to Remember the Concept
Use the mnemonic βVERSIONβ:
| Letter | Meaning |
|---|---|
| V | Version pinning avoids surprises |
| E | Environments stay consistent |
| R | Release control through semantic tagging |
| S | Stable infrastructure deployments |
| I | Identify module differences easily |
| O | Organize updates systematically |
| N | No untested upgrades in production |
π‘ Memory Trick:
βVERSION means your infrastructure version stays consistent, organized, and safe.β
πΉ Interview & Certification Preparation
Common Questions
-
Q: What is Terraform module versioning? A: The process of controlling and locking specific versions of Terraform modules to ensure consistent infrastructure deployment.
-
Q: Why should you pin a module version? A: To avoid unexpected changes when module authors update code.
-
Q: What is semantic versioning in Terraform? A: The use of major, minor, and patch numbers to indicate compatibility and changes in modules.
-
Q: What happens if no version is specified? A: Terraform automatically fetches the latest version β which can cause infrastructure drift.
-
Q: Whatβs the difference between
version = "5.0.0"andversion = "~> 5.0.0"? A: The first locks an exact version; the second allows compatible minor or patch upgrades.
π§ Study Technique β βC.A.R.D.β
| Step | Description |
|---|---|
| C | Compare module versions using the Registry |
| A | Apply each version to test differences |
| R | Record notes on version behavior |
| D | Document a version upgrade plan |
πΉ Why Itβs Important to Learn Terraform Module Versioning
1. Prevents Infrastructure Drift
Without versioning, modules can update automatically β causing unexpected production changes.
2. Improves Team Collaboration
Everyone uses the same version, ensuring consistent builds.
3. Enhances Compliance & Auditing
Auditors can verify which module version created a specific resource.
4. Simplifies Debugging
Knowing the version helps identify exactly which code produced the problem.
5. Ensures Stability During CI/CD
Automated pipelines rely on pinned versions to deploy predictably.
6. Essential for Certification
Terraform Associate and Professional exams often include versioning and module usage scenarios.
πΉ Common Mistakes to Avoid
| Mistake | Why Itβs a Problem | Solution |
|---|---|---|
| No version pin | Infrastructure may change unexpectedly | Always define version explicitly |
| Using βlatestβ version | Can cause breakages when modules update | Use ~> or exact version |
| Forgetting to test new versions | May break production deployments | Test in dev first |
| Not tagging Git releases | Hard to track module history | Use Git tags (v1.0.0) |
| Mixing module sources | Confuses dependency resolution | Use consistent source patterns |
πΉ Real-World Use Cases
1. Multi-Environment Infrastructure
- Dev β Stage β Prod all use the same module version for stability.
2. CI/CD Pipelines
- Jenkins or GitHub Actions references a pinned module version to deploy consistent infra.
3. Large Enterprise Teams
- Shared modules versioned internally to align across 100+ engineers.
4. Disaster Recovery
- Roll back easily to previous versions if a new release introduces errors.
πΉ Version Upgrade Strategy
- Pin β Lock versions for stability.
- Test β Apply new version in dev environment.
- Promote β Upgrade progressively across environments.
- Document β Update changelog and commit message.
- Monitor β Validate infrastructure behavior post-upgrade.
πΉ Summary Table
| Topic | Key Point |
|---|---|
| Definition | Controlling module versions for consistent infra |
| Syntax | version = "x.x.x" |
| Operators | =, ~>, >= for flexibility |
| Purpose | Prevent untested or breaking module updates |
| Mnemonic | VERSION |
| Exam Tip | Understand SemVer and pinning syntax |
| Best Practice | Test updates before production rollout |
πΉ Example Versioning Workflow
# Check current versionterraform providers
# Upgrade moduleterraform init -upgrade
# Review plan with new versionterraform plan
# Apply if stableterraform applyTerraform Module Versioning is one of the most powerful ways to keep your infrastructure predictable, stable, and compliant. By version-locking your modules, you ensure that all environments β from dev to production β run with the same, tested configuration.
Versioning brings:
- Consistency β
- Reliability β
- Safety β
- Traceability β
π¬ βVersion control isnβt just for code β itβs for your entire cloud infrastructure.β
Whether youβre preparing for a Terraform certification or managing multi-cloud environments, mastering module versioning will make you a stronger, more reliable DevOps professional.