0
0
Terraformcloud~10 mins

Count for multiple instances in Terraform - Interactive Code 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" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  count         = [1]
}
Drag options to blanks, or click blank then click option'
A3
B1
C0
D"3"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number for count.
Setting count to 0 which creates no instances.
2fill in blank
medium

Complete the code to reference the ID of the second instance created using count.

Terraform
output "second_instance_id" {
  value = aws_instance.example[[1]].id
}
Drag options to blanks, or click blank then click option'
A1
B2
C0
D"1"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 instead of 1 for the second instance index.
Using a string index instead of a number.
3fill in blank
hard

Fix the error in the count expression to create instances based on a variable.

Terraform
variable "instance_count" {
  type    = number
  default = 2
}

resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  count         = [1]
}
Drag options to blanks, or click blank then click option'
Ainstance_count
Bvar.instance_count
Cvar["instance_count"]
D"var.instance_count"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name without var. prefix.
Using quotes around the variable reference.
4fill in blank
hard

Fill both blanks to create instances with different names using count and the index.

Terraform
resource "aws_instance" "example" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  count         = 3
  tags = {
    Name = "example-$[1]"
    Env  = "$[2]"
  }
}
Drag options to blanks, or click blank then click option'
Acount.index
Bvar.environment
Ccount_index
Denvironment
Attempts:
3 left
💡 Hint
Common Mistakes
Using count_index instead of count.index.
Not using var. prefix for variables.
5fill in blank
hard

Fill all three blanks to create a map of instance IDs keyed by their index using count.

Terraform
output "instance_ids_map" {
  value = { for i in range([1]) : i => aws_instance.example[[2]].id if i [3] 0 }
}
Drag options to blanks, or click blank then click option'
Avar.instance_count
Bi
C>
Dcount.index
Attempts:
3 left
💡 Hint
Common Mistakes
Using count.index instead of i inside the loop.
Using < instead of > in the condition.