Courseiva

AWS provider · domain

Dynamic HCL Configuration

Dynamic HCL Configuration covers expressions, variables, validation, and dynamic blocks in Terraform. Questions present real HCL snippets and ask you to choose correct syntax, predict plan/apply behavior, or fix errors. You must reason about type constraints, for expressions, validation rules, and nested dynamic blocks using the AWS provider.

44 questions14 easy19 medium11 hard

Focused practice

Practice Dynamic HCL Configuration questions

Scored sessions drawing only from this domain — pick a length below.

Start 20-question practice test →

What this domain covers

What to know about Dynamic HCL Configuration

You must read and write HCL that passes typed objects to modules, validates inputs with condition and error_message, transforms collections with for expressions, and generates nested blocks dynamically. The critical skill is matching type constraints and validation conditions to the actual data shape.

Defining complex object type constraints in variable blocks with optional() and required attributes

Using for expressions to transform lists, maps, and sets into new collections

Writing variable validation blocks with condition and error_message to reject invalid plans

Building dynamic blocks that generate aws_security_group ingress rules from list objects

Watch out for

Common Dynamic HCL Configuration exam traps

  • ▸Confusing for expressions with for_each meta-argument, producing invalid syntax or wrong iteration behavior
  • ▸Omitting optional() in object type constraints, causing errors when callers pass partial objects
  • ▸Placing validation in the wrong block or using a condition that references unavailable values at plan time

Question index

All Dynamic HCL Configuration questions (44)

Click any question to see the full explanation, or start a practice session above.

1

You are defining an input variable for a Terraform module that will accept a list of AWS subnet IDs. You want to ensure that the variable only accepts a list of strings and that it is required. Which variable declaration correctly achieves this?

Easy
2

You are authoring a Terraform module that must accept a map of environment names to instance counts. The map is provided by callers and may contain up to 20 entries. You need to derive a flat list of instance names in the form "<env>-<index>" (e.g., "prod-0", "prod-1") so that a downstream resource can use `for_each` with stable keys. Which expression correctly produces this list?

Medium
3

What happens if you use a dynamic block with a 'for_each' expression that evaluates to an empty set?

Hard
4

In a dynamic block, you need to access the current iteration item. What is the default label assigned to the iterator object?

Medium
5

You are writing a Terraform configuration that reads a list of environment names from a variable and creates one aws_s3_bucket for each environment. The order of the list may change over time, and you want to avoid unnecessary bucket replacement. Which configuration snippet achieves this with stable resource addressing?

Hard
6

You want to optionally create an S3 bucket only if a specific variable 'create_bucket' is set to true. Which HCL feature is best for this?

Easy
7

Which TWO of the following are valid uses of the 'for' expression?

Medium
8

You are writing a Terraform configuration that uses a conditional expression to set the instance type for an AWS EC2 instance. The condition checks if the environment is production. The true branch returns a string, but the false branch returns a number. What will happen when you run terraform plan?

Easy
9

Which THREE of the following are valid ways to transform data for Terraform configuration?

Medium
10

You have a Terraform variable defined as a list of objects, each with a name and a list of ports. You need to create AWS security group rules for each port across all objects. Which expression correctly generates a flattened list of objects containing the name and port for each combination?

Medium
11

You are writing a Terraform configuration that creates multiple AWS S3 buckets. The bucket names are derived from a list of project names, but you need to ensure that the names are unique and that the configuration can be safely modified without affecting existing buckets. Which approach should you use?

Hard
12

You are authoring a Terraform module that creates an AWS IAM policy. The policy's Statement blocks must be generated from a list of objects, but each Statement must also conditionally include a Condition block only when the object's condition attribute is non-null. Which approach correctly generates the Statement blocks while conditionally including the nested Condition block?

Medium
13

Which TWO of the following functions are most useful for manipulating lists?

Easy
14

You are authoring a Terraform configuration that provisions an aws_autoscaling_group. The desired instance count is stored in a variable named instance_count, and you need to create exactly that many aws_instance resources. Which approach correctly creates the required number of instances while keeping the configuration valid?

Medium
15

Which THREE of the following represent potential pitfalls when using 'count'?

Hard
16

You are writing a Terraform configuration that uses a for expression to transform a map of objects into a new map. The original map has keys that are strings, and each object contains an attribute 'enabled' of type bool. You need to create a new map that includes only the entries where 'enabled' is true, and the new map's keys should be the original keys prefixed with 'prod-'. Which for expression achieves this?

Hard
17

Which of these is a valid use case for a dynamic block?

Medium
18

You are writing a Terraform configuration that uses a `dynamic` block to generate multiple `ingress` blocks inside an `aws_security_group` resource. The `var.ingress_rules` variable is a list of objects with `from_port`, `to_port`, and `cidr_blocks`. You need to reference the current rule inside the dynamic block's content. Which iterator configuration and reference is correct?

Medium
19

Which TWO of the following statements are correct regarding 'for_each' and state?

Hard
20

You are using a for expression to create a map from a list of objects. Each object has an attribute 'name' that is a string. You want the resulting map's keys to be the 'name' values and the values to be the entire objects. However, you encounter an error because some objects have duplicate 'name' values. How can you modify the for expression to handle duplicates by grouping objects with the same name into a list, so that the map values become lists of objects?

