YATSQ: Yet Another Terraform Structure Question

Discussion

I have been studying different IaC patterns for scalability, and I was curious if anyone has experimented with a similar concept or has any thoughts on this pattern? The ultimate goal is to isolate states, make it easier to scale, and not require introducing an abstraction layer like terragrunt. It comes down to three main pieces:

  1. Reusable modules for common resources (e.g., networking, WAF, EFS, etc.)
  2. Stacks as root modules (each with its own backend/state)
  3. Environment folders (staging, prod, etc.) referencing these stacks

An example layout would be:

└── terraform
    ├── stacks
    │   └── networking      # A root module for networking resources
    │       ├── main.tf
    │       ├── variables.tf
    │       └── outputs.tf
    ├── envs
    │   ├── staging         # Environment overlay
    │   │   └── main.tf
    │   └── prod            # Environment overlay
    │       └── main.tf
    └── modules
        └── networking      # Reusable module with the actual VPC, subnets, etc.
            ├── main.tf
            ├── variables.tf
            └── outputs.tf

Let's say stacks/networking/main.tf looked like:

  region = var.region
}

module "networking_module" {
  source           = "../../modules/networking"
  vpc_cidr         = var.vpc_cidr
  environment_name = var.environment_name
}

output "network_stack_vpc_id" {
  value = module.networking_module.vpc_id
}

And envs/staging/main.tf looked like:

provider "aws" {
  region = "us-east-1"
}

module "networking_stack" {
  source = "../../stacks/networking"

  region           = "us-east-1"
  vpc_cidr         = "10.0.0.0/16"
  environment_name = "staging"
}

# Reference other stacks here

I’m looking for honest insights. Has anyone tried this approach? What are your experiences, especially when it comes to handling cross-stack dependencies? Any alternative patterns you’d recommend? I'm researching different approaches for a blog article, but I have never been a fan of the tfvars approach.

Posted Apr 8, 2025, 9:11 PM UTC

45
Loading comments…