Terraform Module Library

Production-ready Terraform module patterns for AWS, Azure, GCP, and OCI infrastructure

Terraform Module Library

What Is This?

The Terraform Module Library skill provides a structured approach to designing, building, and maintaining production-ready Terraform modules for AWS, Azure, GCP, and OCI infrastructure. It is designed to help DevOps engineers and cloud architects develop reusable Infrastructure as Code (IaC) components that follow best practices across multiple cloud providers. This skill outlines standard patterns for module creation, including directory structure, input and output definitions, documentation, usage examples, and automated testing. By leveraging this skill, organizations can accelerate cloud adoption, enforce consistency, and reduce operational overhead when managing cloud resources.

Why Use It?

Building cloud infrastructure manually or using ad-hoc Terraform scripts can quickly become unmanageable as environments scale. The Terraform Module Library skill addresses this challenge by ensuring that all infrastructure components are developed as reusable modules, each adhering to strict conventions and documentation standards. The benefits of using this skill include:

  • Reusability: Modules can be shared across projects and teams, reducing duplication of effort.
  • Consistency: Standardized patterns ensure cloud resources are provisioned in a uniform way.
  • Scalability: Modular code is easier to maintain, extend, and test, enabling infrastructure to scale with business needs.
  • Multi-Cloud Support: Modules are organized by provider, making it straightforward to support AWS, Azure, GCP, and OCI within the same repository.
  • Best Practices: Each module includes version pinning, documentation, and automated tests, reducing the risk of misconfiguration or drift.

How to Use It

To use the Terraform Module Library skill, begin by cloning or referencing a repository structured as outlined in the skill documentation. The repository typically has the following hierarchy:

terraform-modules/
├── aws/
│   ├── vpc/
│   ├── eks/
│   ├── rds/
│   └── s3/
├── azure/
│   ├── vnet/
│   ├── aks/
│   └── storage/
├── gcp/
│   ├── vpc/
│   ├── gke/
│   └── cloud-sql/
└── oci/
    ├── vcn/
    ├── oke/
    └── object-storage/

Each module follows a standard pattern:

module-name/
├── main.tf          # Main resources
├── variables.tf     # Input variables
├── outputs.tf       # Output values
├── versions.tf      # Provider versions
├── README.md        # Documentation
├── examples/        # Usage examples
│   └── complete/
│       ├── main.tf
│       └── variables.tf
└── tests/           # Terratest files
    └── module_test.go

Example:

AWS VPC Module

A typical usage example for an AWS VPC module might look like this:

examples/complete/main.tf

module "vpc" {
  source = "../../aws/vpc"
  name   = "my-vpc"
  cidr_block = "10.0.0.0/16"
  public_subnets  = ["10.0.1.0/24", "10.0.2.0/24"]
  private_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
  enable_nat_gateway = true
}

variables.tf (in the module)

variable "name" {
  description = "Name of the VPC"
  type        = string
}

variable "cidr_block" {
  description = "CIDR block for the VPC"
  type        = string
}

variable "public_subnets" {
  description = "List of public subnet CIDR blocks"
  type        = list(string)
}

variable "private_subnets" {
  description = "List of private subnet CIDR blocks"
  type        = list(string)
}

variable "enable_nat_gateway" {
  description = "Enable NAT Gateway"
  type        = bool
  default     = false
}

outputs.tf

output "vpc_id" {
  description = "The ID of the VPC"
  value       = aws_vpc.main.id
}

README.md

## AWS VPC Module

Deploy a production-ready VPC with public and private subnets, NAT gateways, and Internet gateway support.

### Usage

module "vpc" {
  source = "../../aws/vpc"
  ...
}

Testing

Each module is expected to include automated tests, typically using Terratest, ensuring that infrastructure code behaves as intended.

When to Use It

Use the Terraform Module Library skill in the following scenarios:

  • When building reusable infrastructure components: Accelerate new project onboarding by reusing tested modules for networks, compute, storage, and databases.
  • To standardize cloud resource provisioning: Enforce organizational policies and architectural standards for all cloud deployments.
  • When adopting infrastructure as code best practices: Replace hand-crafted scripts with modular, version-controlled code.
  • For multi-cloud compatibility: Abstract common patterns across AWS, Azure, GCP, and OCI, supporting hybrid and multi-cloud strategies.
  • To establish organizational Terraform standards: Ensure all teams use the same module patterns for consistency and maintainability.

Important Notes

  • Version Control: Always pin provider and module versions in versions.tf to ensure reproducible builds and guard against breaking changes.
  • Documentation: Every module must include a comprehensive README.md with usage, variables, outputs, and examples.
  • Testing: Automated tests are required for each module. Use Terratest or similar tools to validate resource creation, configuration, and outputs.
  • Inputs and Outputs: Clearly define all input variables and outputs. Use descriptive variable names and provide defaults where appropriate.
  • Separation by Provider: Maintain a clear directory structure, separating modules by cloud provider for clarity and ease of navigation.
  • Security: Avoid hardcoding sensitive values and ensure modules are compatible with secret management solutions.
  • Extensibility: Design modules to be composable and extensible, making it easy to add new features or support additional cloud services.

By adopting the Terraform Module Library skill, you will accelerate infrastructure delivery, improve code quality, and promote operational excellence in cloud environments.