Terraform apply order for output values

I’m considering using terraform output values to store metadata about the terraform configuration in the state. For example, repository revision so that we can correlate state changes with specific revisions of the configuration.

My is: what happens during an apply failure, are there situations where the output value wouldn’t be updated in the state because of some other random failure in the apply? Or does this not matter because of parallelism?

Hi @nishkrishnan,

If there is an error during the apply step then indeed it’s possible that a particular output value might not be updated.

It isn’t possible for anything inside your configuration to depend on a root module output value – it’s only visible outside of Terraform once the run is complete – so it’s always possible in principle for an error to occur before Terraform reaches the output value, although if the output value itself doesn’t depend on any external side-effects then it’s likely (not guaranteed) that it’ll get updated before any fallible actions happen just due to the fact that updating an output value is essentially instantaneous when compared to the time taken to make an API request over the network.

so basically what I’m understanding is that terraform apply fails the entire process at the first error it hits, so its theoretically even possible to not have state change at all during an apply operation right?

Are there any plans to potentially make this a bit smarter? (ie. guarantee committing what you can to the state before failing due to some external error?)

As long as the state storage remains writable throughout the operation, Terraform will still save a state snapshot before exiting with an error if at least one action has already completed to update the state.

That state snapshot will not record the outcome of any actions that didn’t happen yet before the error, though. So it would not include a change to your output value if that action happens to not have occurred yet when Terraform encounters the first error during apply.