HashiCorp Terraform Associate (004) exam dumps

HashiCorp Terraform Associate (004) practice question 123 of 223

Terraform Associate 004. Associate level, HashiCorp. Free question with the correct answer and a full explanation.

HashiCorp Terraform Associate (004) Question 123

Single answer4g Validate configuration using custom conditions

A platform team maintains a reusable Terraform module that creates an AWS security group. They want to prevent unsafe input before any resources are created. The module currently accepts a variable named ingress_cidrs as list(string). The team wants Terraform to fail during planning if any CIDR in the list is not a valid IPv4 CIDR block or if the list is empty. Which implementation best meets this requirement using Terraform custom conditions?

  1. A

    Add a validation block to the ingress_cidrs variable that checks length(var.ingress_cidrs) > 0 && alltrue([for c in var.ingress_cidrs : can(cidrhost(c, 0))]).

  2. B

    Add a precondition block inside the aws_security_group resource that checks each CIDR with cidrhost, because preconditions are evaluated before variable values are accepted.

  3. C

    Add a postcondition block to the output that returns the security group ID, because postconditions stop invalid input before planning continues.

  4. D

    Add nullable = false to the variable and rely on the type constraint list(string) to reject invalid CIDR syntax and empty lists.

Show answer and explanation

Correct answer: A

Explanation

Terraform supports custom conditions in several places, including variable validation blocks, preconditions, and postconditions. For this scenario, variable validation is the best choice because the goal is to validate user-supplied input to a module before any resources are processed. A common and effective pattern is to combine length(...) > 0 with alltrue(...) over a for expression and can(cidrhost(...)) to verify CIDR syntax. HashiCorp documentation describes input variable validation as the mechanism for enforcing requirements on variable values, while preconditions and postconditions are intended for assumptions and guarantees around resources, data sources, and outputs rather than primary input validation.

  • A. Correct.

    Correct. A variable validation block is the appropriate custom condition when you want to reject bad module input as early as possible. length(var.ingress_cidrs) > 0 ensures the list is not empty, and alltrue([for c in var.ingress_cidrs : can(cidrhost(c, 0))]) verifies every element can be parsed as a CIDR. cidrhost raises an error for invalid CIDR notation, and wrapping it with can(...) converts that into false for validation logic. This is a practical pattern for input validation in Terraform.

  • B. Incorrect.

    Incorrect. A precondition can validate assumptions for a resource, data source, or output, but it is not the best fit here because the requirement is specifically to validate module input before resource creation based on the variable itself. Preconditions are useful, but variable validation is the intended mechanism for checking input values directly. The statement that preconditions are evaluated before variable values are accepted is also misleading.

  • C. Incorrect.

    Incorrect. A postcondition is evaluated after Terraform has planned or applied an object and is used to verify the resulting object state, not to validate raw input before planning proceeds. Attaching a postcondition to an output would be too late for this use case and would not be the appropriate way to reject bad input to a module.

  • D. Incorrect.

    Incorrect. nullable = false only prevents the entire variable from being null; it does not enforce that the list contains at least one element. Likewise, the type constraint list(string) ensures only that the value is a list of strings, not that each string is a valid CIDR. This is a common misconception: type constraints do not perform semantic validation of string contents.

Timed practice exam

Take a HashiCorp Terraform Associate (004) practice test under exam conditions

70 questions in 60 minutes, drawn from this bank, with a score report and a per-question review when you finish.

Start timed exam