Terraform renaming state file

Viewed 5006

How can I rename the .tfstate while its holding resources? Example as shown below.

terraform {
  backend "s3" {
    bucket         = "px-terraform-state"
    region         = "ap-southeast-1"
    key            = "network/transaction-logs.tfstate"
    encrypt        = true
    dynamodb_table = "px-terraform-state"
  }
}

to

terraform {
  backend "s3" {
    bucket         = "px-terraform-state"
    region         = "ap-southeast-1"
    key            = "network/sftp-logs.tfstate"
    encrypt        = true
    dynamodb_table = "px-terraform-state"
  }
}
2 Answers

There are two main options for this.

The first is to just change the configuration as you did here and then run terraform init, at which point Terraform should offer to migrate your state from the old location to the new location automatically. If you confirm, Terraform will read the state from the old location, write it to the new location, and then delete the old location.

Another option is to move the object directly in S3 to the new location, then change your configuration as you showed, and finally run terraform init -reconfigure, where the -reconfigure option disables the automatic migration behavior I described in the previous paragraph and just uses the new configuration as a direct replacement for the old configuration, forgetting the old configuration entirely. Once this has succeeded you should then ensure that there is not still a state snapshot at the old location, because otherwise someone may find it in future and be confused about which one is the latest snapshot.

Putting Martin's answer into step-by-step recipes:

Option 1: Modify backend config and re-initialize:

  1. [Optional] Pull down a copy of your current state file just in case prior to any changes: terraform state pull > /tmp/backup.tfstate
  2. In the backend {} config, update the key to the name of the new state file.
  3. In your Terraform root directory, run terraform init.
  4. Terraform will prompt you to migrate your state to the new backend state file. For an S3 backend, the message may look like this:

Do you want to copy existing state to the new backend? Pre-existing state was found while migrating the previous "s3" backend to the
newly configured "s3" backend. No existing state was found in the newly configured "s3" backend. Do you want to copy this state to the new "s3" backend? Enter "yes" to copy and "no" to start with an empty state.

  1. Changes are done. You may wish to log into the console where your remote state is stored and verify that the filename changed to match. You may also wish to delete/rename the old state file which may still be there (e.g. if you have versioning enabled).

Option 2: Rename state file, modify backed and re-configure:

  1. [Optional] Pull down a copy of your current state file just in case prior to any changes: terraform state pull > /tmp/backup.tfstate
  2. In the backend store, e.g. S3, rename the state file.
  3. In the backend {} config, update the key to the name of the new state file (same name as in the previous step).
  4. In your Terraform root directory, run terraform init -reconfigure.
  5. Changes are done. You may wish to take a look at the local .terraform/terraform.tfstate to ensure it's pointing to the correct file/key in the backend.
Related