Issue
A Terraform configuration managing Azure infrastructure combined networking (e.g., virtual networks, subnets) and application services (e.g., Kubernetes clusters, AI services) into a single, monolithic setup. This design introduced several challenges:
- Access Control Limitations: Platform teams could not independently manage networking from application teams.
- Deployment Bottlenecks: Changes to application services required redeploying the entire infrastructure stack.
- State Management Complexity: A single state file grew to 4,200 lines, causing slow deployment times (18+ minutes) and scalability issues.
- Environment Promotion Difficulties: Validating foundation changes without affecting applications was impractical.
The configuration included 120+ variables mixing infrastructure and application concerns, creating a tightly coupled and unmaintainable system.
Culprit
Architectural Debt
The initial design followed a pragmatic approach, consolidating all resources into:
- A single
main.tffile mixing networking and application definitions. - Interdependent modules where networking components referenced application resources.
- A single
terraform.tfvarsfile containing variables for both infrastructure and application layers.
Key Technical Problems
- Circular Dependencies: Networking modules (e.g., subnets for AKS) were defined in the same file as application resources, creating implicit coupling.
- State File Bloat: The single state file contained 4200+ lines, combining virtual networks, Kubernetes clusters, and AI service configurations.
- Variable Pollution: Variables for networking (e.g., CIDR blocks) and applications (e.g., node counts) were mixed, forcing teams to understand unrelated domains.
Solution
Two-Layer Architecture
The solution separated infrastructure into two distinct layers:
- Foundation Layer: Manages core networking components (e.g., virtual networks, subnets, NSGs) independently of applications.
- Application Layer: Manages application-specific resources (e.g., Kubernetes clusters, AI services) with explicit dependencies on the foundation layer.
Key Improvements
- State File Size: Reduced from 4.2MB to 1.8MB (foundation) + 2.1MB (application), a 48% reduction.
- Deployment Time: Foundation layer now deploys in 15 minutes; application layer in 12 minutes, reducing total deployment time.
- Variable Management: Foundation layer uses 23 variables; application layer uses 48, isolating concerns.
- Change Blast Radius: Foundation changes now require only 37% of the previous validation effort.
- Role-Based Access Control: Teams can manage layers independently.
Hybrid Environment Support
For teams requiring environment-specific configurations, the solution supports:
- Separate directories for dev and prod environments within each layer.
- Reusable foundation modules for networking components.
- Environment-specific variables for both foundation and application layers.
This redesign enables faster deployments, better access control, and scalable infrastructure management while maintaining environment parity.