Here's a step by step way to do this that's a little different than the default answer.
First, we have a default s3 backend.
terraform {
backend "s3" {
encrypt = true
bucket = "cool-bucket-name"
key = "production/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "cool-dynamo-db-lock-name"
}
}
Now, check our workspaces.
terraform workspace list
default
* weird-workspace-name
Pull the state down by first commenting out the terraform backend.
#terraform {
# backend "s3" {
# encrypt = true
# bucket = "cool-bucket-name"
# key = "production/terraform.tfstate"
# region = "us-east-1"
# dynamodb_table = "cool-dynamo-db-lock-name"
# }
#}
Run the command to migrate the state locally.
terraform init -migrate-state
You can check the state and see if it's still good to go.
terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Next, pull the state. The name of the state file is determined by what you originally gave as the key.
terraform state pull > terraform.tfstate
terraform workspace new cool-workspace-name
terraform state push terraform.tfstate
terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Alright, now go clean up your old state.
terraform workspace select weird-workspace-name
terraform state list | cut -f 1 -d '[' | xargs -L 0 terraform state rm
terraform workspace select cool-workspace-name
terraform workspace delete weird-workspace-name
Run the terraform apply command again.
terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
And now move your state back into S3, uncomment the terraform backend.tf file.
terraform {
backend "s3" {
encrypt = true
bucket = "cool-bucket-name"
key = "production/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "cool-dynamo-db-lock-name"
}
}
Run:
terraform init -migrate-state
I really appreciate the answer by bhalothia, I just wanted to add another step by step that's a little bit of a different case.