You want to create a string that is a combination of a generated random_id and a variable, and reuse that string several times in your configuration.
What is the simplest correct way to implement this without repeating the random_id and variable?
Correct Answer: C
Detailed Explanation:
* Rationale for Correct answer: A local value in Terraform allows you to define reusable expressions within a module. By combining the random_id and a variable into a local value, you can reference it multiple times without repeating the expression. This improves readability and maintainability, which aligns with Terraform best practices for configuration design.
Example:
locals {
combined_name = " ${random_id.example.hex}-${var.name} "
}
You can then reuse local.combined_name throughout your configuration.
* Analysis of Incorrect Options (Distractors):
* A. Use a data source. Incorrect because data sources are used to fetch information from existing infrastructure, not to define reusable expressions.
* B. Use a module. Incorrect because modules are used for grouping and reusing infrastructure components, not for simple expression reuse.
* D. Add an output value. Incorrect because outputs are used to expose values outside a module, not for internal reuse within the same configuration.
* Key Concept: Local values ( locals ) are used to simplify and reuse expressions within a Terraform configuration.
Reference: Terraform Objective Domain: Read, Generate, and Modify Configurations