---
title: "Drive Terraform Count Argument to Process Multiple Resources"
description: "Tired of manually creating multiple resources in Terraform? Learn how to process multiple resources simultaneously with Terraform count argument in this tutorial!"
canonical: "https://adamtheautomator.com/terraform-count/"
---

# Drive Terraform Count Argument to Process Multiple Resources

> Tired of manually creating multiple resources in Terraform? Learn how to process multiple resources simultaneously with Terraform count argument in this tutorial!

Source: https://adamtheautomator.com/terraform-count/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Drive Terraform Count Argument to Process Multiple Resources](https://adamtheautomator.com/wp-content/uploads/2021/10/Processing-Multiple-Resources-with-Terraform-Count-Argument-1.jpg)

# Drive Terraform Count Argument to Process Multiple Resources

[![](https://secure.gravatar.com/avatar/2beb65fca997135120ed98dc6a2e57dcdf1a7d7d2f5ff687b5d91dc7ccd7a6b5?s=192&d=mm&r=g)Sagar](https://adamtheautomator.com/author/shanky-mendiratta/)19 October 20214 min. read

Categories: [DevOps](/category/devops/)

Tags:[Terraform](/tag/terraform/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Building a Terraform Configuration](#building-a-terraform-configuration)
*   [Running Terraform to Create the Multiple AWS EC2 Instances](#running-terraform-to-create-the-multiple-aws-ec2-instances)
*   [Verifying the AWS EC2 Instances](#verifying-the-aws-ec2-instances)
*   [Conclusion](#conclusion)

Creating multiple resources of the same kind in one go is possible with AWS Management Console, but how do you automate the process? The [Terraform count argument](https://www.terraform.io/docs/language/meta-arguments/count.html) can help!

In this tutorial, you will learn how to build and run a Terraform configuration to create multiple [EC2 instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html) in an AWS account using Terraform count from scratch!

## Prerequisites

This tutorial comprises step-by-step examples. If you’d like to follow along, ensure you have the following in place:

*   An [AWS account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/)
    
*   [Terraform](https://www.terraform.io/docs/enterprise/install/index.html) – This tutorial will use Terraform v1.0.8 running on Ubuntu 18.04.5 LTS, but any operating system with [Terraform](https://adamtheautomator.com/terraform-windows/) should work.
    

Related:[How to Install Terraform on Windows](https://adamtheautomator.com/terraform-windows/)

*   A code editor – Any text editor will work, but it’s best to have one that understands the HCL Terraform language. Try out [Visual Studio (VS) Code](https://code.visualstudio.com/).

## Building a Terraform Configuration

In a DevOps scenario, you typically need to work on various projects that comprise building multiple resources of the same kind. These resources can be multiple EC2 instances, multiple [AWS S3 buckets](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html), or multiple [Lambda functions](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html), Sounds like a tedious process, right?

As a solution, build a Terraform configuration and define a Terraform count to automate the process of creating multiple instances.

1\. Open your terminal and run the commands below to create a folder in your home directory, and change the working directory to that folder.

In this tutorial, the folder is called _terraform count demo_, stored in your home directory, but can be named differently as you prefer. This folder stores your Terraform configuration files.

```bash
mkdir ~/terraform-count-demo
cd ~/terraform-count-demo
```

2\. Next, open your favorite code editor, and copy/paste the configuration below. Save the configuration as _main.tf_ inside the _~/terraform-count-demo_ directory.

> _Terraform uses several different types of configuration files. Each file is written in either plain text format or JSON format with a specific naming convention of either [.tf or .tfjson format](https://www.terraform.io/docs/language/files/index.html)._

The _main.tf_ file allows you to create four identical AWS EC2 instances, each with the same [AMI](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html) and [instance type](https://aws.amazon.com/ec2/instance-types/?trkCampaign=acq_paid_search_brand&sc_channel=PS&sc_campaign=acquisition_IN&sc_publisher=Google&sc_category=Cloud%20Computing&sc_country=IN&sc_geo=APAC&sc_outcome=acq&sc_detail=amazon%20ec2%20instance%20types&sc_content=%7Bad%20group%7D&sc_matchtype=e&sc_segment=536392685920&sc_medium=ACQ-P%7CPS-GO%7CBrand%7CDesktop%7CSU%7CCloud%20Computing%7CEC2%7CIN%7CEN%7CSitelink&s_kwcid=AL!4422!3!536392685920!e!!g!!amazon%20ec2%20instance%20types&ef_id=Cj0KCQjwssyJBhDXARIsAK98ITQem3BsxzQ6K0bJYLG7zuCFqalemJ4STyfGcvXcNhTGMJZsJaDn8McaAmmBEALw_wcB:G:s&s_kwcid=AL!4422!3!536392685920!e!!g!!amazon%20ec2%20instance%20types) declared as variables. These variables have their values defined in another file named _terraform.tfvars_ that you will create later in this section.

> _You can find [Linux AMIs](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html) via the Amazon EC2 console._

```yaml
resource "aws_instance" "my-machine" {
  # Creates four identical aws ec2 instances
  count = 4     
  
  # All four instances will have the same ami and instance_type
  ami = var.ami 
  instance_type = var.instance_type # 
  tags = {
    # The count.index allows you to launch a resource 
    # starting with the distinct index number 0 and corresponding to this instance.
    Name = "my-machine-${count.index}"
  }
}
```

3\. Now, create another file named _vars.tf_ inside the _~/terraform-count-demo_ directory, and copy/paste the content below to the _vars.tf_ file. The _vars.tf file_ is a [Terraform variables file](https://www.terraform.io/docs/language/values/variables.html), which contains all the variables that the configuration file references.

> _Including all configuration values in a single configuration file is possible. But to keep things clear and for developers and admins, breaking the logic and variables into separate files is much more preferable._

```bash
# Creating a Variable for ami
variable "ami" {       
  type = string
}

# Creating a Variable for instance_type
variable "instance_type" {    
  type = string
}
```

4\. Create one more file called _provider.tf_ inside _~/terraform-count-demo_ directory and put in the code below to the _provider.tf_ file. The _provider.tf_ file is where you define the AWS provider so that Terraform can connect to the correct cloud services.

Related:[Getting Started with the Terraform AWS Provider](https://adamtheautomator.com/terraform-aws/)

The code below defines the AWS provider named `aws` and creates resources in the `us-east-2` region.

```yaml
provider "aws" {
  region = "us-east-2"
}
```

5\. Create one last file inside the _~/terraform-count-demo_ directory. Name the file as _terraform.tfvars,_ and paste in the code below. The _terraform.tfvars_ contains the values that Terraform uses to replace the variable references inside of a configuration file.

```yaml
ami = "ami-0742a572c2ce45ebf"
instance_type = "t2.micro"
```

6\. Finally, run the `tree` command below to verify all of the required files are contained in the `terraform-count-demo` folder.

```bash
tree terraform-count-demo  
```

![Verifying Required Files Exist ](https://adamtheautomator.com/wp-content/uploads/2021/10/image-188.png)

Verifying Required Files Exist

## Running Terraform to Create the Multiple AWS EC2 Instances

Now that you have the Terraform configuration file and variables files ready to go, it’s time to initiate Terraform and create four AWS EC2 instances!

To provision a Terraform configuration, Terraform typically uses a three-stage approach [terraform init](https://www.terraform.io/docs/cli/commands/init.html) → [terraform plan](https://www.terraform.io/docs/cli/commands/plan.html) → [terraform apply](https://www.terraform.io/docs/cli/commands/apply.html).

Run the commands below to navigate to the _~\\terraform-count-demo_ directory and create an AWS ec2 instances.

Related:[Creating your First EKS Cluster with Terraform \[Step-by-Step\]](https://adamtheautomator.com/terraform-eks-module/#h-creating-the-aws-eks-cluster-with-a-terraform-configuration)

```bash
# Navigate to the ~\terraform-count-demo directory
cd ~\terraform-count-demo
# Initializes Terraform
terraform init
# Ensures your configuration's syntax is correct and gives you an overview 
# of which resources will be provisioned in your infrastructure.
terraform plan
# Provision/build the AWS EC2 instances
terraform apply
```

Notice the IDs defined in the Terraform output. You’ll need these IDs to correlate the created resources in the next section.

![Building AWS EC2 Instances](https://adamtheautomator.com/wp-content/uploads/2021/10/image-189.png)

Building AWS EC2 Instances

## Verifying the AWS EC2 Instances

By now, you should have created all the EC2 instances launched with Terraform. But verify if all the EC2 instances exist by manually checking in the AWS Management Console.

Open your favorite web browser and log on to the [AWS Management Console](https://aws.amazon.com/console/). Now click on the search bar at the top, search for ‘EC2’, and click on the EC2 menu item, as shown below.

![Opening an EC2 console.](https://adamtheautomator.com/wp-content/uploads/2021/10/image-190.png)

Opening an EC2 console.

On the EC2 page, you’ll see all the four EC2 instances you created. Verify that all IDs are the same ones that Terraform returned earlier under the “Running Terraform to Create the Multiple AWS EC2 Instances” section.

![Checking all the instances in the AWS console.](https://adamtheautomator.com/wp-content/uploads/2021/10/image-191.png)

Checking all the instances in the AWS console.

## Conclusion

In this tutorial, you’ve learned how to use Terraform to launch or create multiple resources simultaneously using Terraform count in a Terraform configuration. With this knowledge take what you have learned and use Terraform to manage your entire infrastructure!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-count%2F&text=Drive%20Terraform%20Count%20Argument%20to%20Process%20Multiple%20Resources)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fterraform-count%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-count%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2023/02/terraform-best-practices.jpg)

### [Ensuring Success by Learning Terraform Best Practices](/terraform-best-practices/)

Discover Terraform best practices and ensure that your DevOps environment is being utilized safely and securely to it’s fullest potential!

![](https://adamtheautomator.com/wp-content/uploads/2022/08/Which-Tool-Terraform-vs-Cloudformation-in-the-Fight-for-AWS.jpg)

### [Which Tool? Terraform vs. Cloudformation in the Fight for AWS](/terraform-vs-cloudformation/)

Learn which tool, Terraform vs Cloudformation is right for infrastructure as configuration with AWS in this ATA Learning tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2022/07/A-Definitive-Guide-to-Leveraging-the-AWS-Terraform-IAM-Role.jpg)

### [A Definitive Guide to Leveraging the AWS Terraform IAM Role](/terraform-iam-role/)

Discover this definitive guide to the AWS Terraform IAM role and take your devops game to the next level in this ATA Learning tutorial.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
