1. Principle #
Reusable building blocks.
Structure #
module "reseau" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = "prod"
cidr = "10.0.0.0/16"
azs = ["eu-west-3a", "eu-west-3b"]
private_subnets = ["10.0.11.0/24", "10.0.12.0/24"]
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
}
module "app" {
source = "./modules/app"
subnet_ids = module.reseau.private_subnets
}2. Sources and versions #
Where modules come from.
Sources #
| Source | Example |
|---|---|
| Local folder | ./modules/app |
| Public registry | terraform-aws-modules/vpc/aws + version |
| Git | git::https://git.exemple.fr/modules/app.git?ref=v1.4.0 |
| Private registry | app.terraform.io/acme/app/aws |
Rule #
Good practice: Always pin a version (or a Git tag): a module that changes under your feet makes plans unpredictable.
3. Designing well #
Useful modules.
Rules #
- One module = one responsibility (network, database, application), not a mere wrapper around a resource.
- Typed, documented variables, with validations and safe defaults.
- Outputs for everything other modules need.
- Examples and tests (terraform test) in the module repository.
- Semantic versioning and a changelog.
