Bird
Raised Fist0
Terraformcloud~20 mins

Count for multiple instances in Terraform - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Count Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Configuration
intermediate
2:00remaining
Count Usage for Multiple Instances
Given the Terraform resource below, how many instances will be created?
Terraform
resource "aws_instance" "example" {
  count         = 3
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
A0
B1
C3
DDepends on the AWS account limits
Attempts:
2 left
💡 Hint
The count argument defines how many copies of the resource are created.
❓ Architecture
intermediate
2:00remaining
Dynamic Count with Variable
If the variable "instance_count" is set to 5, how many instances will this Terraform code create?
Terraform
variable "instance_count" {
  type    = number
  default = 5
}

resource "aws_instance" "example" {
  count         = var.instance_count
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
A5
B1
C0
DDepends on the AWS region
Attempts:
2 left
💡 Hint
The count uses the variable value directly.
❓ service_behavior
advanced
2:00remaining
Count with Conditional Expression
What is the number of instances created by this Terraform resource if var.create_instances is false?
Terraform
variable "create_instances" {
  type    = bool
  default = false
}

resource "aws_instance" "example" {
  count         = var.create_instances ? 4 : 0
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
A4
BTerraform will throw an error
C1
D0
Attempts:
2 left
💡 Hint
The count uses a conditional expression based on the boolean variable.
❓ security
advanced
2:00remaining
Count with Sensitive Variable
If the variable "instance_count" is marked sensitive and set to 2, how many instances will Terraform create?
Terraform
variable "instance_count" {
  type      = number
  sensitive = true
  default   = 2
}

resource "aws_instance" "example" {
  count         = var.instance_count
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
A2
B1
CTerraform will not create any instances because the variable is sensitive
DTerraform will throw a sensitive variable error
Attempts:
2 left
💡 Hint
Sensitive variables hide values in logs but do not affect resource creation.
✅ Best Practice
expert
2:00remaining
Count with Complex Expression
What is the count value and number of instances created by this resource if var.env is "prod"?
Terraform
variable "env" {
  type    = string
  default = "prod"
}

resource "aws_instance" "example" {
  count = var.env == "prod" ? 5 : 2
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
Acount = 2, creates 2 instances
Bcount = 5, creates 5 instances
Ccount = 0, creates 0 instances
DTerraform throws an error due to invalid count expression
Attempts:
2 left
💡 Hint
The count uses a conditional expression comparing the environment variable.

Practice

(1/5)
1. What does the count argument do in a Terraform resource block?
easy
A. It deletes resources after creation.
B. It creates multiple copies of the resource based on the count value.
C. It renames the resource dynamically.
D. It pauses resource creation until manually resumed.

Solution

  1. Step 1: Understand the purpose of count

    The count argument tells Terraform how many copies of a resource to create.
  2. Step 2: Recognize the effect of count value

    If count is set to 3, Terraform creates 3 identical resources.
  3. Final Answer:

    It creates multiple copies of the resource based on the count value. -> Option B
  4. Quick Check:

    count creates copies = A [OK]
Hint: Count controls how many resource copies are made [OK]
Common Mistakes:
  • Thinking count deletes resources
  • Assuming count renames resources
  • Believing count pauses creation
2. Which of the following is the correct syntax to create 4 instances of an AWS EC2 resource using count?
easy
A. resource "aws_instance" "example" { count = 4 }
B. resource "aws_instance" "example" { count = "four" }
C. resource "aws_instance" "example" { count = true }
D. resource "aws_instance" "example" { count = 1..4 }

Solution

  1. Step 1: Check the count value type

    The count argument must be a number, not a string or boolean.
  2. Step 2: Validate syntax correctness

    resource "aws_instance" "example" { count = 4 } uses count = 4, which is a valid number and correct syntax.
  3. Final Answer:

    resource "aws_instance" "example" { count = 4 } -> Option A
  4. Quick Check:

    count = number syntax = D [OK]
Hint: Count must be a number, not string or boolean [OK]
Common Mistakes:
  • Using string instead of number for count
  • Using boolean true/false for count
  • Trying to use a range expression directly
3. Given this Terraform snippet:
resource "aws_s3_bucket" "bucket" {
  count = 3
  bucket = "my-bucket-${count.index}"
}

What will be the names of the created buckets?
medium
A. ["my-bucket", "my-bucket", "my-bucket"]
B. ["my-bucket-1", "my-bucket-2", "my-bucket-3"]
C. ["my-bucket-0", "my-bucket-1", "my-bucket-2"]
D. ["my-bucket-3"]

Solution

  1. Step 1: Understand count.index usage

    The count.index starts at 0 and increments by 1 for each instance.
  2. Step 2: Apply count.index to bucket names

    With count=3, bucket names become "my-bucket-0", "my-bucket-1", and "my-bucket-2".
  3. Final Answer:

    ["my-bucket-0", "my-bucket-1", "my-bucket-2"] -> Option C
  4. Quick Check:

    count.index starts at 0 = C [OK]
Hint: count.index starts at zero and increments [OK]
Common Mistakes:
  • Starting count.index at 1 instead of 0
  • Using same name for all instances
  • Assuming only one bucket created
4. Identify the error in this Terraform resource block:
resource "aws_security_group" "sg" {
  count = 2
  name = "sg-${count}"
}
medium
A. Name attribute cannot use interpolation.
B. Missing required provider configuration.
C. Count cannot be used with security groups.
D. Using count instead of count.index in the name.

Solution

  1. Step 1: Check usage of count in interpolation

    The variable count is not valid inside strings; count.index should be used.
  2. Step 2: Confirm correct interpolation

    Replace "sg-${count}" with "sg-${count.index}" to get unique names.
  3. Final Answer:

    Using count instead of count.index in the name. -> Option D
  4. Quick Check:

    Use count.index for indexes in strings = A [OK]
Hint: Use count.index, not count, inside strings [OK]
Common Mistakes:
  • Using count instead of count.index
  • Assuming count cannot be used with this resource
  • Thinking name interpolation is disallowed
5. You want to create multiple AWS EC2 instances with different instance types using count. Which approach correctly assigns instance types from a list?
hard
A. Use count with var.instance_types[count.index] to assign types.
B. Use count and assign a single instance type to all instances.
C. Use count with a fixed string for instance type.
D. Use count with a random instance type each time.

Solution

  1. Step 1: Understand how to assign different values per instance

    Using count.index allows selecting different values from a list for each instance.
  2. Step 2: Apply list indexing with count.index

    Accessing var.instance_types[count.index] assigns the correct instance type per resource copy.
  3. Final Answer:

    Use count with var.instance_types[count.index] to assign types. -> Option A
  4. Quick Check:

    List indexing with count.index assigns unique values = B [OK]
Hint: Index lists with count.index for unique resource values [OK]
Common Mistakes:
  • Assigning same instance type to all
  • Using fixed string ignoring count.index
  • Expecting random assignment without code
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard