0
0
Terraformcloud~20 mins

Count vs for_each decision in Terraform - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Count vs for_each Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Choosing between count and for_each for resource creation

You want to create multiple identical virtual machines in Terraform. You have a list of VM names. Which approach is best to create one resource per VM name?

AUse for_each with a map of VM names as keys to create one resource per name.
BUse for_each with the VM names list converted to a set to create one resource per name.
CUse count with a fixed number and hardcode VM names inside the resource.
DUse count with the length of the VM names list and index into the list inside the resource.
Attempts:
2 left
💡 Hint

Think about how Terraform uniquely identifies each resource instance.

Configuration
intermediate
2:00remaining
Terraform resource creation with count and for_each

Given the following Terraform snippet, what will be the number of resources created?

variable "servers" {
  default = ["app1", "app2", "app3"]
}

resource "aws_instance" "example" {
  count = length(var.servers)
  ami           = "ami-123456"
  instance_type = "t2.micro"
  tags = {
    Name = var.servers[count.index]
  }
}
AAn error occurs due to invalid count usage.
B3 resources, one for each server name in the list.
C0 resources, because the variable is not referenced correctly.
D1 resource, because count is not supported with lists.
Attempts:
2 left
💡 Hint

Count creates multiple instances based on the number provided.

Architecture
advanced
2:00remaining
Handling resource updates with count vs for_each

You have a Terraform configuration using count to create resources from a list. You remove one item from the list and apply changes. What happens to the resources?

ATerraform keeps all resources unchanged.
BTerraform destroys the resource corresponding to the removed item by matching its index.
CTerraform throws an error because count cannot handle list changes.
DTerraform destroys the last resource in the list regardless of which item was removed.
Attempts:
2 left
💡 Hint

Think about how count indexes resources and what happens when list length changes.

security
advanced
2:00remaining
Security implications of using for_each with sensitive data

You use for_each to create multiple resources with sensitive variables as keys. What is a potential security risk?

ATerraform automatically encrypts all sensitive data in state files, so no risk exists.
BUsing for_each disables sensitive variable masking in logs.
CSensitive data may appear in Terraform state files as keys, exposing secrets.
DThere is no difference in security between count and for_each.
Attempts:
2 left
💡 Hint

Consider how Terraform stores resource identifiers in state files.

service_behavior
expert
2:00remaining
Impact on resource lifecycle when switching from count to for_each

You have a Terraform module using count to create resources. You switch to for_each with a map of resource names. After applying, what is the expected behavior regarding resource replacement?

ATerraform will replace all resources because the resource keys changed from indexes to names.
BTerraform will keep all resources unchanged because count and for_each are interchangeable.
CTerraform will throw an error due to incompatible resource identifiers.
DTerraform will only replace resources whose keys differ between count and for_each.
Attempts:
2 left
💡 Hint

Think about how Terraform tracks resources by keys and indexes.