nimblehq / infrastructure-templates

For IaaS and PaaS as codes
MIT License
10 stars 0 forks source link

Use Terraform locals instead of variables to manage application configs and environment variables #199

Open longnd opened 1 year ago

longnd commented 1 year ago

Why

In previous projects, we have extensively used Terraform variables to manage application configurations and environment variables. However, this creates a dependency between the infrastructure repository and Terraform Cloud, and we lose the ability to audit the changes made, i.e. the values of Terraform Cloud variables are mistakenly changed.

Recently, we adopted a new approach for the same purpose by using Terraform locals in a project (example implementation. We should update the infrastructure-template to follow this approach so that it can be used in future projects.

Example, instead of using a variable (and the value is controlled by a Terraform cloud variable) to hold the RDS instance type

variables "rds_instance_type" {
   type = string
}

it should be controlled by a local value instead

current_db_config = local.db_config[var.environment]
db_config = {
  staging = {
    rds_instance_type  = "db.t3.small"
  }

  prod = {
    rds_instance_type  = "db.t3.large"
  }
}

Who Benefits?