-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
state commands with remote state backends #15652
Conversation
The default value for the -state flag was overriding the location of any remote state.
In order to use a backend for the state commands, we need an initialized meta. Use a single Meta instance rather than temporary ones to make sure the backends are initialized properly.
These already include detailed messages, and it's not a usage issue, it's a config or file location issue.
c529d14
to
cdcd8e8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
Could we be more explicit about the fact that using -state
causes the configured backend to be ignored? Currently it's implied by explaining what happens when the flag isn't set ("By default it will...") but perhaps we could use more direct language here, like:
-state=FILE
- Ignore the current backend configuration and instead operate on the given local state file.
(I generally prefer to have CLI option docs written as imperative statements, but I see that the others here are written more like descriptions of their arguments; I would be okay with leaving this as-is for now and then deciding separately whether we want to holistically update all the CLI option docs to be imperative statements, if you like.)
command/state_mv.go
Outdated
to. This defaults to the same statefile. This will | ||
overwrite the destination state file. | ||
-state-out=PATH Path to the destination state file to write to. If this | ||
isn't specified, the source state file will be used. This |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, got some tabs in here
Update the documentation to match the current behavior, and make the usage output and website docs match.
cdcd8e8
to
55d18dc
Compare
do you do a release soon? I just ran into the |
I am aiming to rename the resource in the remote(s3) state file once an instance has passed all tests. from app_deploying to app_deployed. This is so that next time we deploy there would not be a clash in the state files. Next time we deploy we would first destroy the old 'app_deployed' before renaming the new app_deploying to app_deployed. Hope the above makes sense and is reasonable. But when trying the command,
I get:
Terraform version: v0.10.4 Is the above command expected to work? Edit: Please note, I did initialise with terraform init. The resulting config is:
|
I doubt the workflow you describe has anything to do with that failure, but it should work as expected. When you run the state command, terraform is seeing the config has changed for some reason, most likely your backend config is in a subdirectory which you are providing as an argument to It's on our roadmap to try and reconcile how terraform can operate on paths in some cases, but retains it's local config state in the working directory. |
Thanks @jbardin I realise the issue. I have to cd in to the directory of the terraform configs in order to submit this command. I initialised with the path at the end on initialization (for some reason I have got into the habit of never using cd when writing automation if I can help it). Appending the path like I did with init is not an option with terraform state mv though. So in this case I will need to 'cd'. Is this expected behaviour? |
Re-reading your comment after learning what I wrote about in my last comment makes it more clear now. I think you have already answered my questions. It is expected behaviour for now, but you plan to reconcile. Thanks |
I still don't understand from the docs on how to use terraform state mv on remote state. I am trying to move certain modules out of the original remote statefile to a new remote statefile. I am doing the below (my backend is configured to point to the correct bucket and path to the original statefile:
At this stage is |
@mukund1989 I am able to push the resource to a new state with terraform When destination exists - source is updated (the resource gets deleted - but I assume the next terraform plan/apply would generate it again), but destination is not! I'll experiment some more and come back with results. |
So, it seems to work with some plumbing commands: For example, if you want to move resource from state A to state B:
This works, but as you can see, it's pretty rudimentary. Perhaps there is another solution :) |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Some state commands worked with the backend configuration (push, pull, list, show), while others ignored it (mv, rm), leading to confusing behavior when trying to refactor state files. The website docs were also incorrectly updated, and did not match the behavior of the command, making the overall usage of the state commands confusing and possibly dangerous.
This makes the
rm
andmv
commands also work with a configured backend, reflecting the behavior of the other state commands, and the logical expectations of users.The one piece missing here is that there is no single command to move resources or modules from a local state file to a remote state, or between remote states. There are a number of ways we could introduce the behavior, a special identifier for
-state-out
like@backend
for example, but complex refactoring probably warrants pulling the state into local files anyway, and not all possible options need to be supported. We can put off handing remote targets and workspaces for future refactoring.Fixes #10481