0
0
Terraformcloud~20 mins

For_each for map-based instances in Terraform - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Map Mastery in Terraform
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding for_each with map variables

Given the following Terraform map variable:

variable "servers" {
  type = map(string)
  default = {
    web = "10.0.1.1"
    db  = "10.0.1.2"
  }
}

Which for_each expression correctly creates one resource per server with the key as the resource name?

Afor_each = toset(var.servers)
Bfor_each = values(var.servers)
Cfor_each = keys(var.servers)
Dfor_each = var.servers
Attempts:
2 left
💡 Hint

Think about what for_each expects to iterate over to create resources with keys.

Configuration
intermediate
2:00remaining
Correct resource block using for_each with map

Which resource block correctly uses for_each to create AWS instances from the map below, assigning the instance name from the map key?

variable "instances" {
  default = {
    app = "ami-123456"
    db  = "ami-654321"
  }
}
A
resource "aws_instance" "example" {
  for_each = keys(var.instances)
  ami      = var.instances[each.key]
  tags = {
    Name = each.key
  }
}
B
resource "aws_instance" "example" {
  for_each = toset(var.instances)
  ami      = each.key
  tags = {
    Name = each.value
  }
}
C
resource "aws_instance" "example" {
  for_each = var.instances
  ami      = each.value
  tags = {
    Name = each.key
  }
}
D
resource "aws_instance" "example" {
  for_each = values(var.instances)
  ami      = each.value
  tags = {
    Name = each.key
  }
}
Attempts:
2 left
💡 Hint

Remember that for_each with a map gives each.key and each.value.

Architecture
advanced
2:30remaining
Scaling infrastructure with map-based for_each

You have a map variable defining server roles and counts:

variable "server_roles" {
  default = {
    web = 3
    db  = 2
  }
}

Which approach correctly creates the specified number of instances per role using for_each with maps?

AUse <code>for_each = { for role, count in var.server_roles : for i in range(count) : "${role}-${i}" => role }</code> to flatten keys for each instance
BUse <code>for_each = { for role, count in var.server_roles : role => count }</code> and inside resource use <code>count.index</code> to create multiple instances
CUse <code>for_each = { for role, count in var.server_roles : "${role}-${count}" => count }</code> and create one resource per key
DUse <code>for_each = { for role, count in var.server_roles : role => range(count) }</code> and iterate over ranges
Attempts:
2 left
💡 Hint

Think about how to create unique keys for each instance when count > 1.

security
advanced
2:00remaining
Avoiding sensitive data exposure in for_each maps

You have a map variable with server names and passwords:

variable "server_passwords" {
  type = map(string)
  default = {
    app = "pass123"
    db  = "secret456"
  }
}

Which Terraform practice prevents exposing passwords in resource logs when using for_each?

AUse <code>for_each = var.server_passwords</code> but mark the password attribute as <code>sensitive = true</code> in the resource
BUse <code>for_each = var.server_passwords</code> directly and output the passwords as resource attributes
CConvert the map to a list and use <code>count</code> instead of <code>for_each</code>
DStore passwords in plain text in Terraform state and avoid marking sensitive
Attempts:
2 left
💡 Hint

Terraform has a way to mark sensitive values to avoid logging them.

service_behavior
expert
2:30remaining
Terraform state behavior with map-based for_each resource changes

Given a resource using for_each over a map:

resource "aws_instance" "example" {
  for_each = {
    alpha = "ami-111"
    beta  = "ami-222"
  }
  ami           = each.value
  instance_type = "t2.micro"
}

If you change the map to:

for_each = {
  beta  = "ami-222"
  gamma = "ami-333"
}

What happens to the Terraform state and resources after applying?

ATerraform updates the 'alpha' instance to use AMI 'ami-333' and keeps 'beta' unchanged
BTerraform destroys the 'alpha' instance and creates a new 'gamma' instance, keeping 'beta' unchanged
CTerraform creates a new instance for 'gamma' but leaves 'alpha' and 'beta' unchanged
DTerraform destroys all instances and recreates them with new keys
Attempts:
2 left
💡 Hint

Think about how Terraform tracks resource instances by keys in for_each maps.