I have my project structured like this:
.
├── modules/
│ ├── application/
│ │ ├── main.tf
│ │ └── variables.tf
│ ├── db/
│ │ ├── main.tf
│ │ └── variables.tf
│ └── cdn/
│ ├── main.tf
│ └── variables.tf
└── proj/
├── website_1/
│ ├── main.tf
│ ├── variables.tf
│ ├── dev.tfvars
│ └── prod.tfvars
└── website_2/
├── main.tf
├── variables.tf
├── dev.tfvars
└── prod.tfvars
----
### application/main.tf
resource "my_resource_type" "application" {
description = var.app_description
name = var.app_name
env = var.env_name
}
# lots more resources....
-----
### application/variables.tf
variable "app_name" {
type = string
description = "Name of the application."
}
variable "app_description" {
type = string
description = "Description for the application."
}
variable "env_name" {
type = string
description = "Name of the environment."
}
# lots more variable definitions...
db/main.tf and cdn/main.tf follow similar structure.
Then I have the files within my proj/ folder for the actual resources I want to apply.
### proj/website_1/main.tf
# shared resource configuration
module "application" {
source = "../../modules/application"
app_description = var.app_description
app_name = var.app_name
env_name = var.env_name
}
module "db" {
source = "../../modules/db"
# paramaters
}
module "cdn" {
source = "../../modules/cdn"
# paramaters
}
# unique website_1 config...
--------
### proj/website_2/main.tf
# shared resource configuration
module "application" {
source = "../../modules/application"
app_description = var.app_description
app_name = var.app_name
env_name = var.env_name
}
module "db" {
source = "../../modules/db"
# paramaters
}
module "cdn" {
source = "../../modules/cdn"
# paramaters
}
# unique website_2 config...
Website 1 and Website 2 combine multiple AWS resources in a reusable way, hence the separate modules. The problem is having to go inside project/website_1/ and project/website_2/ and retype the same variable definitions I used across my modules.
I understand this is a common problem in Terraform, but still, I'd like to avoid repeating my variable definitions if I can. It seems like symlinking a common variables.tf file is a bad practice, so what is the "correct"/best practice way (if any) to achieve what I'm trying to achieve within Terraform (without using a separate tool such as Terragrunt)? I'm also open to changing my folder and file structure.
If they all follow the same structure, I would create a single source of truth
variables_template.tffile in a directory calledtemplatesand in the main directory or in a sub-directory I would create a shell script tocporrsyncthevariables_template.tfThen anytime you need to make changes to all the variables, just update the template and run the
update-variables.shscript.I would also
.gitignorethe variables in the subdirectories, as running the script once will populate them.