Skip to content

New Code Action: Rewrite variable as a local value #1569

Open
@radeksimko

Description

@radeksimko

Background

While making Terraform configuration dynamic, users may sometimes leverage variable in a context where it's not appropriate, such as when the variable will never be provided from the outside (either as module input or by user).

Typically the user would encode the values in default, e.g.

variable "internal_name" {
  type    = string
  default = "foobar"
}

This may most likely be a result of them not knowing about or how to use local values, i.e. not knowing the same goal can be achieved more elegantly with

locals {
  internal_name = "foobar"
}

We may not always know the context to be able to reliably tell when a local value is more appropriate than a variable, therefore we cannot report this as a diagnostic.

We can however make it easier for the user to turn existing variables with defaults into local values.

Proposal

  • Provide refactor.rewrite code action which turns any variable with default into a locals entry

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions