Open cspotcode opened 1 year ago
Thanks for this issue report! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional reports. Thanks again!
I'm also facing this problem and I'd say this is not a "use case" but a bug.
I export the TF_CLI_ARGS_init
environment variable with -backend-config=/path/to/local-backend-options.hcl
as value where the file contains the path
and workspace_dir
fields, both set to a custom location (maybe a unnecessary detail, but I do this because the custom path contains all my TF states which are auto-synced with my self-hosted Seafile instance in order to backup the states automatically).
Anyway, using the terraform state rm
command will not take this into account which is kind of expected due to the scope of the environment variable being the init
command, but I still think this is a bug because Terraform should take this path into account. The required metadata is stored in the .terraform/terraform.tfstate
file where the custom backend path is information of it (backend.config.workspace_dir
field). I manage my states with workspaces, but I also checked that the directory within the backend.config.workspace_dir
path contains the required directory (named like my workspace) and terraform workspace show
also yields the correct name.
One workaround I applied is to also export the TF_CLI_ARGS_state_rm
environment variable with -backup=/path/to/my/custom/workspace_dir/$(terraform workspace show)/terraform.tfstate.backup
as value so that Terraform uses the file in the custom workspace directory instead of the default terraform.tfstate.d
directory, located in the current working directory. The $(terraform workspace show)
subshell inline call allows to dynamically use the correct directory of the currently active workspace.
I know that this setup might not be the one of a "common user", but setups like this often also bring up the edge cases where bugs are hidden 😉
Terraform Version
Terraform Configuration Files
Debug Output
-
Expected Behavior
When using local state with custom
workspace_dir
,terraform state rm
stores the state backup into the correctworkspace_dir
Actual Behavior
terraform state rm
tries and fails to save state backup into the default directory which does not exist.terraform.tfstate.d
does not exist because I have setworkspace_dir
to.terraform-qa.tfstate.d
, so that is where state files reside.I can confirm this by looking at
.terraform-qa/terraform.tfstate
(I am also using a customTF_DATA_DIR
pointing to.terraform-qa
)Steps to Reproduce
terraform init
w/local state backend, custom backend configworkspace_dir
Attempt toterraform state rm
something from the state.Additional Context
No response
References
No response