Challenge - 5 Problems
Iterator Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Configuration
intermediate2:00remaining
Terraform for_each with iterator variable
Given the following Terraform resource using
for_each with an iterator variable, what will be the value of the tags attribute for the resource named server2?Terraform
variable "servers" { default = { server1 = "10.0.0.1" server2 = "10.0.0.2" server3 = "10.0.0.3" } } resource "aws_instance" "example" { for_each = var.servers ami = "ami-123456" instance_type = "t2.micro" tags = { Name = each.key IP = each.value } }
Attempts:
2 left
💡 Hint
Remember that
each.key is the map key and each.value is the map value in for_each.✗ Incorrect
The
for_each iterates over the map variable servers. For the resource named server2, each.key is "server2" and each.value is "10.0.0.2". So the tags reflect these values.❓ service_behavior
intermediate2:00remaining
Effect of iterator variable in dynamic block
In Terraform, what will be the number of
ingress rules created by the following security group resource?Terraform
variable "ports" { default = [22, 80, 443] } resource "aws_security_group" "example" { name = "example-sg" dynamic "ingress" { for_each = var.ports content { from_port = ingress.value to_port = ingress.value protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } }
Attempts:
2 left
💡 Hint
Dynamic blocks can iterate over lists or maps to create multiple nested blocks.
✗ Incorrect
The dynamic block iterates over the list of ports [22, 80, 443], creating one ingress block per port, so 3 ingress rules are created.
❓ Architecture
advanced2:00remaining
Using iterator variable in module instantiation
You have a Terraform module that creates a virtual machine. You want to instantiate this module multiple times with different names and IPs using an iterator variable. Which of the following
for_each expressions correctly uses the iterator variable to assign unique names and IPs inside the module?Terraform
variable "vm_configs" { default = { vm1 = "192.168.1.10" vm2 = "192.168.1.11" } } module "vms" { source = "./vm_module" for_each = var.vm_configs name = ??? ip = ??? }
Attempts:
2 left
💡 Hint
Inside a for_each block,
each.key and each.value refer to the current map key and value.✗ Incorrect
The
for_each iterates over the map vm_configs. each.key is the VM name and each.value is the IP address. So assigning name = each.key and ip = each.value correctly passes unique values to each module instance.❓ security
advanced2:00remaining
Iterator variable misuse causing security risk
Consider this Terraform snippet for creating multiple IAM users with policies. What is the security risk caused by incorrect use of the iterator variable?
Terraform
variable "users" { default = ["alice", "bob"] } resource "aws_iam_user" "users" { for_each = toset(var.users) name = each.value } resource "aws_iam_policy_attachment" "attach" { for_each = toset(var.users) name = "attach-${each.value}" users = [aws_iam_user.users[each.key].name] policy_arn = "arn:aws:iam::aws:policy/ReadOnlyAccess" }
Attempts:
2 left
💡 Hint
Check how the keys and values are used in the for_each maps and sets.
✗ Incorrect
The
for_each uses a set, so each.key is the user name string, not an index. Using aws_iam_user.users[each.key] is invalid because the resource keys are user names, but the code uses each.key as an index. This causes a runtime error and no policy attachment.✅ Best Practice
expert3:00remaining
Correct use of iterator variable in nested for_each
You want to create multiple AWS S3 buckets, each with multiple lifecycle rules. Which option correctly uses nested
for_each with iterator variables to assign lifecycle rules to each bucket?Terraform
variable "buckets" { default = { bucket1 = ["logs", "archive"] bucket2 = ["temp", "backup"] } } resource "aws_s3_bucket" "buckets" { for_each = var.buckets bucket = each.key lifecycle_rule { for_each = ??? id = ??? enabled = true prefix = "${each.value}-" tags = { rule = each.key } } }
Attempts:
2 left
💡 Hint
The outer
each refers to buckets, the inner each refers to lifecycle rules.✗ Incorrect
The outer
each.value is a list of lifecycle rule names. Converting it to a set with toset(each.value) allows iteration. The inner each.value is the rule name, so id = each.value uniquely identifies each lifecycle rule.