Environments and Modules
Multi-environment parity with modules: one standard design, different settings per environment, and where modules come from.
One design, many databases
A module is a reusable package of Terraform code, such as "a standard sales database". Each environment uses the same module with its own settings, so development, test, and production share one design and differ only where you choose. That's how Terraform delivers multi-environment parity.
Without modules, teams copy the code for each environment, then change the copies over time, until test no longer looks like production. With a module, the design lives in one place: fix it once, and every environment gets the fix.
Writing a module
A module is just a folder of Terraform files with variables as its inputs and outputs as its results. The team's standards are written into it once:
modules/sales_db/
├── variables.tf # inputs: environment, ecpu_count, backup_days, ...
├── main.tf # the database, with the team's standards built in
└── outputs.tf # id, connection details
# modules/sales_db/main.tf (simplified)
resource "oci_database_autonomous_database" "this" {
compartment_id = var.compartment_id
db_name = "SALES${upper(var.environment)}"
db_workload = "OLTP"
compute_model = "ECPU"
compute_count = var.ecpu_count
is_auto_scaling_enabled = var.auto_scaling
backup_retention_period_in_days = var.backup_days
data_storage_size_in_tbs = 1
admin_password = var.admin_password
freeform_tags = { app = "sales", environment = var.environment, owner = "dba-team" }
}
Using the module in each environment
A common layout gives each environment its own small folder that calls the shared module with its own values, and keeps its own state:
sales-db/
├── modules/sales_db/ # the shared design
└── envs/
├── dev/main.tf # calls the module with dev values
├── test/main.tf
└── prod/main.tf # calls the module with prod values
# envs/prod/main.tf
module "sales_db" {
source = "../../modules/sales_db"
environment = "prod"
compartment_id = var.compartment_id
ecpu_count = 8
auto_scaling = true
backup_days = 60
admin_password = var.admin_password
}
# envs/dev/main.tf: same design, smaller and cheaper
module "sales_db" {
source = "../../modules/sales_db"
environment = "dev"
compartment_id = var.compartment_id
ecpu_count = 2
auto_scaling = false
backup_days = 7
admin_password = var.admin_password
}
A glance at the two files shows every difference between the environments, and nothing else. To promote a change to the design, you change the module once, then plan and apply in dev, then test, then production.
Other ways to separate environments
- One folder, a tfvars file per environment
- One set of code with
dev.tfvars,test.tfvars, andprod.tfvars, and a separate backend key for each. Simple, and fine for small projects, but it's easier to apply the wrong file to the wrong environment. - Workspaces
terraform workspace new prodkeeps several states for one configuration. Handy for short-lived copies, such as a test environment per feature branch, but risky for production, because nothing in the code shows which workspace you're in.
For production databases, separate folders per environment, each with its own state and permissions, are the safest choice.
Where modules come from
- Your own repository: local paths, such as
../../modules/sales_db, or another Git repository, pinned to a tag:source = "git::https://github.com/acme/tf-modules.git//sales_db?ref=v1.3.0". - The Terraform Registry: public modules published by cloud providers and the community, such as modules for networks or databases. Review them before use, and pin the version.
- A private registry: HCP Terraform and some platforms host modules privately for your organization.
Key takeaways
- A module packages a standard design, such as "a sales database", with variables as inputs and outputs as results.
- Each environment calls the same module with its own values and keeps its own state, giving parity with deliberate differences.
- Separate folders per environment are the safest layout for production; tfvars files and workspaces suit simpler cases.
- Modules can come from your repositories, the Terraform Registry, or a private registry; always pin versions.
- Change the module once, then promote it through dev, test, and production.