r/Terraform • • 23d ago

Tutorial How We Use Terramate for Code Generation to Manage 500+ Terraform Stacks in a Monorepo

I wanted to share an article I wrote about structuring a Terraform monorepo with Terramate

https://oponomarov.com/blog/posts/structuring-a-terraform-monorepo-with-terramate/

I feel like Terramate is still relatively unknown, and even fewer people realize that you can use it purely for code generation, without adopting its other features.

That’s exactly how we use it to manage and template 500+ distinct Terraform stacks across different regions, environments, and other dimensions.

This is the first post in a six-part series where I share our experience using Terramate to manage a large-scale Terraform monorepo in practice.

If you’ve ever struggled with organizing hundreds of Terraform stacks, managing configuration across multiple dimensions, or figuring out how to keep everything maintainable as your infrastructure grows, you might find some useful ideas in the series.

Hopefully, it helps someone facing similar challenges.

0 Upvotes

11 comments sorted by

13

u/nunciate 23d ago

The usual layout for multi-account Terraform is a directory per environment with the same files copied into each: development/, staging/ and production/

i'm not sure everyone agrees on this. i've always despised this layout.

4

u/chesser45 23d ago

I’m an environment subdirectory tfvars enjoyer.

All files copied sounds pretty WET to me.

2

u/Zenin 23d ago

Me too. <workspace>.tfvars.json for me with a tiny Makefile wrapper to ensure alignment between tf workspace and tfvars file. -God I wish this was an automatic feature...I should write a PR for tofu. When environments must differ I strongly prefer flags as variables ie var.enable_foo than a massive copy/paste problem that gets "solved" by template systems doing massive copy/paste for you.

But I also keep my environments much more closely aligned than I believe many do. If my dev, staging, prod stacks varied far too wildly to sanely manage them by tfvars alone, I'd probably consider the dir per env model as well.

1

u/iamichi 22d ago

It blows my mind that HashiCorp ever recommended it instead of just adding Terragrunt / Terramate like functionality to Terraform.

I like my code DRY, which is why I prefer Terragrunt over Terramate.

Generated code just means noisier, cluttered PRs, more merge conflicts, and a CI check you can't skip for stale output.

And with Terragrunt I can pass outputs between units with dependency blocks. But to each their own.

1

u/InvincibearREAL 23d ago

If only there was a native feature built into Terraform to handle this scenario, something like workspaces....

1

u/Mrbucket101 23d ago

Am I missing something? We just create modules, and deploy those modules to the various accounts as needed. `variables.tf` for config/customization

Each module has its own repo/ci, and we manage/deploy with semver.

2

u/MongooseWonderful198 23d ago

The article still uses normal modules in main.tf. Terramate generates the backend and provider wiring around them. Terraform's backend block can't use input variables, so variables.tf doesn't cover that part.

If your CI already handles backend configuration and account/region selection, you may have little reason to add another tool. That wiring is the part I'd compare with your current setup.

2

u/Zenin 22d ago

Terraform's backend block can't use input variables, so variables.tf doesn't cover that part.

Yes, but tofu can.

We're cutting more and more over to tofu every day. Nobody has time for Hasicorp's nonsense.

1

u/Zenin 22d ago

File numbering:

I realize this is personal convention, but to be brutally honest it's a dreadful convention. Giving magic meaning to a pile of otherwise random digits does not demonstrate good instincts. Protocols like HTTP use code ranges because code is the primary consumer. The primary consumer here are humans; mnemonic identifiers should be strongly preferred.

Target config in code:

I'm looking at the example stacks/aws/acme-development/eu-west-1/acm/main.tf which looks like a code file, but in fact it's pure environment target config: domain_name = "api.example.com". This while you already have a convention of configurations placed in *.tm.hcl files. I'm left wondering, why not this?

globals {
  domain_name = "api.example.com"
}

This also gives you an opportunity to use Terramate assertions to validate these values so someone doesn't try and pass in "the_api.example.com" for example.

Stack anatomy:

Is mixing hand-authored code with generated code in the same tree the best choice, rather than some form of output/ tree to draw a reliable line between source artifacts and derived artifacts? Prone to human error, prone to scripted error, prone to agent error. Is it chosen because you're committing the generated files to source control anyway?

stacks/aws/acme-development/eu-west-1/dns/

You have backend and provider config generated into here, which I assume means this is an entire stack/state. Is the recommendation to narrow stacks/state down to a single DNS configuration like this?

My own small stacks regularly are tracking a few hundred resources across dozens of resource types that all have natural interdependencies. At a glance it feels like such a model would quickly translate into an unwieldy mass of state and a whole cloth reinvention of dependency tracking that I get for free today from Terraform's plan graph?

Limitations:

You correctly callout multi-account (and implied multi-region) stacks as needing to break the pattern and pick a home and backend location, but I'd expand this and call it out louder: This essay is very AWS specific and it's increasingly common to have multiple service providers in a given stack. Okta, Github, Docker, Datadog, etc over the usual AWS/Google/Azure/OCI hyperscalers.

Also thoughts on if/how IaC before and after terraform IaC wire in, such as packer (pre) or helm (post). I'm concerned in particular the model described seems to make it a bit hostile to inputting variables coming from pre-terraform stages and outputting values to post-terraform stages. For example, with stacks/state so heavily sliced up is a downstream stage required to lookup a dozens of states to collect the output values it needs?

Overall:

I'm always looking for reasons to like these style of tools (Terramate, Terragrunt, etc) and find value in these layouts, etc that are advocated, but it's difficult. I see a lot of complexity and WET that's reframed as DRY due to opaque code generators, all of which seem to be reinventing tfvars, workspaces, or both and not for the better? I'll grant this isn't a you problem, it's a community debate, you just happened to post a blog about parts of it which makes your work a useful vehicle to consider these questions.

1

u/No-Magazine2625 13d ago

At 500 plus stacks, the interesting part is less the code generation itself and more how you keep generated configuration predictable, reviewable, and independently deployable. I would be curious how you handle versioning of the generated contract so a template change cannot silently create a fleet wide behavioral change.

0

u/CartoonistStriking62 22d ago

Cloudposse Atmos for the win!