Skip to content

Modules Creation

Terraform modules are reusable units of infrastructure code that promote consistency, reduce duplication, and simplify complex configurations. By encapsulating resources, variables, and outputs into modular components, teams can streamline deployment workflows and maintain scalable infrastructure. This section guides you through creating, structuring, and reusing Terraform modules effectively.


Creating Terraform Modules

A module is a directory containing Terraform configuration files that define resources, variables, and outputs. To create a module, follow these steps:

1. Define Input Variables

Input variables allow users to customize a module’s behavior. Define them in a variables.tf file:

# variables.tf
variable "bucket_name" {
  description = "Name of the S3 bucket"
  type        = string
  default     = "example-bucket"
}

variable "region" {
  description = "AWS region for bucket creation"
  type        = string
  default     = "us-west-1"
}

2. Configure Resources

Create resources in a main.tf file, using variables for dynamic configuration:

# main.tf
resource "aws_s3_bucket" "example" {
  bucket = var.bucket_name
  region  = var.region
}

3. Define Output Values

Expose useful values from the module via outputs.tf:

# outputs.tf
output "bucket_arn" {
  description = "ARN of the created S3 bucket"
  value       = aws_s3_bucket.example.arn
}

4. Structure the Module Directory

Organize your module into a directory, e.g., modules/s3-bucket, containing:

modules/s3-bucket/
├── variables.tf
├── main.tf
└── outputs.tf


Reusing Terraform Modules

To reuse a module, call it in a parent configuration using the module block. For example:

1. Call the Module in main.tf

# main.tf
module "s3_bucket" {
  source = "./modules/s3-bucket"

  bucket_name = "production-bucket"
  region      = "us-east-1"
}

2. Access Module Outputs

Reference outputs from the module in the parent configuration:

output "s3_bucket_arn" {
  value = module.s3_bucket.bucket_arn
}

3. Pass Variables via CLI

Use the CLI to override default variables:

terraform apply -var="bucket_name=my-bucket" -var="region=eu-central-1"


Best Practices for Reusability

  1. Use Semantic Versioning
    Tag modules with version numbers (e.g., v1.0.0) to ensure compatibility across environments.

  2. Document Variables and Outputs
    Include clear descriptions in variables.tf and outputs.tf to guide users.

  3. Test in Isolation
    Validate modules independently using terraform validate and terraform plan before integrating them into larger configurations.

  4. Avoid Hardcoding Sensitive Data
    Use environment variables or secure secret management tools instead of embedding credentials in modules.

  5. Leverage Remote Modules
    For shared modules, publish them to a registry (e.g., Terraform Registry) and reference them via source = "hashicorp/aws".


Key takeaways

  • Modularize infrastructure to reduce duplication and improve maintainability.
  • Use variables for flexible configuration and outputs to share values between modules.
  • Version modules to ensure stability and compatibility across deployments.
  • Document thoroughly to enable seamless reuse by other teams or projects.
  • Test and validate modules in isolation before integrating them into production workflows.