Hey everyone! I'm looking for some help as I'm trying to restructure a Terraform monorepo for a project that has seen significant growth since I first set it up two years ago. Back then, the application was simple, with basic networking, a single RDS instance, and an API gateway – all hosted in one AWS account and managed through a monorepo with basic CI/CD.
Now, however, the setup has become much more complex, involving a load balancer, multiple containerized APIs in ECS, and various databases. The team wants to transition to a multi-account/environment setup while still maintaining the monorepo structure.
I'm trying to figure out how to break apart a large root state file into more manageable pieces so that the ECS-based API services can be updated and deployed independently, without affecting production. My current plan is to adopt a directory-per-environment strategy and modularize the services to parameterize their inputs. This way, each service would have its own root-level state file for more granular deployment without major overhauls. Also, I'm concerned about maintaining synchronization across environments, as tagging modules can be tricky in a monorepo setup. Any advice on this would be greatly appreciated!
3 Answers
Your approach sounds solid! A directory-per-environment setup along with modular design is definitely the way to go. Just remember not to overhaul everything at once; start with the most frequently changed components like ECS services. That's what really helped me during a similar migration. One tip: keep your shared infrastructure, like VPC and RDS, in a 'foundation' state to avoid duplication across services. This will save you headaches later on!
Honestly, I'd avoid splitting state files if you can help it. It can lead to more maintenance headaches later. Instead, you could stick to one repository for all environments and use different variable files for each stage. It keeps everything consistent and reduces code duplication.
True! Less code means less to manage, but just be careful about ensuring that the environments don't diverge too much over time. You definitely don't want to have unexpected differences in behavior!
I hear you on the monorepo struggles, especially with tagging! Instead of trying to manage everything manually, I suggest setting up automated staging deployments on merge while keeping production deployments manual with approval. This way, you can enforce deployment order and keep drift to a minimum.

Right, and using Terraform state mv to shift resources gradually can help you keep production untouched while you refactor. Just ensure you take snapshots and have a rollback plan ready for when you migrate!