Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The process of segregating environments using the main module approach in Terraform involves creating a separate Terraform configuration file for each environment (e.g. staging, production, etc.) and storing them in separate directories.

Each environment configuration file (main.tf) should define the resources and modules specific to that particular environment. However, to ensure consistency across environments, a main module can be created to house common resources and modules that are shared across all environments.

The main module should be defined in a separate directory that is outside of the environment-specific directories. The environment directories should reference the main module using a module block in the respective main.tf files.

For example, the directory structure might look like:

main-module/
└── main.tf
staging/
└── main.tf
production/
└── main.tf

Then, the main.tf file in the staging directory would declare the main module as follows:

module "main" {
  source = "../main-module"
}

And the main.tf file in the production directory would do the same:

module "main" {
  source = "../main-module"
}

This helps to ensure consistency across environments by sharing common resources and modules while still allowing for environment-specific configuration.