Multi region deployment with Terraform modules
The problem I was managing infrastructure across multiple regions a handful of virtual machines, one per region, each running the same service. The Terraform setup had grown the way these things usually do: when I needed a VM in a new region, I copied the folder for an existing region, renamed a few things, and adjusted the values that were different (location, network ranges, naming). It worked.…
Terraform infrastructure management across multiple regions was becoming increasingly cumbersome, with each region having its own set of resources and configurations. The process of adding a new region involved copying an existing region's folder, renaming a few files, and adjusting a few values. As the number of regions grew, the similarities between them dwindled to just ten lines of code, making any structural changes a tedious process that had to be replicated across all regions.
The solution was to refactor the infrastructure definition into a shared module, with only the differences expressed as data. This approach involved three layers: 1) a shared module containing the actual resource definitions, written with input variables for everything that varied between regions, 2) a single shared entry point that called the module, instead of a separate root configuration for each region, and 3) a small config file per region, specifying only its own values.
This new structure allowed for adding a new region with just one small config file, without modifying any resource code or pipeline definitions. The benefits extended beyond just less typing. The main advantages were improved correctness, as structural changes now only needed to be made once and would automatically apply to all regions.
Drift between regions became easier to detect, and reviewing pull requests for new regions became significantly simpler. The key takeaway was to share the code, not the blast radius, by keeping each region's Terraform state file and deployment approval gate separate from the shared module.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.