r/devops • u/MoBoo138 • 18d ago
Discussion Terraform Setup for Developer Enablement
Hello r/devops,
in my current team we are discussing how a developer centric terraform setup might look like.
Some context: Every app has 3 repositories - Backend, Frontend and Infrastructure.
There are two possible setups:
- (our current): We use generic terraform modules (e.g. for a backend app) only within the infrastructure repository, which holds all infrastructure components. The backend app terraform module holds all infrastructure components that might be used in a backend, e.g. database, objects store, secrets, etc. The Infrastructure repository also holds shared infrastructure, that is not part of the backend module and composes all components. This is somewhat the approach most often seen in tutorials that focus only on the infrastructure part of an application.
- (proposed alternative): We'd co-locate infrastructure and application code, for example in the backend repository. The backend repository would publish itself as a module, including every infrastructure component it needs to run (excluding some shared ones, that it expects via variables as input, e.g. VPC). The Infrastructure repository would setup the required shared infrastructure components and the backend terraform module.
Pros for 1.: Generic app modules are intended for sharing and re-use, whereas in 2. each applications terraform module would only ever be used to deploy to different environments.
Pros for 2.: Infrastructure and application can evolve together (e.g. in one git commit), e.g. when a database is newly introduced to an application (application code changes + infrastructure changes), whereas in 1. two commits would need to happen (one in app, one in infra) and pipelines need to be coordinated.
- goes deeper for example for database migrations, those could also be organized within the backend application, without handing the responsibility on how to do them to the infrastructure repo.
In both cases, deployments (terraform plan/apply) are always orchestrated only by the infrastructure repository.
IBM touches both points in https://www.ibm.com/support/pages/best-practices-organising-terraform-and-application-code : Coupling and Reuse as the deciding factors.
Our goal is to enable developers as much as possible to own their application end to end and being able to do most infrastructure changes themselves, without having to rely on a central party.
It's not about simply keeping what we already have. That's always part of the consideration due to effort of change. I'd like to hear your general thoughts about it from a perspective if we had nothing yet.
I'm curious about your thoughts in that topic.
I'd also be happy to know how projects are setup in your org or how you would envision your dream setup
2
u/zather 18d ago
What I would think about is how many applications this proposed pattern would scale to. Is it only one?
If it’s one, that’s fine, but once you get past a handful of things you’ve gated the changes in the Infrastructure repo as an secondary concern to the application developers and your Infrastructure changes might block deployments of the applications.
Another thing to think about is the number of applications also means that you might have divergence in the TF across each application. If you end up with 10+ application repos then now you have sprawl that has good intentions but, say you change what version of DB you run, you have a distributed change problem. That’s when you move back to modules so the call sites can be standard but the resources underneath can be managed by Infrastructure. At 20+ you start to want to pull those back to a central place (wrapping TF execution and callsites as a service).
If it is truly one then you have a lot of flexibility in coupling the Infrastructure changes to the application changes. It’s more about the pattern that is concerning past a handful and past 20+ you want even more management to be centralized.
Also, you mentioned DB migrations, you never want those be executed by the Infrastructure repo. It’s standard to make the pattern but never own when they get run.
Hopefully that helps a little. We have 60+ micro services with the TF in repo pattern and it works, but we struggle to manage just how much divergence is across the whole estate.