📖 Scenario: You are setting up a simple cloud infrastructure using Terraform. You want to create multiple identical virtual machines (instances) to handle web traffic.
🎯 Goal: Build a Terraform configuration that creates exactly three virtual machine instances using the count meta-argument.
📋 What You'll Learn
Create a resource block for an AWS EC2 instance named aws_instance.web.
Use the count meta-argument to create exactly 3 instances.
Set the ami attribute to "ami-0c55b159cbfafe1f0".
Set the instance_type attribute to "t2.micro".
💡 Why This Matters
🌍 Real World
Creating multiple virtual machines is common when you want to scale your application to handle more users.
💼 Career
Cloud engineers often use Terraform's count feature to efficiently manage multiple resources without repeating code.
Progress0 / 4 steps
1
DATA SETUP: Define the AWS provider
Write a provider block for AWS with the region set to "us-east-1".
Terraform
Hint
The provider block tells Terraform which cloud provider to use and where.
2
CONFIGURATION: Add the count variable
Create a variable called instance_count with a default value of 3.
Terraform
Hint
Variables help you change values easily without editing resource blocks.
3
CORE LOGIC: Create multiple instances using count
Create a resource block named aws_instance.web that uses count = var.instance_count. Set ami to "ami-0c55b159cbfafe1f0" and instance_type to "t2.micro".
Terraform
Hint
The count argument creates multiple copies of the resource.
4
COMPLETION: Add tags to instances
Add a tags block inside the aws_instance.web resource with Name = "WebServer-${count.index + 1}".
Terraform
Hint
Tags help identify your instances in the cloud console.
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
Step 1: Understand the purpose of count
The count argument tells Terraform how many copies of a resource to create.
Step 2: Recognize the effect of count value
If count is set to 3, Terraform creates 3 identical resources.
Final Answer:
It creates multiple copies of the resource based on the count value. -> Option B
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
Step 1: Check the count value type
The count argument must be a number, not a string or boolean.
Step 2: Validate syntax correctness
resource "aws_instance" "example" { count = 4 } uses count = 4, which is a valid number and correct syntax.
D. Using count instead of count.index in the name.
Solution
Step 1: Check usage of count in interpolation
The variable count is not valid inside strings; count.index should be used.
Step 2: Confirm correct interpolation
Replace "sg-${count}" with "sg-${count.index}" to get unique names.
Final Answer:
Using count instead of count.index in the name. -> Option D
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
Step 1: Understand how to assign different values per instance
Using count.index allows selecting different values from a list for each instance.
Step 2: Apply list indexing with count.index
Accessing var.instance_types[count.index] assigns the correct instance type per resource copy.
Final Answer:
Use count with var.instance_types[count.index] to assign types. -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.