Terraform: Feature request: allow manual remote state locking

Created on 26 Jan 2018  ยท  3Comments  ยท  Source: hashicorp/terraform

Currently, backend providers can implement locking during operations that can write state. This happens automatically. And there is a force-unlock command for when things go wrong.

A command for manually locking without running an operation that can write state would be useful. If your backend supports it, it would write the lock but do nothing to the state. Perhaps a command to remove that manual lock would be useful as well.

cli enhancement

Most helpful comment

Hi, those who need terraform lock command.

I know Terraform doesn't provide terraform lock command by design. Nevertheless, I found it's useful to lock state manually when refactoring Terraform configurations. Let me share my use case and context.

In team development, Terraform configurations are generally managed by VCS such as git, and states are shared via a remote state storage which outside of version control. Most Terraform refactorings require not only configuration changes but also state manipulations such as state mv / rm / import. It is not desirable to change state before merging configuration changes. My colleague may be working for another task. I don't want team members to change the state during refactoring to avoid unexpected conflicts.

So I wrote an unofficial tool for locking Terraform state manually.
https://github.com/minamijoyo/tflock

Currently, it is tested only with Terraform 0.12 + AWS S3 (locked with DynamoDB).
The tflock uses a state lock function as same as Terraform uses under the hood. So other backend types may or may not work. Please be careful.

Thanks!

All 3 comments

Hi @2rs2ts,

When planning the implementation for state locks, we made the decision to not handle this particular use case. If desired, options like this can be provided by external automation and workflow systems. The force-unlock command is more of a failsafe, and only works for certain types of locks in some backends.

In practice, many backends may not even support maintaining a lock on a state without the command running. For example, in consul the lock is tied to the calling process being responsive, so the lock is released as soon as the terraform process exits.

Hi, those who need terraform lock command.

I know Terraform doesn't provide terraform lock command by design. Nevertheless, I found it's useful to lock state manually when refactoring Terraform configurations. Let me share my use case and context.

In team development, Terraform configurations are generally managed by VCS such as git, and states are shared via a remote state storage which outside of version control. Most Terraform refactorings require not only configuration changes but also state manipulations such as state mv / rm / import. It is not desirable to change state before merging configuration changes. My colleague may be working for another task. I don't want team members to change the state during refactoring to avoid unexpected conflicts.

So I wrote an unofficial tool for locking Terraform state manually.
https://github.com/minamijoyo/tflock

Currently, it is tested only with Terraform 0.12 + AWS S3 (locked with DynamoDB).
The tflock uses a state lock function as same as Terraform uses under the hood. So other backend types may or may not work. Please be careful.

Thanks!

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

franklinwise picture franklinwise  ยท  3Comments

c4milo picture c4milo  ยท  3Comments

rkulagowski picture rkulagowski  ยท  3Comments

rnowosielski picture rnowosielski  ยท  3Comments

ketzacoatl picture ketzacoatl  ยท  3Comments