---
title: "Organizing Terraform with Modules"
lang: "en"
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/post/organizing-terraform-modules
---

![Blog post image for Organizing Terraform with Modules - How to split Terraform code into reusable modules for networking, databases, and other common components, stored in a shared repository so multiple projects and environments can pull from the same source instead of copy-pasted configuration.](/_astro/hero.5dVJEd3X_Df2Tx.webp)

[Home](/)›[Devtips](/devtips)›[All Categories](/devtips/categories)›[Cloud & Infrastructure Automation](/devtips/categories/cloud--infrastructure-automation)

Devtips

[Prev in Cloud & Infrastructure AutomationHashiCorp Pulls the Plug on CDKTF](/devtips/post/cdktf-deprecation-hashicorp-terraform)[Next in Cloud & Infrastructure AutomationManaging Terraform at Scale with Terragrunt](/devtips/post/terraform-terragrunt-wrappers)

[Cloud & Infrastructure Automation](/devtips/categories/cloud--infrastructure-automation)

# Organizing Terraform with Modules

[Mohammad Abu Mattar](/authors/mohammad-abu-mattar)Published: 16 Jun 202503 Mins read04 Mins listen

[Markdown for AI(opens in a new tab)](/post/organizing-terraform-modules/index.md "Open the plain-Markdown version of this page, for pasting into an AI tool")

TL;DR

How to split Terraform code into reusable modules for networking, databases, and other common components, stored in a shared repository so multiple projects and environments can pull from the same source instead of copy-pasted configuration.

Series

[Mastering Terraform](/series/mastering-terraform)1/4

[NextHashiCorp Pulls the Plug on CDKTF](/devtips/post/cdktf-deprecation-hashicorp-terraform)

All posts in this series (4)

DevTips4

1.  [Organizing Terraform with ModulesYou are here](/devtips/post/organizing-terraform-modules)
2.  [HashiCorp Pulls the Plug on CDKTF](/devtips/post/cdktf-deprecation-hashicorp-terraform)
3.  [Managing Terraform at Scale with Terragrunt](/devtips/post/terraform-terragrunt-wrappers)
4.  [Terraform Workspaces vs. Directory-Based Environments: What Actually Scales](/devtips/post/terraform-workspaces-vs-directory-environments)

### Organizing Terraform with Modules

Contents

