Is it possible to name the remote state file

I am using a remote state which hosts the Terraform state file in a AWS S3 bucket. The state file name by default is terraform.tfstate. I would like to give a more readable name to Terraform state file, e.g. “my_us_east_vpc.state” rather than “terraform.tfstate”, is it doable?

Thanks

For the S3 remote state backend, you can specify the key path where the state file is stored. I don’t think we have a documented process for renaming the state file, but I imagine you could:

  • Rename the state file in your S3 bucket manually
  • Update the key parameter in your Terraform config
  • Run terraform plan to make sure the state can still be read

I haven’t tested this, so please be careful if you do try it!

  1. Terraform maintains everything under tfstate file only. If you will update tfstate file name under backend.tf and will run terraform plan or terraform apply it will try to recreate the resources for you and might also end up in some error due to conflicts.
  2. Copy existing tfstate file in s3 bucket for exmaple- my-state.tfstate to my-new-state.tfstate.
    and update your backedn.tf pointing to my-new-state.tfstate.
    Now if you will run apply/plan it will work for you.
    Also in some cases if you ran terraform plan before copying old tfstate file, you may want to delete the new tfstate file from s3 and its lock-id md5 entry from dynamodb.

This was both frustrating and funny to read. “I think this will work… GOOD LUCK”.

1 Like

However, since my state didn’t consist of much and I wasn’t worried about losing it. I went ahead and tried this plan. My steps were as follows:
1 - run a terraform plan to refresh state
2 - rename bucket object in s3
3 - update state.tf file key value to match new name
4 - terraform init -reconfigure

This was the output:

> terraform plan
╷
│
│ Reason: Backend configuration block has changed
│
│ The "backend" is the interface that Terraform uses to store state,
│ perform operations, etc. If this message is showing up, it means that the
│ Terraform configuration you're using is using a custom configuration for
│ the Terraform backend.
│
│ Changes to backend configurations require reinitialization. This allows
│ Terraform to set up the new configuration, copy existing state, etc. Please run
│ "terraform init" with either the "-reconfigure" or "-migrate-state" flags to
│ use the current configuration.
│
│ If the change reason above is incorrect, please verify your configuration
│ hasn't changed and try again. At this point, no changes to your existing
│ configuration or state have been made.
╵
> terraform init -reconfigure
Initializing modules...


Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v3.75.2

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

You can name the state file anything you like. There is no rule that is must be terraform.tfstate.

I don’t think any of mine are named terraform.tfstate, they are all descriptive so I can look at them all in the same s3 key together.