0
0
Terraformcloud~5 mins

Count for multiple instances in Terraform - Commands & Configuration

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