TA-002-P · Question #337
You have been working in a Cloud provider account that is shared with other team members. You previously used Terraform to create a load balancer that is listening on port 80. After some application…
The correct answer is A. Terraform will not make any changes to the Load Balancer and will update the state file to reflect. When terraform apply is executed, Terraform first refreshes its state by comparing it with the actual infrastructure. If the actual infrastructure already matches the desired state in the configuration (e.g., port is already 443), Terraform will detect no drift and make no…
Question
You have been working in a Cloud provider account that is shared with other team members. You previously used Terraform to create a load balancer that is listening on port 80. After some application changes, you updated the Terraform code to change the port to 443. You run terraform plan and see that the execution plan shows the port changing from 80 to 443 like you intended, and step away to grab some coffee. In the meantime, another team member manually changes the load balancer port to 443 through the Cloud provider console before you get back to your desk. What will happen when you terraform apply upon returning to your desk?
Options
- ATerraform will not make any changes to the Load Balancer and will update the state file to reflect
- BTerraform will change the port back to 80 in your code
- CTerraform will change the load balancer port to 80, and) then change it back to 443
- DTerraform will fail with in error because the state file is no longer accurate
How the community answered
(33 responses)- A76% (25)
- B3% (1)
- C6% (2)
- D15% (5)
Why each option
When `terraform apply` is executed, Terraform first refreshes its state by comparing it with the actual infrastructure. If the actual infrastructure already matches the desired state in the configuration (e.g., port is already 443), Terraform will detect no drift and make no changes to the resource.
Upon running `terraform apply`, Terraform first performs an implicit refresh. It compares its state file with the actual infrastructure in the cloud. Since the load balancer's port has already been manually changed to 443 in the cloud to match the desired state in the configuration, Terraform will detect no difference and will update its state file to reflect the current, correct attribute without performing any infrastructure modification.
Terraform does not modify your code; it modifies infrastructure to match your code.
Terraform will not change the port back to 80 because its implicit refresh will detect that the current state in the cloud already matches the desired state (443).
Terraform is designed to reconcile state discrepancies; it will detect the current state, recognize it matches the desired configuration, and update its state file without error, rather than failing.
Concept tested: Terraform state refresh and drift detection
Source: https://developer.hashicorp.com/terraform/language/state#inspecting-state-and-drift
Topics
Community Discussion
No community discussion yet for this question.