Terraform Associate · Domain 4 of 8 · 22% of the exam

Terraform Associate Domain 4: Terraform Configuration

45 free practice questions for Terraform Configuration in the HashiCorp Certified: Terraform Associate (004) exam. Try each question first, then open "Show answer" for the correct option and explanation.

Terraform Configuration practice questions

  1. Question 1. In the address aws_instance.web, what are aws_instance and web?

    1. The provider and the region
    2. The resource type and the local name
    3. The module and the output
    4. The workspace and the resource
    Show answer

    Answer: B. The resource type and the local name

    Resource addresses are <type>.<name>, and the pair must be unique within a module.

  2. Question 2. What is a data source used for?

    1. Creating resources outside of state
    2. Storing variables for a workspace
    3. Reading information about existing infrastructure
    4. Recording outputs for other teams
    Show answer

    Answer: C. Reading information about existing infrastructure

    A data block, such as data "aws_ami" "ubuntu", queries the provider at plan time and exposes attributes to use elsewhere.

  3. Question 3. How do you refer to the id attribute of the data source data "aws_ami" "ubuntu"?

    1. data.aws_ami.ubuntu.id
    2. aws_ami.ubuntu.id
    3. data["aws_ami"].ubuntu
    4. var.aws_ami.ubuntu.id
    Show answer

    Answer: A. data.aws_ami.ubuntu.id

    Data sources are addressed with the data. prefix, unlike managed resources.

  4. Question 4. Which block declares an input variable?

    1. input "region" {}
    2. param "region" {}
    3. locals { region }
    4. variable "region" {}
    Show answer

    Answer: D. variable "region" {}

    Variables can set a type, a default, a description, validation rules, and sensitive or ephemeral flags.

  5. Question 5. Several sources set the same variable. Which has the highest precedence?

    1. A TF_VAR_ environment variable
    2. -var and -var-file options on the command line
    3. terraform.tfvars
    4. The default value in the variable block
    Show answer

    Answer: B. -var and -var-file options on the command line

    Order, lowest to highest: environment variables, terraform.tfvars, terraform.tfvars.json, *.auto.tfvars in name order, then command-line options in the order given.

  6. Question 6. Apart from the default value, which variable source has the lowest precedence?

    1. TF_VAR_ environment variables
    2. terraform.tfvars
    3. *.auto.tfvars files
    4. -var on the command line
    Show answer

    Answer: A. TF_VAR_ environment variables

    Environment variables such as TF_VAR_region are overridden by any tfvars file or command-line option.

  7. Question 7. Which variable files does Terraform load automatically?

    1. Every .tfvars file in any subdirectory
    2. Only files passed with -var-file
    3. terraform.tfvars, terraform.tfvars.json, and *.auto.tfvars
    4. variables.tf only
    Show answer

    Answer: C. terraform.tfvars, terraform.tfvars.json, and *.auto.tfvars

    Other files, such as prod.tfvars, must be passed with -var-file.

  8. Question 8. A variable has no default and no value is supplied. What happens when you run terraform plan in a terminal?

    1. Terraform uses null
    2. Terraform prompts for the value
    3. Terraform skips resources that use it
    4. Terraform uses an empty string
    Show answer

    Answer: B. Terraform prompts for the value

    With -input=false, as in automation, Terraform fails instead of prompting.

  9. Question 9. Which block lets you reject variable values that don't meet a rule, with your own message?

    1. A validation block with condition and error_message
    2. A check block inside the provider
    3. A lifecycle block with prevent_destroy
    4. A precondition on the output
    Show answer

    Answer: A. A validation block with condition and error_message

    For example, condition = contains(["dev", "prod"], var.env). If it's false, plan fails with error_message.

  10. Question 10. How do you reference the variable region inside a configuration?

    1. variable.region
    2. ${region}
    3. input.region
    4. var.region
    Show answer

    Answer: D. var.region

    Variables use var., locals use local., and module outputs use module.<name>.<output>.

  11. Question 11. How does a root module expose a value, such as a load balancer's DNS name, after apply?

    1. Through a locals block
    2. Through a data block
    3. Through an output block
    4. Through a variable block
    Show answer

    Answer: C. Through an output block

    Outputs print after apply, can be read with terraform output, and are what other configurations can read through remote state.

  12. Question 12. What does sensitive = true on an output do?

    1. Encrypts the value in the state file
    2. Hides the value in CLI output, though it's still stored in state
    3. Prevents the value from being stored anywhere
    4. Removes the output from HCP Terraform
    Show answer

    Answer: B. Hides the value in CLI output, though it's still stored in state

    Sensitive values are shown as (sensitive value). Protect the state, because the real value is still there.

  13. Question 13. A script needs the plain string value of output db_host. Which command prints it without quotes?

    1. terraform output -raw db_host
    2. terraform show db_host
    3. terraform output -json
    4. terraform state show db_host
    Show answer

    Answer: A. terraform output -raw db_host

    -raw prints a string, number, or bool as is. -json prints every output in JSON.

  14. Question 14. What is a local value?

    1. A variable that can be set on the command line
    2. A value stored only on the local backend
    3. An output read by other configurations
    4. A named expression reused within a module, referenced as local.name
    Show answer

    Answer: D. A named expression reused within a module, referenced as local.name

    Locals avoid repeating the same expression, for example a common set of tags.

  15. Question 15. What is the difference between list(string) and set(string)?

    1. A set keeps its elements in the order they were added
    2. A list can't contain the same value more than once
    3. A set holds unique, unordered values; a list is ordered and allows duplicates
    4. They are the same type with two different names
    Show answer

    Answer: C. A set holds unique, unordered values; a list is ordered and allows duplicates

    Sets are common with for_each, which needs unique keys. toset() converts a list into a set.

  16. Question 16. Which type constraint describes a value with a name that is a string and a port that is a number?

    1. object({ name = string, port = number })
    2. map(any) with name and port keys
    3. tuple([string, string])
    4. list(object) of name and port
    Show answer

    Answer: A. object({ name = string, port = number })

    Object types define named attributes with their own types. Attributes can be made optional with optional().

  17. Question 17. What is a tuple type?

    1. A collection of key-value pairs with string keys
    2. A sequence of fixed length in which each element can have its own type
    3. A set of unique strings
    4. Another name for a map of numbers
    Show answer

    Answer: B. A sequence of fixed length in which each element can have its own type

    For example, tuple([string, number, bool]). Lists, in contrast, have one element type.

  18. Question 18. What does the type map(string) mean?

    1. A list of string values
    2. An object with one string attribute
    3. A string that holds a JSON map
    4. String keys, each with a string value
    Show answer

    Answer: D. String keys, each with a string value

    All values in a map share one type. Use an object type when attributes have different types.

  19. Question 19. A resource sets count = 3. How does each instance get a unique name such as web-0, web-1, and web-2?

    1. Using each.key
    2. Using count.index
    3. Using self.index
    4. Using var.count
    Show answer

    Answer: B. Using count.index

    count.index is zero-based. Instances are addressed as aws_instance.web[0] and so on.

  20. Question 20. Which value types can for_each take?

    1. A map or a set of strings
    2. Any list, including ones with duplicates
    3. A single number
    4. Only a tuple
    Show answer

    Answer: A. A map or a set of strings

    Lists must be converted with toset() first, because each instance needs a unique, known key.

  21. Question 21. Why use for_each instead of count for a collection of named items?

    1. for_each creates all of the resources faster
    2. count can't be used together with modules
    3. Removing one item doesn't shift the addresses of the others
    4. for_each instances don't need to be stored in state
    Show answer

    Answer: C. Removing one item doesn't shift the addresses of the others

    With count, deleting the first item renumbers the rest and can cause replacements. for_each addresses instances by key.

  22. Question 22. In a resource using for_each over a map, what do each.key and each.value refer to?

    1. The resource type and its name
    2. The current map key and its value
    3. The provider and its alias
    4. The first and last items of the map
    Show answer

    Answer: B. The current map key and its value

    For a set, each.key and each.value are the same element.

  23. Question 23. How is the instance for key "logs" of a for_each resource aws_s3_bucket.b addressed?

    1. aws_s3_bucket.b.logs
    2. aws_s3_bucket.b[0]
    3. aws_s3_bucket["logs"].b
    4. aws_s3_bucket.b["logs"]
    Show answer

    Answer: D. aws_s3_bucket.b["logs"]

    Quote string keys inside the brackets, for example in terraform state show 'aws_s3_bucket.b["logs"]'.

  24. Question 24. Can a single resource block use both count and for_each?

    1. No, a resource can use only one of them
    2. Yes, count is applied first
    3. Yes, if the counts match
    4. Only inside a module
    Show answer

    Answer: A. No, a resource can use only one of them

    They're mutually exclusive. Pick count for identical copies and for_each for keyed items.

  25. Question 25. What is a dynamic block for?

    1. Loading a configuration file at run time
    2. Changing providers during an apply
    3. Generating repeated nested blocks from a collection
    4. Creating resources only when a condition is true
    Show answer

    Answer: C. Generating repeated nested blocks from a collection

    For example, one ingress nested block per item in var.rules. Overusing them can make code hard to read.

  26. Question 26. With count set on aws_instance.web, what does aws_instance.web[*].id return?

    1. The id of the first instance only
    2. A list of the ids of all instances
    3. A map of instance names to ids
    4. The number of instances
    Show answer

    Answer: B. A list of the ids of all instances

    The splat expression [*] collects one attribute from every element.

  27. Question 27. Which expression builds a new list containing each element of var.names in upper case?

    1. upper(var.names)
    2. var.names.map(upper)
    3. for_each var.names : upper
    4. [for n in var.names : upper(n)]
    Show answer

    Answer: D. [for n in var.names : upper(n)]

    Use square brackets for a list result and curly braces with => for a map result. An if clause filters elements.

  28. Question 28. Which expression sets 3 replicas in prod and 1 everywhere else?

    1. var.env == "prod" ? 3 : 1
    2. if var.env == "prod" then 3 else 1
    3. var.env.prod(3, 1)
    4. switch(var.env, "prod", 3, 1)
    Show answer

    Answer: A. var.env == "prod" ? 3 : 1

    HCL uses the condition ? true_value : false_value form. Both results must be convertible to the same type.

  29. Question 29. Which expression produces a string such as myapp-bucket from var.name?

    1. var.name + "-bucket"
    2. concat(var.name, "-bucket")
    3. "${var.name}-bucket"
    4. 'var.name'-bucket
    Show answer

    Answer: C. "${var.name}-bucket"

    String templates use ${...} for interpolation. concat() joins lists, not strings.

  30. Question 30. Can you write your own functions in Terraform configuration?

    1. Yes, by declaring them in a function block
    2. No, but you can use built-in and provider-defined functions
    3. Yes, by loading JavaScript files at plan time
    4. Only inside child modules, not the root module
    Show answer

    Answer: B. No, but you can use built-in and provider-defined functions

    Terraform includes many built-in functions, and providers can add their own, called as provider::name::function().

  31. Question 31. What does merge(map1, map2) return when both maps have the same key?

    1. An error, since keys must be unique
    2. A list of both values
    3. The value from map1
    4. The value from map2, since later arguments win
    Show answer

    Answer: D. The value from map2, since later arguments win

    merge is often used to combine default tags with resource-specific tags.

  32. Question 32. What does lookup(var.amis, var.region, "ami-default") do?

    1. Returns the region's value, or the default if the key is missing
    2. Searches every configured provider for a matching image
    3. Fails unless var.region is a number that indexes the map
    4. Returns a list of every key that var.amis contains
    Show answer

    Answer: A. Returns the region's value, or the default if the key is missing

    For maps you can also write var.amis[var.region], which fails if the key doesn't exist.

  33. Question 33. Which function renders a file with variables, for example to build a startup script?

    1. file()
    2. jsonencode()
    3. templatefile()
    4. format()
    Show answer

    Answer: C. templatefile()

    templatefile(path, vars) evaluates the file as a template. file() reads it unchanged.

  34. Question 34. What does try(var.settings.port, 8080) return if var.settings has no port attribute?

    1. An error
    2. 8080
    3. null
    4. 0
    Show answer

    Answer: B. 8080

    try returns the first argument that evaluates without an error. can() returns true or false instead.

  35. Question 35. How does Terraform usually learn that one resource depends on another?

    1. From the order of blocks in the file
    2. From alphabetical resource names
    3. From comments in the code
    4. From expressions that reference the other resource
    Show answer

    Answer: D. From expressions that reference the other resource

    Referencing aws_vpc.main.id creates an implicit dependency, so the VPC is created first.

  36. Question 36. When should you add depends_on to a resource?

    1. When a hidden dependency exists that references don't show
    2. On every resource that uses a variable
    3. To make apply run faster
    4. To choose a provider alias
    Show answer

    Answer: A. When a hidden dependency exists that references don't show

    For example, when an app needs an IAM policy attached before it starts but doesn't reference the policy.

  37. Question 37. A resource must be replaced, but the old one can't be deleted until the new one exists. Which lifecycle setting does this?

    1. prevent_destroy = true
    2. ignore_changes = all
    3. create_before_destroy = true
    4. replace_triggered_by = []
    Show answer

    Answer: C. create_before_destroy = true

    By default Terraform destroys first. create_before_destroy reverses the order, which needs names that won't clash.

  38. Question 38. What does lifecycle { prevent_destroy = true } do?

    1. Hides the resource from plans
    2. Makes any plan that would destroy the resource fail
    3. Blocks changes made in the cloud console
    4. Removes the resource from state
    Show answer

    Answer: B. Makes any plan that would destroy the resource fail

    It protects critical resources such as databases from accidental deletion. It doesn't stop deletion if the block is removed from configuration.

  39. Question 39. A tag is changed by an external system, and Terraform keeps trying to revert it. Which setting stops that?

    1. create_before_destroy for that resource
    2. A depends_on on the tagging system
    3. prevent_destroy on the tag
    4. ignore_changes listing the tags attribute
    Show answer

    Answer: D. ignore_changes listing the tags attribute

    ignore_changes tells Terraform not to plan updates for those attributes after creation.

  40. Question 40. What does replace_triggered_by do?

    1. Replaces the resource when a referenced resource or attribute changes
    2. Replaces the provider configuration in the middle of an apply
    3. Prevents the resource from being replaced for any change
    4. Triggers a new speculative plan in HCP Terraform
    Show answer

    Answer: A. Replaces the resource when a referenced resource or attribute changes

    For example, recreate an instance whenever a launch template changes, even if the instance's own arguments didn't.

  41. Question 41. What is a postcondition block used for?

    1. Running a shell script on the machine after apply
    2. Setting default values for a module's input variables
    3. Checking a guarantee about a resource after it's created or read
    4. Choosing the order in which outputs are displayed
    Show answer

    Answer: C. Checking a guarantee about a resource after it's created or read

    Postconditions can use self to check attributes. If one fails, Terraform reports an error so later resources don't build on bad data.

  42. Question 42. How do check blocks differ from preconditions and postconditions?

    1. They stop the apply when they fail
    2. They report warnings without blocking plan or apply
    3. They only run in HCP Terraform
    4. They replace variable validation
    Show answer

    Answer: B. They report warnings without blocking plan or apply

    Checks validate the infrastructure as a whole, for example that a website returns 200, and show a warning if the assertion fails.

  43. Question 43. What does sensitive = true on an input variable do?

    1. Stores the value encrypted in state
    2. Prevents the value from being passed to providers
    3. Requires the value to come from a vault
    4. Redacts the value in plan and apply output
    Show answer

    Answer: D. Redacts the value in plan and apply output

    Sensitivity propagates to expressions that use the value, but the value can still be stored in state.

  44. Question 44. What does ephemeral = true on an input variable do?

    1. Keeps the value out of plan and state files
    2. Deletes the variable after one run
    3. Makes the value visible only in the console
    4. Stores the value in HCP Terraform only
    Show answer

    Answer: A. Keeps the value out of plan and state files

    Ephemeral values exist only during a run, so they can be used only in places that are also not persisted, such as provider configuration or write-only arguments.

  45. Question 45. What is a write-only argument, such as password_wo on a database resource?

    1. An argument that can only be set once at creation
    2. An argument whose value is read from a file
    3. An argument sent at apply time but never stored in plan or state
    4. An argument that only HCP Terraform can set
    Show answer

    Answer: C. An argument sent at apply time but never stored in plan or state

    Write-only arguments accept ephemeral values. Because Terraform can't see a stored value, a companion version argument is usually changed to trigger an update.

HashiCorp, Terraform, and HCP Terraform are trademarks of HashiCorp, Inc. These questions are original study material, not questions from the real exam.