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
- 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
- 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
- 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.