Move resources within Terraform state (rename, move to module)
Move Terraform resources without recreation. Rename, move into/out of modules, or reorganize state structure. Use when refactoring infrastructure code without changing actual resources.
npx claudepluginhub lgbarn/terraform-aws-eks<source_address> <destination_address>state/Move resources within Terraform state without recreation. Arguments: $ARGUMENTS
moved Block (Recommended - Terraform 1.1+)Declarative approach, tracked in version control:
# moves.tf
moved {
from = aws_instance.web
to = aws_instance.application
}
moved {
from = aws_s3_bucket.data
to = module.storage.aws_s3_bucket.main
}
terraform state mv CommandImperative approach for immediate changes:
terraform state mv <source> <destination>
# Command approach
terraform state mv aws_instance.old aws_instance.new
# Moved block approach
moved {
from = aws_instance.old
to = aws_instance.new
}
# Command approach
terraform state mv aws_s3_bucket.logs module.logging.aws_s3_bucket.main
# Moved block approach
moved {
from = aws_s3_bucket.logs
to = module.logging.aws_s3_bucket.main
}
terraform state mv module.app.aws_instance.main aws_instance.main
terraform state mv module.old_name module.new_name
# From count to for_each
terraform state mv 'aws_subnet.private[0]' 'aws_subnet.private["us-east-1a"]'
## State Move: [Operation]
### Operation Details
- **Source**: aws_instance.web
- **Destination**: module.compute.aws_instance.main
- **Type**: Move into module
### Pre-Move Verification
**Current State**:
```bash
$ terraform state list | grep aws_instance
aws_instance.web
Source Resource:
$ terraform state show aws_instance.web
# aws_instance.web:
resource "aws_instance" "web" {
ami = "ami-0abcdef1234567890"
instance_type = "t3.large"
id = "i-1234567890abcdef0"
# ... other attributes
}
Add to configuration:
# moves.tf
moved {
from = aws_instance.web
to = module.compute.aws_instance.main
}
Then run:
terraform plan # Verify move, no recreation
terraform apply # Apply the move
terraform state mv aws_instance.web module.compute.aws_instance.main
Move "aws_instance.web" to "module.compute.aws_instance.main"
Successfully moved 1 object(s).
Verify new location:
terraform state list | grep aws_instance
module.compute.aws_instance.main
Verify no changes:
terraform plan
No changes. Your infrastructure matches the configuration.
Update your Terraform configuration to match:
Before (root module):
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = var.instance_type
}
After (in module):
# modules/compute/main.tf
resource "aws_instance" "main" {
ami = var.ami_id
instance_type = var.instance_type
}
If move was incorrect:
terraform state mv module.compute.aws_instance.main aws_instance.web
For multiple resources:
# moves.tf - Moving to module structure
moved {
from = aws_instance.web
to = module.compute.aws_instance.main
}
moved {
from = aws_security_group.web
to = module.compute.aws_security_group.main
}
moved {
from = aws_iam_role.web
to = module.compute.aws_iam_role.main
}
Or via commands:
terraform state mv aws_instance.web module.compute.aws_instance.main
terraform state mv aws_security_group.web module.compute.aws_security_group.main
terraform state mv aws_iam_role.web module.compute.aws_iam_role.main
## Safety Precautions
1. **Backup state first**:
```bash
terraform state pull > state_backup.json
Use -dry-run (if available):
terraform state mv -dry-run source dest
Verify with plan after move:
terraform plan
# Must show NO CHANGES
terraform plan