Reputation: 171
Tried the following:
Resource creation(ec2) using terraform>>manually edit a resource(changed type) from AWS console>>run 'terraform plan'>>shows changes required after doing 'terraform refresh' internally BUT state file is not getting updated?
Looks like some bug.
(article states refresh is done automatically, it should affect the state file?- https://www.terraform.io/docs/cli/commands/plan.html)
I note that Manually running 'terraform refresh' updates the state file to reflect present state of infrastructure(which then would be compared with config file ie desired state).
Upvotes: 2
Views: 14566
Reputation: 216
As per the below documentation link, you can update your state file without making modifications to your infrastructure using the -refresh-only flag for plan and apply operations.
https://developer.hashicorp.com/terraform/tutorials/state/refresh
But when I checked with terraform version 1.66, it's still not working properly
Test Scenario:
Execution Steps:--
but -refresh-only option working fine when using it with apply command like this:
$ terraform apply -refresh-only
Would you like to update the Terraform state to reflect these detected changes?
Terraform will write these changes to the state without modifying any real infrastructure.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
Upvotes: 0
Reputation: 743
As per the latest documentation the plan operation implicitly does refresh unless you specify refresh=false as a parameter.
Upvotes: 2
Reputation: 171
Thanks @Software Engineer
The second para explicitly mentions that 'state' is not affected. So in summary, its not treated like a normal 'terraform refresh'. IMAGE
https://www.terraform.io/docs/cli/commands/plan.html
Upvotes: 1