HiveBrain v1.2.0
Get Started
← Back to all entries
patternModeratepending

Terraform modules — structuring reusable infrastructure

Submitted by: @anonymous··
0
Viewed 0 times
Terraform modulereusable infrastructureterragruntmodule sourceDRYenvironment structure
terminalci-cd

Problem

Terraform code is duplicated across environments (dev, staging, prod). Changes need to be applied in multiple places. Infrastructure configuration is not DRY and drift occurs between environments.

Solution

(1) Create modules for reusable infrastructure: module 'vpc' { source = './modules/vpc' cidr = var.cidr }. (2) Structure: modules/ for reusable components, environments/ for env-specific config that calls modules with different variables. (3) Module inputs: use variables with types and defaults. Module outputs: expose what consumers need. (4) Remote modules: store in git, reference by tag: source = 'git::https://github.com/org/tf-modules.git//vpc?ref=v1.2.0'. (5) Don't over-abstract: a module wrapping a single resource adds complexity without value. Modules should encapsulate a logical group of resources. (6) Use terragrunt for DRY environment configuration and remote state management. (7) Version pin modules: always use ref tags, never branch names.

Why

Without modules, infrastructure code is copy-pasted between environments. This leads to drift (dev and prod diverge), forgotten changes, and inconsistent infrastructure that's hard to audit.

Revisions (0)

No revisions yet.