Edit module

Terraform (HCL) pattern

Update a module by specifying its old source and the new one.

It will update the source attribute of the modules and remove all variables that are not found on another module path.


Apply with the Grit CLI
grit apply edit_module

Rewrites as expected

BEFORE
// @filename: main.tf
module "test_module1" {
  source    = "old_source"
  variable1 = "variable1"
  variable2 = "variable2"
  variable3 = "variable3"
  variable4 = "variable4"
}

module "test_module2" {
  source    = "old_source"
  variable1 = "variable1"
  variable2 = "variable2"
  variable3 = "variable3"
  variable4 = "variable4"
}

module "test_module3" {
  source    = "another_source"
  variable1 = "variable1"
  variable2 = "variable2"
  variable3 = "variable3"
  variable4 = "variable4"
}

// @filename: variables.tf
variable "variable1" {}
variable "variable2" {
  description = "description"
}
AFTER
// @filename: main.tf
module "test_module1" {
  source = "new_source"
  variable1 = "variable1"
  variable2 = "variable2"
}

module "test_module2" {
  source = "new_source"
  variable1 = "variable1"
  variable2 = "variable2"
}

module "test_module3" {
  source    = "another_source"
  variable1 = "variable1"
  variable2 = "variable2"
  variable3 = "variable3"
  variable4 = "variable4"
}

// @filename: variables.tf
variable "variable1" {}
variable "variable2" {
  description = "description"
}