Terraform Associate Exam QuestionsBrowse all questions from this exam

Terraform Associate Exam - Question 174


You have a Terraform configuration that defines a single virtual machine with no references to it. You have run terraform apply to create the resource, and then removed the resource definition from your Terraform configuration file.

What will happen when you run terraform apply in the working directory again?

Show Answer
Correct Answer: B

When a resource is defined in a Terraform configuration and is subsequently removed from the configuration file, running 'terraform apply' will cause Terraform to align the real infrastructure with the current state of the configuration file. Since the configuration no longer includes the resource, Terraform will recognize that the resource should no longer exist and will destroy it.

Discussion

6 comments
Sign in to comment
keiffo2
Mar 6, 2024

If you remove the resource from your config file and the resource is in your state file, terraform will apply the configuration in the config file - which is to delete the resource

depal_dhirOption: B
Mar 7, 2024

This will destroy the VM

Nick_001Option: B
Sep 6, 2024

tested in lab. When reference to resource is removed in config file the next "terraform apply" will destroy the resource. Below is the output from cli # aws_instance.name will be destroyed # (because aws_instance.name is not in configuration)

dokaedu
Mar 29, 2024

The question is saying removing the description of the resource, NOT removing the resource (VM)

dnscloud02
Apr 9, 2024

definition not description

dokaedu
Mar 29, 2024

The question is saying removing resource definition of the resource, NOT removing the resource (VM)

chessmaster5000Option: B
Mar 22, 2025

When you remove a resource definition from your Terraform configuration and then run terraform apply, Terraform will see that the resource exists in the state file but is no longer defined in the configuration. Terraform interprets this as a signal that the resource should no longer exist and will plan to destroy it to match the current configuration. So, when you run terraform apply: -It will show a plan to destroy the VM. -Once you confirm, it will delete the VM from the cloud provider and remove it from the state file. This behavior ensures that the actual infrastructure matches what’s defined in your .tf files.