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
-
Question 1. In the address aws_instance.web, what are aws_instance and web?
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.
-
Question 2. What is a data source used for?
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.
-
Question 3. How do you refer to the id attribute of the data source data "aws_ami" "ubuntu"?
Show answer
Answer: A. data.aws_ami.ubuntu.id
Data sources are addressed with the data. prefix, unlike managed resources.
-
Question 4. Which block declares an input variable?
Show answer
Answer: D. variable "region" {}
Variables can set a type, a default, a description, validation rules, and sensitive or ephemeral flags.
-
Question 5. Several sources set the same variable. Which has the highest precedence?
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.
-
Question 6. Apart from the default value, which variable source has the lowest precedence?
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.
-
Question 7. Which variable files does Terraform load automatically?
Show answer
Answer: C. terraform.tfvars, terraform.tfvars.json, and *.auto.tfvars
Other files, such as prod.tfvars, must be passed with -var-file.
-
Question 8. A variable has no default and no value is supplied. What happens when you run terraform plan in a terminal?
Show answer
Answer: B. Terraform prompts for the value
With -input=false, as in automation, Terraform fails instead of prompting.
-
Question 9. Which block lets you reject variable values that don't meet a rule, with your own message?
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.
-
Question 10. How do you reference the variable region inside a configuration?
Show answer
Answer: D. var.region
Variables use var., locals use local., and module outputs use module.<name>.<output>.
-
Question 11. How does a root module expose a value, such as a load balancer's DNS name, after apply?
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.
-
Question 12. What does sensitive = true on an output do?
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.
-
Question 13. A script needs the plain string value of output db_host. Which command prints it without quotes?
Show answer
Answer: A. terraform output -raw db_host
-raw prints a string, number, or bool as is. -json prints every output in JSON.
-
Question 14. What is a local value?
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.
-
Question 15. What is the difference between list(string) and set(string)?
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.
-
Question 16. Which type constraint describes a value with a name that is a string and a port that is a number?
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().
-
Question 17. What is a tuple type?
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.
-
Question 18. What does the type map(string) mean?
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.
-
Question 19. A resource sets count = 3. How does each instance get a unique name such as web-0, web-1, and web-2?
Show answer
Answer: B. Using count.index
count.index is zero-based. Instances are addressed as aws_instance.web[0] and so on.
-
Question 20. Which value types can for_each take?
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.
-
Question 21. Why use for_each instead of count for a collection of named items?
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.
-
Question 22. In a resource using for_each over a map, what do each.key and each.value refer to?
Show answer
Answer: B. The current map key and its value
For a set, each.key and each.value are the same element.
-
Question 23. How is the instance for key "logs" of a for_each resource aws_s3_bucket.b addressed?
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"]'.
-
Question 24. Can a single resource block use both count and for_each?
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.
-
Question 25. What is a dynamic block for?
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.
-
Question 26. With count set on aws_instance.web, what does aws_instance.web[*].id return?
Show answer
Answer: B. A list of the ids of all instances
The splat expression [*] collects one attribute from every element.
-
Question 27. Which expression builds a new list containing each element of var.names in upper case?
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.
-
Question 28. Which expression sets 3 replicas in prod and 1 everywhere else?
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.
-
Question 29. Which expression produces a string such as myapp-bucket from var.name?
Show answer
Answer: C. "${var.name}-bucket"
String templates use ${...} for interpolation. concat() joins lists, not strings.
-
Question 30. Can you write your own functions in Terraform configuration?
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().
-
Question 31. What does merge(map1, map2) return when both maps have the same key?
Show answer
Answer: D. The value from map2, since later arguments win
merge is often used to combine default tags with resource-specific tags.
-
Question 32. What does lookup(var.amis, var.region, "ami-default") do?
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.
-
Question 33. Which function renders a file with variables, for example to build a startup script?
Show answer
Answer: C. templatefile()
templatefile(path, vars) evaluates the file as a template. file() reads it unchanged.
-
Question 34. What does try(var.settings.port, 8080) return if var.settings has no port attribute?
Show answer
Answer: B. 8080
try returns the first argument that evaluates without an error. can() returns true or false instead.
-
Question 35. How does Terraform usually learn that one resource depends on another?
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.
-
Question 36. When should you add depends_on to a resource?
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.
-
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?
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.
-
Question 38. What does lifecycle { prevent_destroy = true } do?
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.
-
Question 39. A tag is changed by an external system, and Terraform keeps trying to revert it. Which setting stops that?
Show answer
Answer: D. ignore_changes listing the tags attribute
ignore_changes tells Terraform not to plan updates for those attributes after creation.
-
Question 40. What does replace_triggered_by do?
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.
-
Question 41. What is a postcondition block used for?
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.
-
Question 42. How do check blocks differ from preconditions and postconditions?
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.
-
Question 43. What does sensitive = true on an input variable do?
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.
-
Question 44. What does ephemeral = true on an input variable do?
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.
-
Question 45. What is a write-only argument, such as password_wo on a database resource?
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.