Refactoring your codebase terraform is a necessity within the lifecycle of your codebase. As your infrastructure grows and as you add collaborators to the project, you’ll need to reconsider things like :
To keep track of your continuous changes, you should set up quality probes, which will serve as goals the refactored codebase aims to reach like :
Your priorities regarding a refactoring are the following in this order :
Less than 2 aka you and a partner.
You should frequently work in pair and communicate on the refacto. You must review each others PR, that way both of you will have the entire knowledge of what’s merged into the codebase.
3 collaborators and more aka a team.
What’s risky with a team refactoring a codebase is poor quality code merged into the codebase due to a loss in information transmission. To control code quality over time during the refactoring you need to proceed as follows :
Permit yourself to edit your legacy codebase only if you do not create
resource
bloc. It’s OK if you need like a new buckets which are created looping over a list of string. If you need to copy paste theresource
bloc or write a new one -> do it in the new codebase.And when you do, don’t take legacy codebase in example
That way you’ll have a clear view on your refactored code. Also this pin in time your codebase state, not your infrastructure state. This tag is meant to track modifications not to be used again to apply.
The goal is to find the right balance between macro layers (too few layers) and micro layers (too many layers).
Macro layers (too few, ideally avoid having only one terraform state with every resource in it) create problems such as:
Micro layers (too many) create different problems such as:
To know how to define the scope of balanced layers, you can ask yourself how to dispatch resources in 3 states or also in 3 folders. You can rely on the 3-tier way of splitting an architecture. If you realize while refactoring that terraform plans takes too long (more than 1 minute), you may need to split it again.
The gains would be :
Modules serve 1 purpose : Don’t repeat yourself. You write modules for 2 reasons :
Use as much modules from the Theodo Cloud internal library or Providers repositories as you can. If no module there matches your needs, here is how you should implement modules in your codebase:
Pay attention to terraform plan elapsed time as you build your modules.
Once you have your modules, you should focus on code readability and maintainability. So, implement naming standards, versioning standards and others.
Trying to change multiple parts or make multiple steps at once.
It’s very tempting to change a parameter on the resource or bump some versions while migrating to modules. But taking small steps and splitting complexity is always a better idea. Proceed at slow pace but keep the plan clear.
Going to extremes with layer design
Avoid both macro layers (everything in one state) and micro layers (excessive fragmentation). Find the right balance for your specific use case and team size.