Count
In this lesson, we'll go through the count in detail using examples.
We'll cover the following
In chapter Resources, we covered the basics of resources. In this chapter, we are going to cover some more advanced techniques that will help you on your Terraform journey.
Count
Let’s say that you wanted to create lots of resources and you wanted them all to be pretty much the same. This would be a pretty laborious task if you used the techniques we have learned so far since it would come down to copy and pasting a resource again and again. If you later wanted to make a change to the resource, you would have to change every copy.
Project example #1
Let’s work through an example:
provider "aws" {region = "us-east-2"}resource "aws_sqs_queue" "queue0" {name = "queue-0"}resource "aws_sqs_queue" "queue1" {name = "queue-1"}resource "aws_sqs_queue" "queue2" {name = "queue-2"}resource "aws_sqs_queue" "queue3" {name = "queue-3"}output "queue-0-name" {value = aws_sqs_queue.queue0.name}
In the example above, we create four queues by specifying them individually. This is just about manageable with four queues but would not scale to lots of queues and would make maintenance hard. Let’s see how to create the same four queues using a count. As a side note, notice that we are outputting the name of queue0. This will make more sense when we look at the count implementation and is here for reference.
Running the project
Click the terminal to run terraform init
followed by terraform apply
. When prompted, enter yes
to run the project.
📝Note: Don’t forget to destroy the Terraform infrastructure using
terraform destroy
after you have completed the task.
Project example #2
Let’s walk through another example:
provider "aws" {region = "us-east-2"}resource "aws_sqs_queue" "queues" {count = 4name = "queue-${count.index}"}output "queue-0-name" {value = aws_sqs_queue.queues[0].name}
When you run the code above you will notice that the same four queues got created. This code is much
simpler and allows us to make sweeping changes to every queue in a single place. The attribute
that drives this to be four queues rather than a single queue is count
. The count is a special attribute that is
available on every resource. When you set the count
, Terraform will know that you want to create 0,
1 or more of this resource. We can get the current index of the count
to use as part of our Terraform
code by using the special variable count.index
. This is used inside the name attribute to name
each of the queues after the current index. You can think of this as a for loop in the background
starting at 0 and ending when the counter gets to the current count
.
As we are using the count
attribute on a resource in the background Terraform will always create a list of the resource (even if you only create 1). We can access one of the queues that we created when using the count attribute by using the []
syntax, with the index of the item you want to select in the brackets. For example, to select queue 0
, use aws_sqs_queue.queues[0]
. You can then use any property on the queue just as if it was a normal resource that had been created
without a count.
Another great use case for a count is when you want a resource to be optional. Perhaps in a
development environment, you want a certain resource but do not want to create it in production.
To do this, you can set the count of a resource to 0
if you do not want the resource or 1
if you do want it.
📝Note: You need to remember that doing this will change how you reference the created resource to using the list syntax.
Running the project
Click the terminal to run terraform init
followed by terraform apply
. When prompted, enter yes
to run the project.
📝Note: Don’t forget to destroy the Terraform infrastructure using
terraform destroy
after you have completed the task.
Get hands-on with 1300+ tech skills courses.