Which Terraform feature would let you tag multiple resources with a string composed from a generated random_id and a variable without repeating them in each resource?

Prepare for the HashiCorp Terraform Associate Exam. Utilize flashcards and multiple-choice questions with detailed hints and explanations. Boost your confidence and be ready for success!

Multiple Choice

Which Terraform feature would let you tag multiple resources with a string composed from a generated random_id and a variable without repeating them in each resource?

Explanation:
Local values let you compute a value once and reuse it across many resources. By building the tag string from the generated random_id and a variable in a single local, you can reference that same local in every resource’s tags block. This keeps the tagging consistent without duplicating the concatenation logic in each resource. For example, you could define: locals { shared_tag = "${random_id.example_id.id}-${var.environment}" } And then use the local in multiple resources: tags = { Name = local.shared_tag Environment = var.environment } This approach ensures a single source of truth for the composed tag value. Data sources read existing data, outputs expose values to callers, and modules could encapsulate tagging logic, but locals are the simplest and most direct way to reuse a computed value across resources within the same configuration.

Local values let you compute a value once and reuse it across many resources. By building the tag string from the generated random_id and a variable in a single local, you can reference that same local in every resource’s tags block. This keeps the tagging consistent without duplicating the concatenation logic in each resource.

For example, you could define:

locals {

shared_tag = "${random_id.example_id.id}-${var.environment}"

}

And then use the local in multiple resources:

tags = {

Name = local.shared_tag

Environment = var.environment

}

This approach ensures a single source of truth for the composed tag value. Data sources read existing data, outputs expose values to callers, and modules could encapsulate tagging logic, but locals are the simplest and most direct way to reuse a computed value across resources within the same configuration.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy