200-301 Question 499
Single answerYou are a network administrator tasked with automating the deployment of network infrastructure on a cloud platform using Terraform. During the deployment, you notice that a configuration file fails to execute because a required variable is missing. What is the best way to handle this issue in Terraform?
- A
Declare the variable in the Terraform configuration file and provide a default value.
- B
Hardcode the variable value in the Terraform provider block to avoid missing it again.
- C
Manually input the variable value during the Terraform 'apply' step every time.
- D
Use the '-var-file' option to specify the variable values in a separate file during deployment.
Show answer and explanation
Correct answer: A
Explanation
The best practice when handling missing variables in Terraform is to declare them in the configuration file and provide default values. This ensures that the configuration remains flexible and prevents deployment failures due to undefined variables. Hardcoding values or relying on manual input is not recommended for scalability and automation purposes. While '-var-file' is a useful method for organizing variable values, it does not address the problem of missing variables directly.
- A. Correct.
Declaring the variable in the Terraform configuration file and providing a default value ensures that the variable is always defined, even if the user does not explicitly provide a value. This is the best practice for handling missing variables.
- B. Incorrect.
Hardcoding variable values in the Terraform provider block is not recommended as it reduces flexibility and defeats the purpose of using variables for dynamic configurations.
- C. Incorrect.
Manually inputting the variable value during the 'apply' step every time is not efficient and can lead to human error, especially in automated environments.
- D. Incorrect.
Using the '-var-file' option is a valid approach for specifying variable values, but it is not the best solution for handling missing variables since it requires an additional file every time.