0
0
Terraformcloud~10 mins

Module versioning in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Module versioning
Define module source with version
Terraform init downloads module version
Terraform plan uses module version
Terraform apply deploys infrastructure
Update version in config to upgrade module
Terraform init -upgrade upgrades module version
Terraform apply deploys updated infra
Terraform uses module versioning to control which module code is used during deployment. You specify a version, Terraform downloads that version, and applies it. Updating the version updates the module.
Execution Sample
Terraform
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "3.14.0"
  name    = "my-vpc"
  cidr    = "10.0.0.0/16"
}
This code uses a specific version 3.14.0 of the AWS VPC module to create a VPC named 'my-vpc'.
Process Table
StepActionModule VersionTerraform CommandResult
1Define module with version 3.14.03.14.0terraform initModule version 3.14.0 downloaded
2Run terraform plan3.14.0terraform planPlan created using module 3.14.0
3Run terraform apply3.14.0terraform applyInfrastructure deployed with module 3.14.0
4Update module version to 3.15.03.15.0terraform init -upgradeModule upgraded to 3.15.0
5Run terraform apply3.15.0terraform applyInfrastructure updated with module 3.15.0
6No further changes3.15.0terraform applyNo changes, infra matches module 3.15.0
💡 Execution stops when infrastructure matches the specified module version and no changes are pending.
Status Tracker
VariableStartAfter Step 1After Step 4Final
module_versionundefined3.14.03.15.03.15.0
Key Moments - 2 Insights
Why do I need to run 'terraform init' after changing the module version?
Because 'terraform init' downloads the specified module version. Without it, Terraform uses the old version. See execution_table steps 1 and 4.
What happens if I don't specify a version in the module source?
Terraform uses the latest module version available, which can cause unexpected changes. Specifying a version locks the module to a known state.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what module version is used during the first 'terraform apply'?
A3.15.0
Blatest
C3.14.0
Dundefined
💡 Hint
Check the 'Module Version' column at step 3 in the execution_table.
At which step does Terraform upgrade the module version?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for 'Module upgraded' in the 'Result' column in execution_table.
If you skip 'terraform init -upgrade' after changing the version, what happens?
ATerraform uses the old module version
BTerraform throws an error
CTerraform uses the new version anyway
DTerraform deletes the module
💡 Hint
Refer to key_moments about why 'terraform init' is needed after version change.
Concept Snapshot
Terraform module versioning:
- Specify 'version' in module block to lock module version
- Run 'terraform init' to download module
- Use 'terraform init -upgrade' to update module version
- Terraform applies infrastructure using specified module version
- Helps keep infrastructure stable and predictable
Full Transcript
Module versioning in Terraform means specifying which version of a module to use in your configuration. When you write a module block with a version, Terraform downloads that exact version during 'terraform init'. Running 'terraform plan' and 'terraform apply' then uses that version to create or update infrastructure. If you want to upgrade the module, you change the version number and run 'terraform init -upgrade' to download the new version. This process ensures your infrastructure uses the module version you expect, avoiding surprises from automatic updates.