How to move modules and resources

Hey all, I am trying to do some refactoring to move modules. Let’s call our huge module X, which has a resource file s3.tf. s3.tf is using a module to create a bucket so terraform plan will spit out the address as module.<module_name>.<s3_bucket_name>.aws_s3_bucket.bucket.
I want to move this out of module X and create a separate module Y and have the same resource file but under Y
Below will be the new project structure

.
└── root/
    ├── X/
    │   ├── main.tf
    │   ├── variables.tf
    │   ├── other tf files
    │   └── s3.tf -- which will be deleted during the move
    ├── Y/
    │   ├── s3.tf
    │   ├── main.tf
    │   └── other tf files
    ├── main.tf
    └── output.tf

I am trying to use the moved block to move the resources but Terraform is not able to find the correct movement of modules/resource. I have tried this in root/main.tf

moved {
  from = module.X.module.<s3_bucket_name>.aws_s3_bucket.bucket
  to   = module.Y.module.<s3_bucket_name>.aws_s3_bucket.bucket
}

What am I missing? With the current plan, it will destroy the resources and recreate the resource with the same address.

Hi @crazy_avatar,

I can’t tell what is happening without a complete example, but given the comment “which will be deleted during the move” it sounds like maybe you are expecting Terraform to modify the config to?

You need to make the config changes in tandem with addition of the moved block. The moved block only handles fixing up the existing state to match the config, the config should still declare exactly what you want the end result to be.