Moving resources to a brand-new Terraform State File

Hi all.

I have a bunch of resources created, and the infrastructure has become too big, so I need to move some resources to their own State file.

I’d appreciate if you guys could let me know if my approach is correct, please?

Current structure looks like this:

├── CHANGELOG.md
├── README.md
├── bitbucket-pipelines.yml
├── data.tf
├── env.tf
├── main.tf
├── modules
│   ├── route53
│   │   ├── README.md
│   │   ├── example.com.tf
│   │   ├── foo.com.tf
│   │   ├── outputs.tf
│   │   ├── private-hosted-zones.tf
│   │   ├── public-hosted-zones.tf
│   │   └── variables.tf
├── notify-slack-requirements.tf
├── outputs.tf
├── provider.tf
├── variables.tf
└── versions.tf
  1. I run terraform state list to get the exact resource name
module.route53.aws_route53_record.tfer--example-record1
module.route53.aws_route53_record.tfer--example-record2
module.route53.aws_route53_record.tfer--example-record3
module.route53.aws_route53_record.tfer--example-record4
module.route53.aws_route53_record.tfer--example-record5
  1. I start the migration of the resources to a brand-new Terraform state file
terraform state mv -state-out='./modules/route53/terraform.tfstate' module.route53.aws_route53_record.tfer--example-record1 module.route53.aws_route53_record.tfer--example-record1

terraform state mv -state-out='./modules/route53/terraform.tfstate' module.route53.aws_route53_record.tfer--example-record2 module.route53.aws_route53_record.tfer--example-record2

terraform state mv -state-out='./modules/route53/terraform.tfstate' module.route53.aws_route53_record.tfer--example-record3 module.route53.aws_route53_record.tfer--example-record3

terraform state mv -state-out='./modules/route53/terraform.tfstate' module.route53.aws_route53_record.tfer--example-record4 module.route53.aws_route53_record.tfer--example-record4

terraform state mv -state-out='./modules/route53/terraform.tfstate' module.route53.aws_route53_record.tfer--example-record5 module.route53.aws_route53_record.tfer--example-record5
  1. I create the provider.tf file and any other file necessary to run Terraform in the ./modules/route53/ directory. It should look like something like this:
├── CHANGELOG.md
├── README.md
├── bitbucket-pipelines.yml
├── data.tf
├── env.tf
├── main.tf
├── modules
│   ├── route53
│   │   ├── README.md
│   │   ├── example.com.tf
│   │   ├── foo.com.tf
│   │   ├── main.tf # <-- Added now
│   │   ├── outputs.tf
│   │   ├── private-hosted-zones.tf
│   │   ├── provider.tf # <-- Added now
│   │   ├── public-hosted-zones.tf
│   │   └── variables.tf
├── notify-slack-requirements.tf
├── outputs.tf
├── provider.tf
├── variables.tf
└── versions.tf

Is this approach the correct way? Any suggestions are much appreciated. Thanks!

Yes, I’ll be doing a backup of the original Terraform state file prior this migration, just in case. :slight_smile:

I’ve tried the following:

terraform state mv -state-out=modules/route53/.terraform/terraform.tfstate module.route53.aws_route53_record.tfer--example-record1 aws_route53_record.tfer--example-record1

The output is:

Move "module.route53.aws_route53_record.tfer--example-record1" to "aws_route53_record.tfer--example-record1"
Successfully moved 1 object(s).

Then, I go to the Route53 folder and try to list the existing infrastructure:

cd modules/route53
terraform state list

I get the following error:

Error: Failed to load state: Terraform 1.0.9 does not support state version 4, please update

That error appears if I try terraform plan or any other command.