Hashicorp Certified Terraform Associate 003 · Free Practice Question Medium
Question 13
As you are developing new Terraform code, you are finding that you are constantly repeating the same expression over and over throughout your code, and you are worried about the effort that will be needed if this expression needs to be changed. What feature of Terraform can you use to avoid repetition and make your code easier to maintain?
-
A
terraform graph -
B
remotebackend -
C
localsblock -
D
datablock
Reveal correct answer
Correct answer: C
Explanation
A local value assigns a name to an expression, so you can use it multiple times within a configuration without repeating it. The expressions in local values are not limited to literal constants; they can also reference other values in the configuration in order to transform or combine them, including variables, resource attributes, or other local values.
You can use local values to simplify your Terraform configuration and avoid repetition. Local values (locals) can also help you write a more readable configuration by using meaningful names rather than hard-coding values. If overused they can also make a configuration hard to read by future maintainers by hiding the actual values used.
Use local values only in moderation, in situations where a single value or result is used in many places and that value is likely to be changed in the future. The ability to easily change the value in a central place is the key advantage of local values.
https://developer.hashicorp.com/terraform/language/values/locals
A.
The terraform graph command in Terraform is used to generate a visual representation of the dependency graph of your infrastructure. While understanding the dependency graph is important for troubleshooting and optimizing your infrastructure, it does not directly help with avoiding repetition in your code. It is not the feature you would use to make your code easier to maintain by reducing duplication of expressions.
B.
The remote backend in Terraform is used to store the state file remotely, rather than locally on your machine. While the remote backend is important for state management and collaboration, it does not directly address the issue of repeating the same expression in your code. It is not the feature you would use to avoid repetition and improve code maintainability.
C.
Using locals in Terraform allows you to define reusable values or expressions within your configuration. By defining the repeated expression as a local variable, you can easily reference it throughout your code without having to repeat it multiple times. This not only reduces duplication but also makes your code more maintainable and easier to update if the expression needs to be changed in the future.
D.
The data block in Terraform is used to fetch and use data from outside sources, such as APIs or other Terraform configurations. While the data block is useful for integrating external data into your Terraform code, it does not specifically address the issue of repetition within your code. It is not the feature you would use to avoid repeating the same expression and improve code maintainability.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
