WebMagicInformatica
Terraform Associate (004) practice questions

Terraform Associate (004) practice questions: Terraform configuration

8 free questions on Terraform configuration, 28% of the HashiCorp Terraform Associate exam. Try each one, then open the answer and explanation. None of them are in the free readiness check, so you can still take it fresh.

Question 1

A variable instance_type is set in terraform.tfvars as t3.micro. The same run is started with -var="instance_type=t3.large". Which value does Terraform use?

  1. A.

    t3.large

  2. B.

    t3.micro, because terraform.tfvars always wins.

  3. C.

    The default value declared in the variable block.

  4. D.

    Terraform raises a conflict error.

Show answer and explanation

Answer: A

Values from -var and -var-file on the command line have the highest precedence, so they override terraform.tfvars and environment variables.

  • t3.micro, because terraform.tfvars always wins.: terraform.tfvars is loaded before command-line options.
  • The default value declared in the variable block.: Defaults are used only when no other value is supplied.
  • Terraform raises a conflict error.: Precedence resolves the conflict silently.
Question 2

An application needs an IAM policy to exist before it starts, but nothing in the instance block references the policy attributes. Which configuration makes Terraform create the policy first?

resource "aws_iam_role_policy" "app" { /* ... */ }
resource "aws_instance" "app" { /* ... */ }
  1. A.

    Add count = 1 to the policy resource.

  2. B.

    Add depends_on = [aws_iam_role_policy.app] to the instance resource.

  3. C.

    Put the instance block above the policy block in the file.

  4. D.

    Run terraform fmt.

Show answer and explanation

Answer: B

depends_on declares an explicit dependency when Terraform cannot infer it from references.

  • Add count = 1 to the policy resource.: Count does not influence ordering.
  • Put the instance block above the policy block in the file.: File order doesn't determine creation order.
  • Run terraform fmt.: Formatting doesn't affect the dependency graph.
Question 3

A configuration defines this local value. How do you use it elsewhere in the same module?

locals {
  name_prefix = "${var.project}-${var.environment}"
}
  1. A.

    local.name_prefix

  2. B.

    locals.name_prefix

  3. C.

    var.name_prefix

  4. D.

    output.name_prefix

Show answer and explanation

Answer: A

Local values are declared in a locals block (plural) and referenced as local.<name> (singular).

  • locals.name_prefix: The reference uses local, not locals.
  • var.name_prefix: var refers to input variables.
  • output.name_prefix: Outputs are not referenced with output. inside the same module.
Question 4

You want Terraform to check an assumption about its input, that the chosen AMI is for the x86_64 architecture, before it evaluates the instance resource, and stop with a clear message if the assumption is false.

resource "aws_instance" "app" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"

  lifecycle {
    ____ {
      condition     = data.aws_ami.ubuntu.architecture == "x86_64"
      error_message = "The AMI must be for x86_64."
    }
  }
}

What belongs in the blank?

  1. A.

    postcondition

  2. B.

    precondition

  3. C.

    validation

  4. D.

    depends_on

Show answer and explanation

Answer: B

A precondition in the lifecycle block is checked before Terraform evaluates the resource, which is how HashiCorp recommends you validate assumptions about inputs such as a data source result.

  • postcondition: A postcondition is checked after the resource is planned or applied and is meant to guarantee properties of the resource's own result (usually via self).
  • validation: validation blocks belong inside variable blocks, not in lifecycle.
  • depends_on: It declares dependencies and does not take a condition.
Question 5

Given the variable below, which expression reads the size for "small"?

variable "sizes" {
  type = map(number)
  default = {
    small = 10
    large = 100
  }
}
  1. A.

    var.sizes(small)

  2. B.

    var.sizes->small

  3. C.

    var.sizes["small"]

  4. D.

    sizes.var["small"]

Show answer and explanation

Answer: C

Map values are accessed with square brackets and the key, for example var.sizes["small"] (or var.sizes.small).

  • var.sizes(small): Parentheses are for function calls.
  • var.sizes->small: Terraform doesn't use this operator.
  • sizes.var["small"]: References start with var. for variables.
Question 6

What does templatefile do in this configuration?

resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = "t3.micro"
  user_data     = templatefile("${path.module}/init.tftpl", { name = var.site_name })
}
  1. A.

    It uploads the file to the instance with SSH.

  2. B.

    It validates the file as HCL.

  3. C.

    It copies the file into the state.

  4. D.

    It reads the template file and renders it, replacing placeholders with the supplied values.

Show answer and explanation

Answer: D

templatefile(path, vars) renders a template using the given variables, and path.module is the directory of the current module.

  • It uploads the file to the instance with SSH.: It only renders a string; provisioners or user data transport it.
  • It validates the file as HCL.: The file can be any text format.
  • It copies the file into the state.: The rendered result is used as an argument value.
Question 7

What is the difference between path.module and path.root in Terraform configuration?

  1. A.

    path.module is the filesystem path of the module containing the expression, and path.root is the path of the root module.

  2. B.

    Both always return the same value.

  3. C.

    path.module is the Terraform binary's directory.

  4. D.

    path.root is the cloud provider's API URL.

Show answer and explanation

Answer: A

path.module points to the current module's directory, while path.root points to the root module's directory, so a child module can load its own files reliably.

  • Both always return the same value.: They differ when used inside a child module.
  • path.module is the Terraform binary's directory.: It refers to the module directory.
  • path.root is the cloud provider's API URL.: It is a filesystem path.
Question 8

The plan shows id = (known after apply) for a new resource. What does this mean?

  1. A.

    The configuration has an error in that argument.

  2. B.

    The value is generated by the provider during apply, so it can't be known during planning.

  3. C.

    The value is sensitive and has been hidden.

  4. D.

    The value was lost from state.

Show answer and explanation

Answer: B

Some attributes, such as IDs assigned by the platform, are only known after the resource exists. Terraform marks them as unknown in the plan.

  • The configuration has an error in that argument.: Unknown values are normal.
  • The value is sensitive and has been hidden.: Sensitive values show (sensitive value).
  • The value was lost from state.: The resource isn't created yet.

How ready are you for Terraform Associate (004)?

Take the free readiness check: 15 questions from every exam domain in 20 minutes, with your score per domain.

  • 20 minutes
  • 15 questions
  • Free with an account

More Terraform Associate (004) practice by topic

Back to the Terraform Associate (004) practice test →

Independent practice material written from the official exam guide. Not affiliated with or endorsed by the exam provider.