terraform moved block - ghdrako/doc_snipets GitHub Wiki

Introduced in Terraform 1.1, this feature allows you to define resource address changes directly in your configuration instead of manually moving state files.

resource "aws_instance" "a" {
  count = 2  # (resource-type-specific configuration)
}

resource "aws_instance" "b" {
  count = 2  # (resource-type-specific configuration)
}

moved {
  from = aws_instance.a
  to   = aws_instance.b
} 

Moving Resource Between Modules

## Before: main.tf
module "s3" {
  source = "./modules/aws/another_module"
}
## After: main.tf
module "s3" {
  source = "./modules/aws/s3"
}

moved {
  from = module.s3.aws_s3_bucket.moved_demo
  to   = module.another_module.aws_s3_bucket.moved_demo
}

Renaming a Module

## Before: main.tf
module "s3" {
  source = "./modules/aws/s3"
}
## After: main.tf
module "s3_renamed_module" {
  source = "./modules/aws/s3"
}

moved {
  from = module.s3
  to   = module.s3_renamed_module
}