0
0
Terraformcloud~20 mins

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

Choose your learning style9 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.