0
0
Terraformcloud~10 mins

Count vs for_each decision in Terraform - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create multiple instances using count.

Terraform
resource "aws_instance" "example" {
  count = [1]
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
Drag options to blanks, or click blank then click option'
A3
Bfor_each
C1
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using for_each instead of count when a number is expected.
Passing a map or list directly to count.
2fill in blank
medium

Complete the code to create resources using for_each with a map.

Terraform
resource "aws_s3_bucket" "example" {
  for_each = [1]
  bucket   = each.key
  acl      = "private"
}
Drag options to blanks, or click blank then click option'
A["bucket1", "bucket2"]
B3
Ccount
D{"bucket1" = "value1", "bucket2" = "value2"}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a map for for_each when keys are needed.
Using count instead of for_each for maps.
3fill in blank
hard

Fix the error in the code by choosing the correct for_each value.

Terraform
resource "aws_security_group_rule" "example" {
  for_each = [1]
  type        = "ingress"
  from_port   = 80
  to_port     = 80
  protocol    = "tcp"
  cidr_blocks = [each.value]
}
Drag options to blanks, or click blank then click option'
A{"rule1" = "0.0.0.0/0", "rule2" = "10.0.0.0/16"}
B["0.0.0.0/0", "10.0.0.0/16"]
C2
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a map for for_each.
Using count instead of for_each.
4fill in blank
hard

Fill both blanks to create multiple resources with count and reference the index.

Terraform
resource "aws_instance" "example" {
  count = [1]
  ami           = "ami-123456"
  instance_type = "t2.micro"
  tags = {
    Name = "instance-$[2]"
  }
}
Drag options to blanks, or click blank then click option'
A3
Bcount.index
Cfor_each
Deach.key
Attempts:
3 left
💡 Hint
Common Mistakes
Using for_each with count.index.
Using each.key with count.
5fill in blank
hard

Fill all three blanks to create resources using for_each and access keys and values.

Terraform
resource "aws_s3_bucket" "example" {
  for_each = [1]
  bucket   = [2]
  acl      = "private"
  tags = {
    Environment = [3]
  }
}
Drag options to blanks, or click blank then click option'
A{"dev" = "dev-bucket", "prod" = "prod-bucket"}
Beach.key
Ceach.value
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using count instead of for_each.
Mixing each.key and each.value incorrectly.