Back to Blog

When to Create Terraform Modules

Published: Jan 10, 2025 by Joe Hernandez
TerraformInfrastructure as CodeDevOpsBest Practices

Terraform modules are great for organizing and reusing code, but let's be honest, sometimes they just add more work than they're worth. While they can help standardize infrastructure and enforce best practices, they can also slow things down, create unnecessary abstraction, and make debugging a nightmare. So, when should you use Terraform modules, and when should you skip them? Let's break it down.

When Terraform Modules Make Sense

1. You Need Reusable Code Across Multiple Environments

If you're deploying the same infrastructure across dev, staging, and prod, a module can save you time and reduce copy-pasting. It helps ensure consistency across environments.

Example use cases:

2. You’re Managing a Complex Set of Resources

When a resource has multiple dependencies, settings, or complex interconnections, a module makes it easier to manage and reuse without cluttering your main Terraform files.

Example use cases:

3. You Want to Enforce Best Practices and Security Standards

Modules can help enforce compliance by ensuring all deployments follow security guidelines, naming conventions, and configurations.

Example use cases:

4. You Want to Minimize Human Error

Using modules can help prevent misconfigurations, especially when multiple engineers are working on the same infrastructure. It ensures things are deployed the same way every time.

Example use cases:

When Terraform Modules Might Not Be Worth It

1. You’re Just Deploying a Simple, One-Off Resource

If you only need a single resource or a couple of related resources, a module might just add extra files and complexity without providing much benefit.

Example:

resource "azurerm_storage_account" "example" {
  name                     = "examplestorage"
  resource_group_name      = "example-rg"
  location                 = "eastus"
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

If all you need is a single storage account, wrapping it in a module is overkill.

2. Creating the Module Takes Longer Than Just Writing the Resource

If you find yourself spending hours structuring a module when you could deploy the resource in minutes, it’s probably not worth it.

Example:

3. The Module Creates More Code Than Necessary

Modules often require extra files (main.tf, variables.tf, outputs.tf), and for small resources, they might end up being more verbose than just defining the resource directly.

Example:

module "small_ns_group" {
  source  = "./modules/network_security_group"
  name    = "example-nsg"
  rules   = [
    { name = "AllowSSH", priority = 100, direction = "Inbound", access = "Allow", protocol = "Tcp", source_port_range = "*", destination_port_range = "22" }
  ]
}

For something as small as a security group with a few rules, this extra layer of abstraction might be unnecessary.

4. Debugging Becomes a Headache

Modules introduce abstraction, which means when something breaks, you have to dig through multiple files to figure out what went wrong.

Example:

Final Thoughts

Terraform modules are powerful, but they're not always necessary. If a module makes your infrastructure more reusable, easier to manage, and enforces best practices, go for it. But if it adds unnecessary complexity, creates more code, or takes longer than just defining the resource, then keep it simple.

A good rule of thumb: Use modules when they simplify your work, not when they complicate it.

Share this post