Refreshing Terraform state is done by adding the
-refresh-only flag (since
terraform refresh is deprecated) when running
apply operations. To start off, what does it mean, and why are you getting stdout messages telling you to add the
-refresh-only flag on the next plan/apply operation?
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 similar things.
Once you are asked to refresh the TF state, run the following command:
terraform apply -refresh-only
Note(s): 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.
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.