Incase of any update in resource does terraform deletes the resource first or cr . . .

Moiz Asif:
Incase of any update in resource does terraform deletes the resource first or creates an updated resource then delete the previous resource?

Kylash M R:
@Moiz Asif when you do terraform plan, tf syncs the manual change made and updates the state file. It will then show update in place to be made when Terraform plan.

Kylash M R:
If given yes as confirmation, TF deploys the resource as specified in the tf config files.

Moiz Asif:
@Kylash M R So if I have made change in resource like ec2 will it create a new ec2 with those changes and then delete the old ec2 or delete first then spin a new one, also does this behavior vary from resource to resource?

Kylash M R:
TF will do in place update by deleting the existing resource and creating one as present in tf config files.

Kylash M R:
The tf config file is the desired state and tf always tries to match the desired state.

Kylash M R:
Thats the reason why terraform syncs the changes with real infra when doing terraform plan(internally runs Terraform refresh)

Moiz Asif:
I am sorry but I am still not clear on this do you mean both process will be done simultaneously like a new ec2 will be created but since there are two ec2 now and state file allows one so old one is deleted?

Kylash M R:
Yes, right old one will get deleted and new instance will be created that matches the desired state

Kylash M R:
You will know better once you execute this plan.

Moiz Asif:
This means that updated resource is created first than old resource is deleted, I am confused because we have a lifecycle rule to prevent delete before creation so my guess is that new one is created after old is deleted.

Kiran Prabhu:

  1. Compare TF state with Real Infra
  2. If state does not match - delete the resource
  3. Deploy the resource

Moiz Asif:
Ok thanks I will check

Kylash M R:
Oh…you didn’t mention lifecycle rules.

Moiz Asif:
No thats okay I wanted to know the default behaviour

Kylash M R:
Okay. Remember this : Terraform always tries to match the desired state( tf config file)

Kylash M R:
If something changes, tf destroys them and again goes with desired state

Kiran Prabhu:
https://developer.hashicorp.com/terraform/language/resources/behavior

Moiz Asif:
Yes thanks @Kylash M R I think this and @Kiran Prabhu’s message sums it up pretty well for me thankyou both :raised_hands: