Introduction
When adopting Infrastructure as Code (IaC) with Terraform, a key architectural decision is how to organise source code repositories. Teams must decide whether to store Terraform configuration files with the application source code (Monorepo) or maintain them in separate repositories (Polyrepo). There is no one-size-fits-all solution—the ideal structure depends on team responsibilities, deployment workflows, and how application and infrastructure changes are managed.
Scenario
Two common patterns highlight the choice:
- Serverless or Microservices Architecture (e.g., AWS Lambda):
Here, the function code and its infrastructure resources—such as permissions, triggers, and event sources—are tightly coupled. They usually need to be versioned and deployed together. - Traditional Server-Based Architecture:
In setups where a centralised infrastructure (like a cluster of EC2 instances or shared networking) is managed independently of application deployments, the infrastructure typically changes less frequently than the application code.
The guiding principle is to consider how often application and infrastructure code need to change in lockstep.
Recommendation
Choose a repository structure that reflects the rate of change and ownership of each component:
Architecture/Cadence | Repository Structure | Rationale |
Serverless / Microservices (Tightly Coupled) | Monorepo(Terraform with App Code) | Application and infrastructure form a single deployable unit and usually evolve together. Keeping them in one repository simplifies versioning and enables atomic changes. |
Traditional / Centralized Infrastructure (Loosely Coupled) | Polyrepo(Separate Repositories) | Core infrastructure is often managed by a platform team on a different schedule than application releases. Separation clarifies team responsibilities and reduces unnecessary dependencies. |
Ultimately, the decision is influenced more by team workflows and collaboration needs than by technical limitations.
Additional Information
Regardless of whether you adopt a Monorepo or Polyrepo, Terraform Modules remain an essential best practice:
- Encapsulation & Reuse: Modules encapsulate complex infrastructure logic, enabling reuse across multiple projects and environments.
- Decoupling: Applications can call centrally maintained modules—even from a third repository—ensuring consistent standards and reducing duplication.
The key is to align repository boundaries with rate of change and ownership, ensuring smooth deployments and minimal friction between development and operations teams.