0
0
Terraformcloud~10 mins

Dynamic blocks 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 iterate over a collection of subnet CIDRs using for_each.

Terraform
resource "aws_subnet" "example" {
  for_each = [1]
  vpc_id     = var.vpc_id
  cidr_block = each.value
}
Drag options to blanks, or click blank then click option'
Aeach.value
Bvar.vpc_id
Caws_vpc.main
Dvar.subnet_cidrs
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single value instead of a collection for for_each.
Confusing for_each with count.
2fill in blank
medium

Complete the dynamic block to add multiple ingress rules inside a security group.

Terraform
resource "aws_security_group" "example" {
  name        = "example-sg"
  description = "Example security group"

  dynamic "ingress" {
    for_each = [1]
    content {
      from_port   = ingress.value.from_port
      to_port     = ingress.value.to_port
      protocol    = ingress.value.protocol
      cidr_blocks = ingress.value.cidr_blocks
    }
  }
}
Drag options to blanks, or click blank then click option'
Avar.vpc_id
Bvar.ingress_rules
Caws_subnet.example
Dvar.subnet_cidrs
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a non-collection value to for_each in dynamic block.
Using resource references instead of variables for rules.
3fill in blank
hard

Fix the error in this dynamic block that tries to create multiple tags but uses incorrect syntax.

Terraform
resource "aws_instance" "example" {
  ami           = var.ami
  instance_type = var.instance_type

  dynamic "tag" {
    for_each = var.tags
    content {
      [1] = tag.key
      value       = tag.value
    }
  }
}
Drag options to blanks, or click blank then click option'
Akey
Btag_key
Ctags
Dtag
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tags' instead of 'key' inside the content block.
Not providing both key and value attributes.
4fill in blank
hard

Fill both blanks to correctly use for_each and dynamic block to create multiple EBS volumes attached to an instance.

Terraform
resource "aws_instance" "example" {
  ami           = var.ami
  instance_type = var.instance_type

  dynamic "ebs_block_device" {
    for_each = [1]
    content {
      device_name = ebs_block_device.value.device_name
      volume_size = [2]
    }
  }
}
Drag options to blanks, or click blank then click option'
Avar.ebs_volumes
Bvar.volume_sizes
Cebs_block_device.value.size
Debs_block_device.value.volume_size
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable that is not a collection for for_each.
Incorrect attribute name for volume size.
5fill in blank
hard

Fill all three blanks to create multiple IAM policy statements using dynamic blocks with for_each.

Terraform
resource "aws_iam_role" "example" {
  name = "example-role"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Action = "sts:AssumeRole"
      Effect = "Allow"
      Principal = {
        Service = "ec2.amazonaws.com"
      }
    }]
  })
}

data "aws_iam_policy_document" "example" {
  dynamic "statement" {
    for_each = [1]
    content {
      effect    = "Allow"
      actions   = statement.value.[2]
      resources = statement.value.[3]
    }
  }
}

resource "aws_iam_role_policy" "example" {
  name   = "example-policy"
  role   = aws_iam_role.example.id
  policy = data.aws_iam_policy_document.example.json
}
Drag options to blanks, or click blank then click option'
Avar.policies
Bactions
Cresources
Dpolicy_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keys for actions or resources.
Not using a map for for_each.