0
0
Terraformcloud~5 mins

Module versioning in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you use Terraform modules, you want to control which version of the module your infrastructure uses. Module versioning helps you pick a specific version so your setup stays stable and predictable.
When you want to reuse a module from the Terraform Registry but need a specific version to avoid unexpected changes.
When you maintain your own modules and want to update your infrastructure only after testing a new module version.
When working in a team and you want everyone to use the same module version for consistency.
When you want to roll back to a previous module version if a new one causes issues.
When you want to track which module version your infrastructure is currently using.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.0"
    }
  }
}

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "3.14.2"

  name = "example-vpc"
  cidr = "10.0.0.0/16"

  azs             = ["us-east-1a", "us-east-1b", "us-east-1c"]
  public_subnets  = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  private_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]

  enable_nat_gateway = true
}

The terraform block sets the Terraform and provider versions to ensure compatibility.

The module block pulls the VPC module from the Terraform Registry at version 3.14.2. This fixed version ensures your infrastructure uses a tested and stable module release.

The rest of the module configuration defines the VPC settings like CIDR and subnets.

Commands
This command initializes the Terraform working directory. It downloads the specified module version and provider plugins.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching ">= 4.0"... - Installing hashicorp/aws v4.50.0... - Installed hashicorp/aws v4.50.0 (signed by HashiCorp) Initializing modules... - module.vpc Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
This command shows what Terraform will do to create or update your infrastructure using the specified module version.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # module.vpc.aws_vpc.main will be created + resource "aws_vpc" "main" { + cidr_block = "10.0.0.0/16" + enable_dns_support = true + enable_dns_hostnames = true + tags = { + "Name" = "example-vpc" } } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the planned changes and creates the infrastructure using the module version specified.
Terminal
terraform apply -auto-approve
Expected OutputExpected
module.vpc.aws_vpc.main: Creating... module.vpc.aws_vpc.main: Creation complete after 5s [id=vpc-0a1b2c3d4e5f6g7h8] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approves the apply without asking for confirmation
This command lists all resources Terraform manages, showing that the module's resources are tracked.
Terminal
terraform state list
Expected OutputExpected
module.vpc.aws_vpc.main
Key Concept

If you remember nothing else from this pattern, remember: specifying a module version locks your infrastructure to a tested module release, preventing unexpected changes.

Common Mistakes
Not specifying the module version in the module block.
Terraform will use the latest module version, which may introduce breaking changes or unexpected behavior.
Always include the 'version' argument in the module block to lock to a specific module version.
Using an invalid or non-existent module version.
Terraform will fail to download the module, causing initialization errors.
Check the module's available versions on the Terraform Registry and use a valid version string.
Changing the module version without running 'terraform init' again.
Terraform will not download the new module version, so your infrastructure will still use the old one.
Run 'terraform init' after changing the module version to download the new version.
Summary
Define the module source and version in your Terraform configuration to control which module release is used.
Run 'terraform init' to download the specified module version and providers.
Use 'terraform plan' and 'terraform apply' to preview and apply infrastructure changes with the locked module version.
Check managed resources with 'terraform state list' to confirm module resources are tracked.