0
votes

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).

Regards.

Aditya

1
Why would you expect it to update the state file? And why do you need that behaviour? Normally the state file would be updated by apply or destroy which is when Terraform is actually making changes. As you've noticed, you can run terraform refresh to force a refresh and update the state file but this isn't normally required.ydaetskcoR
Because the document says 'terraform plan' internally runs a 'refresh' which means the state file should get updated to reflect the current infrastructure.Aditya Garg
@ydaetskcoR - the OP could be using -refresh=true flag, which is the same as running tf refresh before the plan. I agree that this shouldn't be necessary unless you've got something else wrong but it is possible.Software Engineer
@AdityaGarg - no it doesn't say that. 2nd paragraph states This command is a convenient way to check whether the execution plan for a set of changes matches your expectations without making any changes to real resources or to the state. It does run a refresh, but it doesn't say that it updates the state file, it explicitly states that it doesn't.Software Engineer
@SoftwareEngineer thanks for that, the second para explicitly mentions that 'state' is not affected. So in summary, its not treated like a normal 'terraform refresh'Aditya Garg

1 Answers

0
votes

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