Manage similar resources with count
The count
argument replicates the given resource or module a specific
number of times with an incrementing counter. It works best when resources will
be identical, or nearly so.
In this tutorial, you will use Terraform to provision a VPC, load balancer, and
EC2 instances on AWS. Then you will use the count
argument to provision
multiple EC2 instances per private subnet with a single resource block.
Prerequisites
You can complete this tutorial using the same workflow with either Terraform Community Edition or HCP Terraform. HCP Terraform is a platform that you can use to manage and execute your Terraform projects. It includes features like remote state and execution, structured plan output, workspace resource summaries, and more.
Select the Terraform Community Edition tab to complete this tutorial using Terraform Community Edition.
This tutorial assumes that you are familiar with the Terraform and HCP Terraform workflows. If you are new to Terraform, complete the Get Started collection first. If you are new to HCP Terraform, complete the HCP Terraform Get Started tutorials first.
For this tutorial, you will need:
- Terraform v1.2+ installed locally.
- an HCP Terraform account and organization.
- HCP Terraform locally authenticated.
- the AWS CLI.
- an HCP Terraform variable set configured with your AWS credentials.
Apply initial configuration
Clone the example GitHub repository.
Change into the new directory.
The configuration in main.tf
will provision a new VPC with public and private
subnets, a load balancer, and two EC2 instances, one in each private subnet. The
variables located in variables.tf
allow you to configure the VPC. For
instance, the private_subnets_per_vpc
variable controls the number of private
subnets the configuration will create.
Create infrastructure
Set the TF_CLOUD_ORGANIZATION
environment variable to your HCP Terraform
organization name. This will configure your HCP Terraform integration.
Initialize your configuration. Terraform will automatically create the
learn-terraform-count
workspace in your HCP Terraform organization.
Note
This tutorial assumes that you are using a tutorial-specific HCP Terraform organization with a global variable set of your AWS credentials. Review the Create a Credential Variable Set for detailed guidance. If you are using a scoped variable set, assign it to your new workspace now.
Once your directory has been initialized, apply the configuration, and remember
to confirm with a yes
.
This configuration has some limitations. Currently, each private
subnet only contains one EC2 instance. If you increase the
private_subnets_per_vpc
variable, Terraform won't automatically add EC2
instances, because the EC2 instance resources are hard coded.
Make this configuration more robust by adding a variable to control the number
of EC2 instances in each private subnet with count
.
Refactor the EC2 configuration
Refactor the EC2 configuration to make it more generic. Remove or comment out
the entire block defining the app_b
EC2 instance from main.tf
.
Next, rename the resource for the other EC2 instance from app_a
to app
.
Declare a variable for instance number
Now, add the instances_per_subnet
variable to variables.tf
to define how
many instances each private subnet will have.
Scale EC2 configuration with count
Next, edit main.tf
to use count to provision multiple EC2 instances with the
app
resource block, based on the value of the new instances_per_subnet
variable and the number of private subnets.
Each instance provisioned by the resource block with count
will have a
different incrementing value for count.index
- starting with zero. This
configuration uses count.index
and modulo division to assign each instance to
a private subnet.
Because the default value of instances_per_subnet
is 2
, Terraform will
provision two EC2 instances per private subnet.
Update the load balancer
Update the load balancer configuration in the elb_http
block to attach the
instances to the load balancer.
The name of resources or modules provisioned with count
refers to the entire
collection. In this example, aws_instance.app
now refers to all of the EC2
instances. You can reference individual items in collections with the same notation as
list indexing. For example, aws_instance.app[0]
refers to the first instance
Terraform provisions.
You can create a list of all of the values of a given attribute for the items in the
collection with a star. For instance, aws_instance.app.*.id
will be a list of
all of the IDs of the instances.
Update outputs.tf
to refer to the new aws_instance.app
block instead of
app_a
and app_b
.
Apply scalable configuration
Apply this configuration now. Be sure to respond to the confirmation prompt with
yes
.
Terraform will output values for the VPC, load balancer, and instances.
Now you have configured the number EC2 instances per private subnet using the
instances_per_subnet
variable and count
. Terraform configured that many
instances per subnet, assigned them to subnets, and attached them to the load
balancer.
Clean up resources
After verifying that the resources were deployed successfully, run terraform destroy
to destroy them. Remember to respond to the confirmation prompt with
yes
.
If you used HCP Terraform for this tutorial, after destroying your resources,
delete the learn-terraform-count
workspace from your HCP Terraform
organization.
Next steps
Now that you have used count
in your configuration, explore the following
resources.
- Read the Terraform documentation for the count meta-argument.
- Learn how to use
for_each
for more complex configurations. - Learn how to use functions and dynamic expressions to make your Terraform configuration more flexible.