Skip to content

New Code Action: Move a block between modules #1566

Open
@radeksimko

Description

@radeksimko

Background

As Terraform adoption grows, users may begin with a single root module and end up with a lot of code which then needs refactoring in the sense of breaking it down to different modules.

This is usually mostly done by manually copying and pasting blocks around and adding moved {} blocks where existing state is involved. Such process can be laborious and the server could make it easier.

For example, the user may start with ./main.tf

resource "aws_vpc" "main" {
  // ...
}

resource "aws_instance" "jumphost" {
  // ...
}

and expects to end up with a new module/folder in ./network

and ./network/main.tf

resource "aws_instance" "jumphost" {
  // ...
}

and a modified ./main.tf

resource "aws_instance" "jumphost" {
  // ...
}

module "network" {
  source = "./network"
}

moved {
  from = aws_vpc.main
  to   = module.network.aws_vpc.main
}

Proposal

  • Provide code actions:
    • Move a resource to an existing module
    • Move a data to an existing module
    • Move a resource to a new module
    • Move a data to a new module

The moved block could be optional and maybe have sensible default either to generate it, or base it on whether there is a backend block and/or existing state.

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