terraform,

Refreshing Terraform state

Dec 09, 2021 · 1 min read · Post a comment
Refreshing Terraform state

Refreshing Terraform state is done by adding the -refresh-only flag (since terraform refresh is depricated) when running plan and apply operations. To start off, what does it mean and why are you getting stdout messsages telling you to add the -refresh-only flag on the next plan/apply operation?

Solution

Refreshing the Terraform state means that TF will read the up-to-date configs from all managed cloud resources and update the TF state to match.

Basically, you are getting the -refresh-only stdout messages when there is a TF state drift that has happened outside of Terraform since it’s last run.

A drift could happen because of a manual configuration update done by a team member, or even worse, by an unauthorized person, a TF misconfiguration, not using lifecycle blocks to ignore certain resources that are frequently updated by another app (CI/CD for example), not covering edge cases, and so on.

Once you are asked to refresh the TF state, run the following command:

terraform apply -refresh-only

Note: Add the -auto-approve flag if you don’t want to be prompt to type yes once TF compares the state with the remote resources configs, but I’m strongly against it.

Conclusion

As always, hope this was worthwhile. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on telegram.