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.
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
Terraform multi-environment, workspaces and repository organization
Organize Terraform for several environments: one folder per environment, tfvars files, CLI worksp...
Preparing for the HashiCorp Certified: Terraform Associate certification
Preparation guide for the Terraform Associate: format, official objectives (IaC concepts, workflo...
Terraform best practices: structure, state, security, review and maintenance
Terraform best practices for teams: repository structure, naming, pinned versions, encrypted and ...
The HCL language: resources, variables, outputs, data and expressions
Writing Terraform: terraform, provider, resource and data blocks, typed variables with validation...
Terraform: principles, architecture and workflow
Understand Terraform: declarative infrastructure as code, architecture (core, providers, state), ...
Terraform state: remote backends, locking, import and refactoring
Manage Terraform state: the role of the state file, sensitive data, remote backends (S3, azurerm,...
Testing and securing Terraform: validate, terraform test, tflint, Checkov and policies
Ensure the quality of Terraform code: fmt and validate, native terraform test tests with mocked p...
Terraform in CI/CD: pipelines, plan review, OIDC and GitOps
Automate Terraform: plan pipeline on merge requests and apply after approval, posting the plan as...
Terraform across multiple clouds: AWS, Azure, Google Cloud, OVHcloud and Kubernetes
Use Terraform with the main providers: aws, azurerm, google, ovh and openstack, kubernetes and he...