tfscp is a tool designed for migrating Terraform resources across remote states. It simplifies the process by leveraging Terraform's existing capabilities.
tfscp automates the following steps:
- Creates a temporary operating directory.
- Validates that
terraform initsucceeds in both source and target state directories. - Downloads the source and target states locally.
- Uses
terraform state mvto move the specified resource from the source to the target state. - Pushes the updated target state back to the remote location using
terraform state push. - Cleans up the temporary operating directory.
To install tfscp, run the following commands:
wget https://github.com/thomasv314/tfscp/releases/download/v1.0.2/tfscp-darwin-amd64
mv tfscp-darwin-amd64 /usr/local/bin/tfscp
chmod u+x /usr/local/bin/tfscp
Usage:
tfscp [terraform resource address] [flags]
Flags:
--dry-run If true, do not actually move the resource (default true)
-h, --help help for tfscp
--source-dir string Source directory to move a terraform resource from (optional, defaults to CWD)
--target-dir string Target directory to move a terraform resource to
Suppose you have the following Terraform states:
└── apps
├── applications.tf
├── config.tf # Uses s3://terraform-states/apps.tfstate
└── foobar-service
├── config.tf # Uses s3://terraform-states/foobar-service.tfstate
└── service.tf
2 directories, 4 files
Here, the apps directory contains a module for your app: module.foobar_service.
By default, tfscp operates in a safe "dry-run" mode. To preview actions, run tfscp from the source directory:
tfscp "module.foobar_service" --target-dir="./foobar-service"
For example:
If the dry-run output looks good, execute the migration by disabling the dry-run mode:
tfscp "module.foobar_service" --target-dir="./foobar-service" --dry-run=false

