RousselTMLEARNING

Article · Terraform: from discovery to expertise

Modules

1. Principle #

Reusable building blocks.

Structure #

Module composition
A module = a folder with variables.tf, main.tf, outputs.tf and a README.
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 #

SourceExample
Local folder./modules/app
Public registryterraform-aws-modules/vpc/aws + version
Gitgit::https://git.exemple.fr/modules/app.git?ref=v1.4.0
Private registryapp.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.

📄 Terraform modules: designing, versioning and reusing →

My progress

  • ⏱ Estimated time : ≈ 4 min

Sign in to track your progress and take notes. Sign in

⭐ Rate this lesson

5.0 initial rating from the teaching team

Sign in to rate this lesson →

💬 Questions about this lesson

No questions yet: be the first to ask for help or share your configuration.

Ask a question about this lesson →

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...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!