nerdexam
HashiCorp

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.

Use the Terraform CLI (terraform plan, apply, destroy, fmt, init, validate, workspace, import, taint, providers, output)

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)
  • A
    7% (3)
  • B
    5% (2)
  • C
    2% (1)
  • D
    86% (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.

ATerraform will terminate and recreate the VM

Terraform only terminates and recreates resources when a change in configuration forces replacement; no code change means no replacement.

BTerraform will create another duplicate VM

Terraform tracks resources in the state file and will not create a duplicate of an already-tracked resource.

CTerraform will apply the VM to the state file

Applying to the state file is not a meaningful action; the state file is already synchronized after the first successful apply.

DNothingCorrect

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

#Idempotency#Terraform state#terraform apply#Resource management

Community Discussion

No community discussion yet for this question.

Full TA-002-P Practice