Hard
21

Which meta-argument is best for deploying resources that are strictly dependent on the number of items in a configuration?

Easy
22

When using for_each on a resource, how do you access the value of the current iteration inside the resource configuration?

Medium
23

You are creating a security group in AWS and need to define multiple ingress rules based on a list of port numbers. Which meta-argument is most appropriate to use to create one resource instance per port?

Easy
24

You are defining an input variable for a Terraform module that will accept a map of environment names to configuration objects. The object should have an instance_type string and a min_size number. You want Terraform to validate that any provided value matches this structure. Which type constraint should you use for the variable?

Medium
25

You are iterating over a map using for_each. You need to access the key of the current map element. Which object do you use?

Medium
26

A team defines a variable `allowed_ports` as `list(number)` and wants to validate that every value is between 1 and 65535 before any resources are planned. They add a `validation` block to the variable. Which configuration correctly enforces this constraint?

Easy
27

You maintain a Terraform module that deploys AWS resources into several environments. Rather than declaring a separate variable per environment, you want a single input variable that accepts a map of environment names to configuration objects, where each object contains a region string and an instance count number, and your configuration should reject any input that does not match this shape at plan time. Which variable type constraint should you declare?

Medium
28

You are writing a Terraform configuration that uses a `for` expression to transform a list of strings into a map where each string is both the key and the value. Which of the following expressions achieves this?

Easy
29

Your team manages AWS infrastructure with Terraform. A module needs to accept a map of environment names to instance types, where each instance type must be one of a fixed set of allowed values. You want Terraform to reject invalid configurations during plan with a clear error message. Which approach best validates the variable input?

Medium
30

How can you debug the output of a complex 'for' expression in Terraform?

Medium
31

When using 'for_each' on a list of strings, what must you do to ensure the configuration is stable?

Easy
32

Which function is most useful when you have a nested list of objects and want to create a single flat list to iterate over with for_each?

Easy
33

Which function allows you to safely access a map value even if the key might not exist?

Easy
34

You are attempting to use a conditional expression to set a variable, but the types returned by the true and false results differ. What happens?

Hard
35

A module needs to render a JSON policy document where the list of allowed actions comes from a variable that may contain duplicates and mixed case, and the final output must be deterministic and free of duplicates. The engineer writes jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = local.actions, Resource = "*" }] }). Which local definition for local.actions best guarantees determinism and uniqueness?

Hard
36

What is the primary difference between 'count' and 'for_each' regarding state management?

Easy
37

You want to pass a complex object type to a module, but you need to ensure specific attributes are present. How do you define this in the variable block?

Hard
38

A team wants to pass a variable "allowed_ports" to a module and guarantee at apply time that every value is an integer between 1024 and 65535, rejecting invalid plans with a clear message. They prefer the validation to live in the variable block so callers get immediate feedback. Which declaration accomplishes this?

Easy
39

Which THREE of the following are valid ways to pass data to a module dynamically?

Medium
40

A configuration defines a dynamic block that generates ingress rules for an aws_security_group from a list of objects. Each object has a from_port, to_port, and protocol attribute, and one object also carries an extra cidr_blocks list. You want every generated rule to include the from_port, to_port, and protocol values, and to include cidr_blocks only when it is present, without writing a separate block for the odd entry. Which approach satisfies this?

Hard
41

A team defines an aws_instance resource whose instance_type should normally come from a variable, but for one specific workspace named "dr" the value must be forced to t3.micro regardless of the variable. The team wants a single resource block that behaves correctly in every workspace without duplicating the resource. Which expression assigns instance_type appropriately?

Medium
42

You are writing a Terraform configuration that uses a dynamic block to generate multiple ingress rules within an aws_security_group resource. The dynamic block iterates over a variable named ingress_rules. Inside the content block, you need to reference the current rule. Which expression correctly accesses the current element?

Easy
43

An engineer is authoring a reusable module that accepts variable "subnets", typed as map(object({ cidr_block = string, public = bool })). The module must create one aws_subnet per entry, attaching an aws_route_table_association only when that entry's public attribute is true. The association resource must not appear at all for private subnets, and its key must match the subnet's map key. Which approach satisfies these requirements with the least waste?

Medium
44

You have a list of objects representing AWS tags. You need to transform this list into a map for use with a for_each argument. Which function should you use?

Medium

Frequently asked questions

What does the Dynamic HCL Configuration domain cover on the AWS provider exam?
You must read and write HCL that passes typed objects to modules, validates inputs with condition and error_message, transforms collections with for expressions, and generates nested blocks dynamically. The critical skill is matching type constraints and validation conditions to the actual data shape.
How many questions are in this domain?
This page lists all 44 Dynamic HCL Configuration questions in the AWS provider question bank. The actual exam draws from this domain proportionally to its weighting in the official exam blueprint.
What is the best way to practise this domain?
Start with a short focused session (10 questions) to identify gaps, then work through explanations. Repeat with a longer session once the weak areas feel solid.
Can I practise only Dynamic HCL Configuration questions?
Yes — the session launcher on this page filters questions to this domain only. Choose any session length for inline explanations and scoring.
HashiCorp Certified: Terraform Authoring and Operations Professional (AWS provider) (AWS provider) Dynamic HCL Configuration Practice Questions