TA-002-P · Question #407
You have a simple Terraform configuration containing one virtual machine (VM) in a cloud provider. You run terraform apply and the VM is created successfully. What will happen if you terraform apply a
The correct answer is D. Nothing. Terraform is idempotent - if the real infrastructure already matches the configuration and state file, a second terraform apply detects no changes and takes no action.
Question
You have a simple Terraform configuration containing one virtual machine (VM) in a cloud provider. You run terraform apply and the VM is created successfully. What will happen if you terraform apply again immediately afterwards without changing any Terraform code?
Options
- ATerraform will terminate and recreate the VM
- BTerraform will create another duplicate VM
- CTerraform will apply the VM to the state file
- DNothing
How the community answered
(43 responses)- A7% (3)
- B5% (2)
- C2% (1)
- D86% (37)
Why each option
Terraform is idempotent - if the real infrastructure already matches the configuration and state file, a second terraform apply detects no changes and takes no action.
Terraform only terminates and recreates resources when a change in configuration forces replacement; no code change means no replacement.
Terraform tracks resources in the state file and will not create a duplicate of an already-tracked resource.
Applying to the state file is not a meaningful action; the state file is already synchronized after the first successful apply.
Terraform compares the configuration to the state file and the actual infrastructure. If no differences are detected, the execution plan contains zero changes and Terraform exits without modifying anything, confirming the idempotent nature of the tool.
Concept tested: Terraform idempotency on repeated apply
Source: https://developer.hashicorp.com/terraform/intro/core-workflow
Topics
Community Discussion
No community discussion yet for this question.