6 min read A. Sulaiman

Terraform Modules for Multiple Environments

Terraform Base Modules for Multi-Environment Infrastructure

Terraform Base Modules for Multi-Environment Infrastructure

Background

In infrastructure management, a structured and consistent approach is essential to provision resources across various environments—such as development, staging, and production. To address this challenge effectively, Base Modules are introduced to improve reusability, maintainability, and scalability in cloud resource management on AWS (or other cloud platforms).

A Base Module serves as the foundational building block that encapsulates common infrastructure patterns and best practices, enabling teams to deploy consistent and secure environments with minimal configuration overhead.

Goals

This approach is designed to achieve the following objectives:

  • Consistency: Enforce standardized configurations across all environments.
  • Reusability: Eliminate duplication by defining infrastructure logic once and reusing it everywhere.
  • Auditability: Simplify tracking of configuration changes across environments through versioned modules.
  • Scalability: Support easy scaling or modification of infrastructure within each environment.
  • Readability: Maintain clear, concise, and well-structured Terraform code that is easy for teams to understand and maintain.
  • Isolation: Prevent resource or configuration conflicts between environments by design.

Architecture Overview

Alt text

Base Module

The Base Module is a reusable, parameterized Terraform module that defines common infrastructure components required across all environments. Typical resources include:

  • Networking (VPCs, subnets, route tables, NAT gateways)
  • Security (security groups, IAM roles, KMS keys)
  • Core services (S3 buckets, CloudWatch log groups, ECR repositories)

This module is designed to be environment-agnostic—it accepts input variables (e.g., env, cidr_block, instance_types) to adapt its behavior without duplicating code.

Resource per Environment

Each environment (development, staging, production) instantiates the same Base Module but with its own set of input variables defined in dedicated environment configurations (e.g., envs/dev/main.tf, envs/prod/main.tf).

This pattern ensures:
- Shared logic remains centralized and version-controlled.
- Environment-specific settings (e.g., instance sizes, tagging policies, feature flags) are cleanly isolated.
- Changes to the base infrastructure propagate safely across environments when intended.

Example Structure

Benefits in Practice

  • Reduced risk: Fewer configuration errors due to shared, tested module logic.
  • Faster onboarding: New environments can be spun up with minimal effort.
  • Simplified compliance: Security and tagging standards are codified in the base module.
  • Team alignment: Engineers work from the same blueprint, improving collaboration.

Getting Started

  • Define your Base Module in modules/base/.
  • Parameterize all environment-specific values using variables.
  • Create a directory per environment under envs/.
  • Instantiate the Base Module in each environment with appropriate inputs.
  • Apply with standard Terraform workflow (init, plan, apply).