Bird
Raised Fist0
Terraformcloud~5 mins

Count for multiple instances in Terraform - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you want to create many copies of the same resource without writing each one by hand. Terraform's count feature lets you do this easily by telling it how many copies to make.
When you want to launch several virtual machines with the same settings.
When you need multiple storage buckets that are similar but separate.
When you want to create many network rules that follow the same pattern.
When you want to test scaling by adding more instances quickly.
When you want to manage a group of similar resources without repeating code.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  count         = 3
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "example-instance-${count.index}"
  }
}

This file tells Terraform to create 3 AWS EC2 instances.

provider sets the cloud region.

resource defines the EC2 instance.

count tells Terraform to make 3 copies.

tags names each instance uniquely using the count index.

Commands
This command prepares Terraform to work by downloading necessary plugins and setting up the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do before making any changes. It helps you check that the 3 instances will be created as expected.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example[0] will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + tags = { + "Name" = "example-instance-0" } } # aws_instance.example[1] will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + tags = { + "Name" = "example-instance-1" } } # aws_instance.example[2] will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + tags = { + "Name" = "example-instance-2" } } Plan: 3 to add, 0 to change, 0 to destroy.
This command creates the 3 instances in AWS as defined. The -auto-approve flag skips the manual confirmation step.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example[0]: Creating... aws_instance.example[1]: Creating... aws_instance.example[2]: Creating... aws_instance.example[0]: Creation complete after 10s [id=i-0123456789abcdef0] aws_instance.example[1]: Creation complete after 10s [id=i-0123456789abcdef1] aws_instance.example[2]: Creation complete after 10s [id=i-0123456789abcdef2] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
→
-auto-approve - Automatically approve the apply step without asking for confirmation
This command displays the current state of the created resources, confirming that 3 instances exist with their details.
Terminal
terraform show
Expected OutputExpected
aws_instance.example[0]: id = i-0123456789abcdef0 ami = ami-0c55b159cbfafe1f0 instance_type = t2.micro tags = { Name = example-instance-0 } aws_instance.example[1]: id = i-0123456789abcdef1 ami = ami-0c55b159cbfafe1f0 instance_type = t2.micro tags = { Name = example-instance-1 } aws_instance.example[2]: id = i-0123456789abcdef2 ami = ami-0c55b159cbfafe1f0 instance_type = t2.micro tags = { Name = example-instance-2 }
Key Concept

If you remember nothing else from this pattern, remember: the count argument lets you create many copies of the same resource easily by telling Terraform how many you want.

Common Mistakes
Not using count.index to give each instance a unique name.
All instances get the same name, causing confusion or conflicts in the cloud provider.
Use count.index inside resource properties to create unique names or identifiers for each instance.
Setting count to zero or a negative number.
Terraform will error or create no resources, which might not be what you want.
Always set count to a positive integer to create resources, or omit count if you want only one.
Summary
Use the count argument in a resource block to create multiple instances of that resource.
Run terraform init to prepare, terraform plan to preview, and terraform apply to create the resources.
Use count.index to make each instance unique, such as naming them differently.

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