Module Sources
Imagine you’re building your Lego city with Terraform. Sometimes you design your own sets, but other times you grab a ready‑made Lego kit from the store or borrow one from a friend.
Terraform modules work the same way: you can source them locally, from the public registry, from GitHub, or even from private repositories. Knowing where modules come from - and how to reference them - is key to building scalable, collaborative infrastructure.
Local Modules
- Stored in your project directory.
- Use case: Quick prototypes, custom modules for a single project.
Referenced with a relative path:
module "web" {
source = "./modules/ec2"
}
| # | Pros | # | Cons |
|---|---|---|---|
| 1 | Easy to create and customize. | 1 | Not easily shareable across teams. |
| 2 | Great for project‑specific logic. | 2 | No versioning or central management. |
| 3 | No external dependencies. |
Terraform Registry Modules
- Publicly available modules maintained by the community or providers.
- Use case: Standardized, battle‑tested modules for common infrastructure patterns.
Referenced with namespace/name/provider:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
}
| # | Pros | # | Cons |
|---|---|---|---|
| 1 | Community‑tested and widely used. | 1 | Less flexibility—must adapt to module design. |
| 2 | Versioning support for stability. | 2 | External dependency (requires internet or registry access). |
| 3 | Saves time—no need to reinvent common patterns. |
Local vs Registry Modules
| # | Local Modules | # | Registry Modules |
|---|---|---|---|
| 1 | Custom project logic. | 1 | Standardized infrastructure (VPCs, subnets, security groups). |
| 2 | Rapid prototyping. | 2 | Large teams needing consistency. |
| 3 | Small teams or single‑project use. | 3 | Enterprise projects with compliance requirements. |
GitHub / Git Repositories
- Use case: Custom modules shared across teams, versioned with Git tags.
Modules can be sourced directly from GitHub or any Git repo:
module "web" {
source = "git::https://github.com/user/repo.git//modules/ec2?ref=v1.0.0"
}
Private Module Repositories
- Enterprises often host private registries or repos.
- Use case: Secure, internal modules for compliance and governance.
Example (Terraform Cloud private registry):
module "network" {
source = "app.terraform.io/my-org/network/aws"
version = "1.2.0"
}
Choosing the Right Source
- Local: Fast iteration, project‑specific.
- Registry: Community‑tested, standardized.
- GitHub: Custom, version‑controlled, team‑shared.
- Private: Enterprise‑grade, secure, compliant.
Hands‑On Lab / Demo
Lab: Using Multiple Module Sources
GitHub Security Group module:
module "sg" {
source = "git::https://github.com/myorg/infra-modules.git//security-group?ref=v1.0.0"
}
Registry VPC module:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
cidr = "10.0.0.0/16"
}
Local EC2 module:
module "web" {
source = "./modules/ec2"
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
Pro Tips & Best Practices
- Always pin versions when using registry or Git modules.
- Document module sources for clarity.
- Use private registries for enterprise compliance.
- Test modules locally before publishing them.
- Prefer registry modules for common patterns; use Git/private for custom needs.
Summary & Cheatsheet
- Sources: Local, Registry, GitHub, Private.
- Syntax:
module "name" { source = "..." }. - Best practice: Pin versions, document sources, choose based on use case.
Quick mnemonic: Local, Registry, Git, Private = LRGP
The Hackers Notebook
Module sources are the supply chain of Terraform. They determine where your reusable building blocks come from local experiments, public registries, GitHub repos, or private enterprise libraries. By mastering sources, you gain flexibility to build infrastructure that’s both standardized and customized.
