Open schollii opened 2 years ago
Hi @schollii,
Long ago when locking was added to remote state operations, the terraform_remote_state
data source was not updated to use these locks to prevent any disruption in existing workflows, and it has remained this way ever since. This works out OK, because none of the remote state implementations have a consistency level which would ever return a corrupt state value on concurrent access, and even if that could happen, it would only cause the remote state data source to fail and just halt evaluation.
While we can look into locking options for terraform remote state when we decide on a plan to move forward with a new remote storage subsystem, I'm not sure locking would be added in the future either. The state locks are meant to protect the state from unsafe concurrent access, not as a mechanism for coordinating multiple processes. Even if the remote state data source were to lock the state, it can't serialize access in a defined order, meaning that the remote data source could be read immediately before or immediately after a concurrent operation with no error but yielding different results. Terraform has always left this type of workflow coordination to external systems.
Ok thanks. I think it would be worth adding a couple of sentences (in the remote state webpage) explaining that remote state data sources do not lock the remote state (even those backeds that support it like s3), because it is one less argument to provide (in the case of s3 backend, the dynamodb lock table is ignored and can be left out).
Terraform Version
Terraform Configuration Files
In one folder, create
main.tf
:In same folder create
backend.tf
(replace YOUR_ORG by something unique):Create an s3 bucket
YOUR_ORG-tfstate-backends
in us-east-1, and a dynamodb tabletfstates-lock
.Run
terraform init
andterraform apply
and store the state in s3. Verify that the tfstate is there.In another folder, create 'main.tf':
Run terraform init and apply.
Debug Output
n/a
Expected Behavior
terraform should abort in the second stack because it should try to lock the remote state and fail to find the dyndb table.
Assumption: the remote state should be locked when getting outputs; but I just checked and
terraform output
does not seem to lock the state file, so that's probably why the actual behavior is different from expected.However, this should be mentioned somewhere in the docs related to terraform_remote_state for backend = s3. I imagine the dynamodb_table could just be omitted.
Actual Behavior
terraform did not fail. "abc" is output, meaning the state was read without first locking it.
Steps to Reproduce
Explained in the terraform config files section otherwise too hard to explain, but basically:
Additional Context
No response
References
No response