Description
Background
While refactoring Terraform configuration, users may sometimes leverage local values to extract some computations and make parts of the configuration "dynamic". There are even apparently tools which generate locals
entries according to some reports:
- Externally declared locals produce errors vscode-terraform#1581
- "config/terraform/*.tf" locals are ignored since v2.28.0 vscode-terraform#1584
The common theme of ending up in a situation where local value is used as a variable is most likely just misunderstanding of the language features. We cannot naturally change external tooling but the server could make it easier to fix these mistakes in existing configuration.
For example, the user may begin with
locals {
instance_size = "t3.micro"
}
and end with
variable "instance_size" {
type = string
default = "t3.micro"
}
Importantly we cannot tell whether a local variable was meant to be a variable as we don't have any context to decide or even guess that, we can give user the choice still though.
Proposal
- Provide
refactor.rewrite
code action which turns existing local value into a variable