r/Terraform • u/shmileee • 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.
13
u/nunciate 23d ago
i'm not sure everyone agrees on this. i've always despised this layout.
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.
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.tfwhich 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?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!