Processing Multiple Resources with Terraform Count Argument

Shanky

Read more posts by this author.

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 can help!

In this tutorial, you will learn how to build and run a Terraform configuration to create multiple EC2 instances 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:

  • Terraform – This tutorial will use Terraform v1.0.8 running on Ubuntu 18.04.5 LTS, but any operating system with Terraform should work.
  • 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.

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, or multiple Lambda functions, 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.

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.

The main.tf file allows you to create four identical AWS EC2 instances, each with the same AMI and instance type 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 via the Amazon EC2 console.

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, 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.

# 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.

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

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.

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.

tree terraform-count-demo  
Verifying Required Files Exist
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 initterraform planterraform apply.

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

# 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
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. 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.
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.
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!

Subscribe to Stay in Touch

Never miss out on your favorite ATA posts and our latest announcements!

Looks like you're offline!