Count

In this lesson, we'll go through the count in detail using examples.

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:

Press + to interact
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 ...