Using Terraform Module Sources: Local, Registry, and Git
📖 Scenario: You are managing infrastructure with Terraform. You want to reuse code by using modules from different sources: a local folder, the Terraform Registry, and a Git repository.
🎯 Goal: Build a Terraform configuration that uses three modules: one from a local path, one from the Terraform Registry, and one from a Git repository. This will help you organize and reuse your infrastructure code efficiently.
📋 What You'll Learn
Create a Terraform configuration file named main.tf.
Add a module named local_module sourced from a local folder ./modules/local_module.
Add a module named registry_module sourced from the Terraform Registry terraform-aws-modules/vpc/aws with version 3.14.2.
Add a module named git_module sourced from a Git repository https://github.com/hashicorp/example.git using the main branch.
💡 Why This Matters
🌍 Real World
Terraform modules help organize and reuse infrastructure code efficiently across projects and teams. Using different module sources allows flexibility in managing infrastructure components.
💼 Career
Understanding how to use modules from local paths, registries, and Git repositories is essential for infrastructure engineers and DevOps professionals working with Terraform.
Progress0 / 4 steps
1
Create the initial Terraform configuration with a local module source
Create a file named main.tf and add a module block named local_module with the source set to ./modules/local_module.
Terraform
Hint
Use the module block with the exact name local_module and set source to ./modules/local_module.
2
Add a module from the Terraform Registry
In main.tf, add a module block named registry_module with the source set to terraform-aws-modules/vpc/aws and version set to 3.14.2.
Terraform
Hint
Use the exact module name registry_module, set source to the registry path, and specify version as 3.14.2.
3
Add a module sourced from a Git repository
In main.tf, add a module block named git_module with the source set to git::https://github.com/hashicorp/example.git?ref=main.
Terraform
Hint
Use the exact module name git_module and set source to the Git URL with git:: prefix and ?ref=main to specify the branch.
4
Complete the Terraform configuration with required Terraform block
Add a terraform block at the top of main.tf specifying required_version as >= 1.0.0 and a required_providers block with aws provider source hashicorp/aws and version ~> 4.0.
Terraform
Hint
Start your file with a terraform block specifying the required Terraform version and the AWS provider source and version.
Practice
(1/5)
1. What does the source argument specify in a Terraform module block?
easy
A. The location where Terraform finds the module code
B. The version of Terraform to use
C. The cloud provider for the module
D. The output variables of the module
Solution
Step 1: Understand the role of source in a module
The source argument tells Terraform where to get the module code from, such as a local folder, registry, or Git repo.
Step 2: Identify what source does not specify
It does not specify Terraform version, cloud provider, or outputs; those are separate configurations.
Final Answer:
The location where Terraform finds the module code -> Option A
Quick Check:
Module source = module location [OK]
Hint: Remember: source means 'where to get the module' [OK]
Common Mistakes:
Confusing source with Terraform version
Thinking source defines outputs
Assuming source sets cloud provider
2. Which of the following is the correct syntax to use a local module located in a folder named modules/network?
easy
A. module "net" { source = "modules/network" }
B. module "net" { source = "registry/modules/network" }
C. module "net" { source = "git::https://example.com/network.git" }
D. module "net" { source = "network" }
Solution
Step 1: Identify local module source syntax
Local modules use a relative or absolute path as the source. Here, "modules/network" is a relative path.