HashiCorp Certified: Terraform Associate (004) · Domain 8 · 12% of exam

HCP Terraform

Drill 19 practice questions focused entirely on HCP Terraform for the HashiCorp Terraform Associate exam. Tap an answer for instant feedback and a full explanation — no sign-up, always free.

Verified answer19 questions
Question 1 of 19

A team wants an output that displays a custom DNS name when the variable custom_dns is set to a non-empty string, but otherwise falls back to the auto-generated endpoint from an AWS resource attribute. They want to avoid nested conditional expressions. Which built-in function best returns the first non-empty value from a list of arguments?

Reviewed for accuracy · Report an issue
Question 2 of 19

A team wants an aws_cloudwatch_metric_alarm to be created only when a boolean variable named enable_monitoring is set to true. When enable_monitoring is false, no alarm resource should exist. Which configuration correctly implements this toggle using the count meta-argument?

Reviewed for accuracy · Report an issue
Question 3 of 19

You are writing a security group resource that must produce a variable number of nested `ingress` blocks based on a list of port objects passed as `var.ingress_rules` (each with `port` and `cidr` attributes). You want to avoid hardcoding repeated `ingress { ... }` blocks. Which construct correctly generates the nested blocks?

Reviewed for accuracy · Report an issue
Question 4 of 19

You are writing a module that creates 6 EC2 instances using count, and you want to distribute them across a list of 3 availability-zone subnet IDs stored in var.subnet_ids. You need each instance's subnet_id to cycle through the 3 subnets (instance 0 → subnet 0, instance 3 → subnet 0 again, etc.) without going out of bounds. Which expression should you assign to subnet_id?

Reviewed for accuracy · Report an issue
Question 5 of 19

A teammate wrote the following resource block and Terraform returns an error during plan: ```hcl variable "buckets" { type = list(string) default = ["logs", "backups", "assets"] } resource "aws_s3_bucket" "this" { for_each = var.buckets bucket = each.value } ``` The error states the `for_each` argument must be a map or a set of strings, not a list. Which change fixes the configuration while keeping the same input variable?

Reviewed for accuracy · Report an issue
Question 6 of 19

A platform engineer wants to guarantee that an AWS S3 bucket created by Terraform always has versioning explicitly enabled before the apply is considered valid. They want the apply to fail with a clear error if the resulting resource does not meet this requirement, evaluated against the resource's own attributes after planning. Which Terraform feature should they use?

Reviewed for accuracy · Report an issue
Question 7 of 19

You are given a variable named subnet_names of type list(string) containing three unique subnet names. You want to create one aws_subnet resource per name using for_each so that resources are keyed by name rather than by numeric index. Which configuration correctly uses for_each with this list variable?

Reviewed for accuracy · Report an issue
Question 8 of 19

You are creating multiple S3 buckets from a map variable that pairs a logical name with an environment tag. Inside a resource block using for_each over this map, you need to set the bucket name from the map key and apply the environment as a tag from the map value. Which pair of references correctly retrieves the key and value for each iteration?

Reviewed for accuracy · Report an issue
Question 9 of 19

You maintain a list of instance objects and need an output that contains only the private IP addresses of instances whose 'role' attribute equals "web". The variable is defined as a list of objects, each with 'role' and 'private_ip' attributes. Which expression produces the desired filtered list?

Reviewed for accuracy · Report an issue
Question 10 of 19

You have a variable `environments = ["dev", "staging", "prod"]` and need to produce a local value that is a map where each key is the environment name and each value is the environment name converted to uppercase (e.g., {"dev" = "DEV"}). Which local expression produces this result?

Reviewed for accuracy · Report an issue
Question 11 of 19

You are defining an AWS IAM policy inline in Terraform. Rather than hardcode a JSON string, you want to build the policy from HCL data structures so it stays readable and can reference other resource attributes. Which built-in function should you use to convert an HCL object into the JSON string the policy argument expects?

Reviewed for accuracy · Report an issue
Question 12 of 19

Your team manages a production launch configuration whose attribute changes force Terraform to replace the resource. During recent applies, Terraform destroyed the old resource before creating the replacement, causing a brief service outage because a dependent autoscaling group had no valid launch configuration during the gap. Which lifecycle setting should you add to the resource block to eliminate this downtime?

Reviewed for accuracy · Report an issue
Question 13 of 19

An operations team manages an AWS instance with Terraform. A separate cost-tracking automation tool continuously adds and updates a 'CostCenter' tag on the instance out-of-band. Every time the team runs 'terraform apply', Terraform tries to remove that tag to match the configuration. The team wants Terraform to stop reverting the externally-managed tag while still managing all other attributes of the instance. Which configuration change accomplishes this?

Reviewed for accuracy · Report an issue
Question 14 of 19

Your team manages a production RDS database with Terraform. During a routine refactor, a colleague accidentally removed the database's configuration, and a subsequent 'terraform apply' nearly destroyed the instance. You want Terraform to hard-fail any plan that would delete this specific database, forcing engineers to consciously edit the configuration before removal. Which lifecycle setting achieves this?

Reviewed for accuracy · Report an issue
Question 15 of 19

You provisioned multiple EC2 instances using a single resource block with count = 3. In an output block, you want to expose a list containing the private IP address of every instance created. Which expression produces this list most concisely?

Reviewed for accuracy · Report an issue
Question 16 of 19

You are refactoring a Terraform configuration and want to verify how a complex expression using cidrsubnet() and element() will evaluate against your variable values before running a plan. You need an interactive way to test the expression without modifying any resources or state. Which approach lets you evaluate the expression against your current variables and state?

Reviewed for accuracy · Report an issue
Question 17 of 19

A team member defines the following in their configuration: variable "environment" { default = "prod" } locals { base_tags = { Owner = "platform" Managed = "terraform" } final_tags = merge(local.base_tags, { Environment = var.environment }) } What is the resulting value of local.final_tags?

Reviewed for accuracy · Report an issue
Question 18 of 19

A developer writes a local value to fetch a specific subnet CIDR by position: local.cidrs = element(var.subnet_cidrs, 0). The variable subnet_cidrs is declared as type set(string). After running terraform plan, Terraform returns an error indicating the argument type is invalid for the element function. What is the correct explanation and fix?

Reviewed for accuracy · Report an issue
Question 19 of 19

You are writing a module that reads an optional nested attribute from a variable named var.config, which may or may not contain a 'network' key with a 'subnet_id' sub-key. If the attribute path does not exist, evaluating it directly would raise an error. You want the local value to fall back to the string "default-subnet" whenever the lookup fails. Which expression should you use in the locals block?

Reviewed for accuracy · Report an issue

More Terraform Associate practice

Keep going with the other HashiCorp Certified: Terraform Associate (004) domains, or take a full timed mock exam.

← Back to Terraform Associate overview