Terraform Config Root Setups
resourcely.io
Terraform Config Root Setups
1–10 of 53 posts
Re: Terraform Config Root Setups
#2This gave myself a refresher on how they are organizing their cloud infrastructure within their source control systems. I took a lense from the world of terraform since that’s mostly the world i live in today and the last few years.
I explored 10 different ways to structure your Terraform config roots, each promising scalability but delivering varying degrees of chaos. From single-environment simplicity to multi-cloud madness, customers are stuck navigating spaghetti directories and state file hell.
I probably missed things. Might have gotten things wrong. Take a look and let me know what you think.
What patterns are you using that I missed?
Re: Terraform Config Root Setups
#3Re: Terraform Config Root Setups
#4Re: Terraform Config Root Setups
#5Re: Terraform Config Root Setups
#6> Multi-Environment Setup with Shared Modules
But the con of saying versioning is tricky across modules is damn near impossible to reliably manage.. especially because if I'm introducing a new variable to a shared module A) I need to also add this variable in the inputs of each of the environment.
I haven't found a way to manage multiple versions of the modules across environments if all using the same shared modules. Is it even possible?
Re: Terraform Config Root Setups
#7The method I go to almost always is > Multi-Environment Setup with Shared Modules But the con of saying versioning is tricky across modules is damn near impossible to reliably manage.. especially because if I'm introducing a new variable to a shared module A) I need to also add this variable in the inputs of each of the environment. I haven't found a way to manage multiple versions of the modules across environments…
Define a default which is backwards compatible.
Re: Terraform Config Root Setups
#8I am a big fan of modularisation, it is possible to extend this approach to divide logically your infrastructure and mirror that by separating out the terraform state files too.
Number of TF deployment increases but they each have smaller blast radiuses and you now need to manage making available outputs of builds to those which are dependent on them.
Re: Terraform Config Root Setups
#9Composable modules such as `terraform-aws-lambda/modules/standard-function`, `terraform-aws-iam/moduls/role-for-aws-lambda`, etc, which get composed for a specific usecase in a root module (which we call stacks). The stack has directories under it such as `dev/main/primary/` `dev/sandbox-a/primary/` `dev/sandbox-a/test-a/`, etc, where `dev` is environment, `main/sandbox-a` is tenant and the `primary/test-a` is the namespace. The namespaces contain a `tfvars` file and potentially some namespace specific assets, readme's, documentation, etc. The CD system then deploys the root module for each namespace present.
Stacks are then optionally (sometimes deeply) nested under parent directories, which are used for change control purposes, variable inference and consistency testing.
OpenTofu >1.8.0 is required for all of this to keep it nice and tidy.
Re: Terraform Config Root Setups
#10 - root
-- /envs
--- / dev.tfvars
--- / prod.tfvars
- main.tf
When it gets deployed by the CICD, the right tfvars file is passed in via the -var-file parameter. A standard `env` var is also passed in, and used as a basis for a naming convention. Backend is also set by the pipeline.The rationale here is that our environments should be almost the same between them, and any variations should be accomplished by parameterization.
Modules are kept either in separate repos, if they need to be shared between many workspaces, or under the `modules` subfolder.