[Why organize your Terraform code?](#why-organize-your-terraform-code)[Where the complexity comes from](#where-the-complexity-comes-from)[What structure buys you](#what-structure-buys-you)[The problem with messy Terraform code](#the-problem-with-messy-terraform-code)[Duplicated code](#duplicated-code)[Maintenance pain](#maintenance-pain)[Team collaboration problems](#team-collaboration-problems)[The fix: Terraform modules](#the-fix-terraform-modules)[How a module is structured](#how-a-module-is-structured)[Reusing a module](#reusing-a-module)[Version control and sharing](#version-control-and-sharing)[Module concepts](#module-concepts)[Breaking infrastructure into modules](#breaking-infrastructure-into-modules)[Input variables](#input-variables)[Module dependencies](#module-dependencies)[What modules give you](#what-modules-give-you)[Code quality](#code-quality)[Faster development](#faster-development)[Team productivity](#team-productivity)[What's your approach?](#whats-your-approach)[Community insights](#community-insights)[Sharing what works](#sharing-what-works)

## [Why organize your Terraform code?](#why-organize-your-terraform-code)

### [Where the complexity comes from](#where-the-complexity-comes-from)

If you’re using Terraform to build out your infrastructure, you know how quickly things get complicated. Every new environment, every new account, every new team wants a slightly different version of the same VPC. That’s where modules come in. Think of them as the thing that keeps your code reusable instead of copied.

### [What structure buys you](#what-structure-buys-you)

A module gives one VPC, one RDS instance or one IAM setup a single definition that every environment calls. When the definition changes, you change it once and bump a version tag instead of hunting through five directories.

## [The problem with messy Terraform code](#the-problem-with-messy-terraform-code)

### [Duplicated code](#duplicated-code)

As your infrastructure gets bigger, your Terraform files turn into a real tangle. You copy and paste code, you end up with files nobody wants to open, and you lose track of which copy is current. Updates become a headache, and your whole team slows down with you.

### [Maintenance pain](#maintenance-pain)

Large, monolithic Terraform files are hard to read, hard to test and hard to change. A tweak to a security group in one area can break an unrelated part of your infrastructure, and `terraform plan` is the first place you find out.

### [Team collaboration problems](#team-collaboration-problems)

When several people work on the same large files, merge conflicts become frequent, and resolving a conflict in HCL that describes live infrastructure is exactly the kind of merge you don’t want to get wrong.

## [The fix: Terraform modules](#the-fix-terraform-modules)

### [How a module is structured](#how-a-module-is-structured)

Imagine modules as ready-to-go blueprints for parts of your infrastructure. You could create a module for a standard network setup or a database configuration. Then you reuse that blueprint across different projects or environments. Store these modules in a shared place, like a Git repository, so your team can call them up with a few lines of code whenever needed.

### [Reusing a module](#reusing-a-module)

Once a module exists, every project and environment calls the same source. Each caller passes its own variables, so dev and prod share the definition without sharing the sizing.

### [Version control and sharing](#version-control-and-sharing)

Pin the module source to a tag rather than a branch. A shared repository means everyone on the team can call the module with a few lines of code, and pinning means a change to `main` doesn’t reach production until someone chooses it.

## [Module concepts](#module-concepts)

### [Breaking infrastructure into modules](#breaking-infrastructure-into-modules)

-   Split your Terraform code into modules for the pieces you build more than once.
-   Put those modules in a shared repository so everyone on the team can reach them.
-   Use input variables to customize how a module behaves for different situations.

### [Input variables](#input-variables)

Input variables are the only knobs a caller gets. Expose instance sizes, CIDR ranges and counts. Keep naming conventions and tagging inside the module, so callers can’t drift from them by accident.

### [Module dependencies](#module-dependencies)

Modules can reference each other’s outputs, and that’s how a network module feeds subnet IDs to a database module. It’s also how you get an ordering problem, so keep the dependency chain shallow enough to explain out loud.

## [What modules give you](#what-modules-give-you)

### [Code quality](#code-quality)

Modules stop you writing the same configuration over and over. Fewer copies means fewer mistakes, and it means a review of one module covers every place that module is used.

### [Faster development](#faster-development)

Reusable modules mean less time on boilerplate. Standing up a new environment turns into filling in variables rather than writing HCL from scratch.

### [Team productivity](#team-productivity)

Everyone works from the same definitions, so a discussion about the network is a discussion about one module instead of five copies that have quietly diverged.

## [What’s your approach?](#whats-your-approach)

### [Community insights](#community-insights)

How do you like to keep your Terraform projects organized? Any tips or tricks you’ve picked up along the way?

### [Sharing what works](#sharing-what-works)

Whether you’re using the Terraform Registry, Git submodules or a private registry, the trade-offs are different, and hearing how you landed on yours is useful to the rest of us.

Was this useful?

## Tags

[#Terraform](/devtips/tags/terraform)[#Infrastructure as Code](/devtips/tags/infrastructure-as-code)[#Modules](/devtips/tags/modules)[#Cloud Automation](/devtips/tags/cloud-automation)[#DevOps](/devtips/tags/devops)[#IaC](/devtips/tags/iac)[#Cloud Infrastructure](/devtips/tags/cloud-infrastructure)

## Share

[Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Fmkabumattar.com%2Fdevtips%2Fpost%2Forganizing-terraform-modules "Share on Facebook")[Twitter](https://twitter.com/intent/tweet/?text=Organizing%20Terraform%20with%20Modules&url=https%3A%2F%2Fmkabumattar.com%2Fdevtips%2Fpost%2Forganizing-terraform-modules "Share on Twitter")[LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fmkabumattar.com%2Fdevtips%2Fpost%2Forganizing-terraform-modules&title=Organizing%20Terraform%20with%20Modules&summary=How%20to%20split%20Terraform%20code%20into%20reusable%20modules%20for%20networking%2C%20databases%2C%20and%20other%20common%20components%2C%20stored%20in%20a%20shared%20repository%20so%20multiple%20projects%20and%20environments%20can%20pull%20from%20the%20same%20source%20instead%20of%20copy-pasted%20configuration.&source=https://mkabumattar.com "Share on LinkedIn")[WhatsApp](https://wa.me/?text=Organizing%20Terraform%20with%20Modules%20https%3A%2F%2Fmkabumattar.com%2Fdevtips%2Fpost%2Forganizing-terraform-modules "Share on WhatsApp")[Telegram](https://t.me/share/url?url=https%3A%2F%2Fmkabumattar.com%2Fdevtips%2Fpost%2Forganizing-terraform-modules&text=Organizing%20Terraform%20with%20Modules "Share on Telegram")[Reddit](https://www.reddit.com/submit?url=https%3A%2F%2Fmkabumattar.com%2Fdevtips%2Fpost%2Forganizing-terraform-modules&title=Organizing%20Terraform%20with%20Modules "Share on Reddit")[Hacker News](http://news.ycombinator.com/submitlink?u=https%3A%2F%2Fmkabumattar.com%2Fdevtips%2Fpost%2Forganizing-terraform-modules&t=Organizing%20Terraform%20with%20Modules "Share on Hacker News")[Pinterest](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fmkabumattar.com%2Fdevtips%2Fpost%2Forganizing-terraform-modules&media=&description=How%20to%20split%20Terraform%20code%20into%20reusable%20modules%20for%20networking%2C%20databases%2C%20and%20other%20common%20components%2C%20stored%20in%20a%20shared%20repository%20so%20multiple%20projects%20and%20environments%20can%20pull%20from%20the%20same%20source%20instead%20of%20copy-pasted%20configuration. "Share on Pinterest")[Email](<mailto:?subject=Organizing%20Terraform%20with%20Modules&body=Check out this article: https%3A%2F%2Fmkabumattar.com%2Fdevtips%2Fpost%2Forganizing-terraform-modules>)

## Comments

## You might also enjoy

More posts on similar topics

[![Managing Terraform at Scale with Terragrunt](/_astro/hero.DUZZoi07_ZRPUOh.webp)](/devtips/post/terraform-terragrunt-wrappers)

## [Managing Terraform at Scale with Terragrunt](/devtips/post/terraform-terragrunt-wrappers)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Cloud & Infrastructure Automation](/devtips/categories/cloud--infrastructure-automation)

The problem with Terraform at scale Duplicated code across environments If you're managing infrastructure with Terraform across several environments or projects, you've probably hit the point

[#Terraform](/devtips/tags/terraform)[#Terragrunt](/devtips/tags/terragrunt)[#Infrastructure as Code](/devtips/tags/infrastructure-as-code)+4 tags

[read more](/devtips/post/terraform-terragrunt-wrappers)

[![HashiCorp Pulls the Plug on CDKTF](/_astro/hero.BBIsBB2t_Z22hNwP.webp)](/devtips/post/cdktf-deprecation-hashicorp-terraform)

## [HashiCorp Pulls the Plug on CDKTF](/devtips/post/cdktf-deprecation-hashicorp-terraform)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Cloud & Infrastructure Automation](/devtips/categories/cloud--infrastructure-automation)

CDKTF is officially deprecated The deprecation announcement Well, it finally happened. HashiCorp (now owned by IBM) officially deprecated the Cloud Development Kit for Terraform (CDKTF)

[#Terraform](/devtips/tags/terraform)[#CDKTF](/devtips/tags/cdktf)[#HashiCorp](/devtips/tags/hashicorp)+6 tags

[read more](/devtips/post/cdktf-deprecation-hashicorp-terraform)

[![Terraform Workspaces vs. Directory-Based Environments: What Actually Scales](/_astro/hero.utyGomxG_Z2cpb8C.webp)](/devtips/post/terraform-workspaces-vs-directory-environments)

## [Terraform Workspaces vs. Directory-Based Environments: What Actually Scales](/devtips/post/terraform-workspaces-vs-directory-environments)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Cloud & Infrastructure Automation](/devtips/categories/cloud--infrastructure-automation)

Why this choice matters Hey, want to stop sweating every prod apply? The way you split dev, staging, and prod in Terraform decides how much damage a single mistake can do. Get it right and a

[#Terraform](/devtips/tags/terraform)[#Workspaces](/devtips/tags/workspaces)[#Environments](/devtips/tags/environments)+3 tags

[read more](/devtips/post/terraform-workspaces-vs-directory-environments)

[![Securing CI/CD with IAM Roles](/_astro/hero.Bl9B2DZz_ZDIuXQ.webp)](/devtips/post/securing-cicd-with-iam-roles)

## [Securing CI/CD with IAM Roles](/devtips/post/securing-cicd-with-iam-roles)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [DevOps & DevSecOps](/devtips/categories/devops--devsecops)

Why secure your CI/CD pipeline? Why pipeline security matters Your pipeline holds credentials for every environment you deploy to, which makes it one of the most valuable targets you own. A s

[#CICD Security](/devtips/tags/cicd-security)[#IAM Roles](/devtips/tags/iam-roles)[#Least Privilege](/devtips/tags/least-privilege)+4 tags

[read more](/devtips/post/securing-cicd-with-iam-roles)

[![Tracing Microservices with OpenTelemetry](/_astro/hero.BOHz8WyH_Z1IEpz3.webp)](/devtips/post/tracing-microservices-opentelemetry)

## [Tracing Microservices with OpenTelemetry](/devtips/post/tracing-microservices-opentelemetry)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Observability & Monitoring](/devtips/categories/observability--monitoring)

Why monitor your microservices? The complexity of distributed systems If you're juggling multiple services, it's hard to track how they work together. OpenTelemetry lets you follow one reques

[#OpenTelemetry](/devtips/tags/opentelemetry)[#Microservices](/devtips/tags/microservices)[#Distributed Tracing](/devtips/tags/distributed-tracing)+4 tags

[read more](/devtips/post/tracing-microservices-opentelemetry)

[![Policy-as-Code Governance with OPA/Rego](/_astro/hero.CwAJ64Mi_1WeH9p.webp)](/devtips/post/policy-as-code-opa-rego)

## [Policy-as-Code Governance with OPA/Rego](/devtips/post/policy-as-code-opa-rego)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [DevOps & DevSecOps](/devtips/categories/devops--devsecops)

Why policy-as-code matters The governance problem Managing infrastructure at scale gets complicated fast. As your infrastructure grows, keeping it consistent and compliant gets harder. M

[#Policy as Code](/devtips/tags/policy-as-code)[#OPA/Rego](/devtips/tags/oparego)[#Compliance](/devtips/tags/compliance)+4 tags

[read more](/devtips/post/policy-as-code-opa-rego)

6 related posts
