codigube
codigube

Reputation: 1256

Terraform Apply has different "plan" than Terraform Plan

I sometime see that Terraform Apply has different "plan" than Terraform Plan.

For instance, today i have seen one of TF files that I am trying to "Terraform Apply" resulted in only 1 "change" and 1 "add" while it got "3 add", "1 change" and "3 destroy" when using "Terraform Plan"

I have been using Terraform for just two months. Is this intended behavior in Terraform?

Could anyone give an explanation for this behavior? Thanks!

Terraform version: 0.11.13

Upvotes: 1

Views: 4122

Answers (7)

simmyk
simmyk

Reputation: 26

Have been using terraform for quite a long time and this is not an intended behaviour. It looks like something has changed in between plan and apply. But what you can do is save the plan in a file using

terraform plan -out plan.tfplan 

and then deploy using same file

terraform apply plan.tfplan.

Upvotes: 0

Makafui Naaman
Makafui Naaman

Reputation: 11

Try to follow these steps when trying to perform a Terraform apply .

  • First make sure the changes to the terraform file has been saved.
  • Try running a terraform plan terraform-plan before running terraform-apply

Sounds like some the files changes have been made to are not saved with the current terraform file

Upvotes: 1

Did you comment any of the resources in terraform file while triggering command : terraform apply ?

If yes Please check the same as commenting resources in existing terraform file will result in destroying those resources in terraform.

Upvotes: 0

Apaar Sharma- AIS
Apaar Sharma- AIS

Reputation: 46

This is unexpected behaviour, but the best practice it to:

  1. terraform plan -out deploy.tfplan

it will save the plan in the deploy.tfplan file.

  1. Now, terraform apply deploy.tfplan.

this will ensure that the plan you want is executed all the Time without fail.

Upvotes: 2

deepanmurugan
deepanmurugan

Reputation: 2113

This is not an intended behaviour of terraform unless if there is a mess anywhere. I never saw this kind of issue any time till now. Did you ever edited or deleted your .tfstate state file after you passed the terraform plan command? If you are observing this issue again or still facing this kind of issue, probably you can open an issue with the product owner. But I don't think this is an issue and you will never face this kind of issue again.

Upvotes: 1

Shubham Sharma
Shubham Sharma

Reputation: 21

Terraform builds a graph of all the resources.It, then creates the non-dependent resources in parallel to make resource creation slightly efficient. Is any the resource creation fails, It leaves terraform in partially applied state which gets recorded in the tfstate file. After fixing the issue with resource, when you reapply the .tf files it shows you only the new resources to be changed. In your case, I think it has got more to do with the fact that some resource have a policy of "destroy-before-creation" which shows up in result. so when you apply change to 1 resource it ends up showing 1 resource deleted 1 created. This when occurs with some non "destroy-before-creation" type resources, ends up giving you output like what you mentioned above

Upvotes: 0

Ashish Bhatia
Ashish Bhatia

Reputation: 629

Can you explain the full scenario? Normally, in my experience it is same. Difference i can only see -- Either you are using variable file with plan and apply and some variables causes some resources and other way might be if you using a remote location for state and some other job/person also updating the state. If you are running everything locally, it should not happen like this.

Upvotes: 0

Related Questions