Open juarezr opened 2 years ago
Thanks for the request @juarezr,
The order of the dependencies
is not taken into account within Terraform, so it seems reasonable that we could sort these as part of the serialization process to help with data normalization.
The id
fields however are part of the data source, and not handled independently by Terraform (the fact that everything has an id
is only due to the provider SDK). The problematic data sources would need to be individually updated by each provider to prevent any changes from being recorded. There are also numerous other resources that update fields each time they are read which will cause the same type of change. While these may cause a nuisance generating noise in the state and "Objects have changed outside of Terraform", it is not incorrect for providers to do this, so it always will be something that must be dealt with.
If you are concerned with only tracking changes made by the configuration, and you are running Terraform from a centralized workflow, it perhaps may be better to track the output of the plans directly which will show planned changes separately from external drift.
In addition to what @jbardin said, while there would certainly not be any harm in making the parts of this that Terraform Core controls serialize in a consistent way, I want to be clear that keeping your Terraform state under version control is not a recommended workflow and so Terraform is not designed around that usage pattern. The only currently-supported approach for sharing state across a team is to use a remote state storage backend.
I wouldn't expect that recommendation to change even if we do make the state snapshot serialization use a normalized ordering, because it still doesn't address the fundamental workflow problem that you can't know the new state until you've applied the changes, but you typically shouldn't apply the changes until they are pushed to your version control system or else others might make conflicting changes that don't take yours into account and thus create a difficult-to-reconcile fork. There's some older discussion about that in #13891.
I completely agree that storing state in version control neither scales nor solves any team workflow need.
Also, it could be even dangerous besides isolated and temporary scenarios like proof-of-concept or experimentation by a single person
The only way it couldn't be worse it's leaving/losing the tfstate in your local computer.
Improve versioning and tracking of tfstate changes in Git commit and merge
Current Terraform Version
Use-cases
Currently, we have many terraform repositories that are separated by the specific needs of the application or the area of impact on our cloud infrastructure.
For tracking the state we use two strategies/use cases:
terraform.tstate
files tracked in git for smaller, simpler apps for reducing the burden of provisioning, backup, and maintenance.Problem description
Currently, when using local state files on git, like (2) above, we have a big changeset between two
terraform apply
that spans the whole lines of theterraform.tfstate
file even when the real change is just one property.This complicates tracking changes and merging in the use case (2).
The problem is that between two
terraform apply
commands:terraform.tfstate
in a random order.id
ofdata
blocks/attributes change according to a timestamp even if their properties are unchanged.See:
And:
Attempted Solutions
For dealing with this problem we had been:
dependency
and `id´Proposal
For reducing the changeset I would like to propose changes that IMHO don't impact on compatibility:
Sorting
dependency
in alphabetical order before writing toterraform.tfstate
This will improve because:
See:
Avoid changing the
id
ofdata
blocks when nothing else has changedThe data didn't change since the last timestamp anyway: