This article explores Terraform’s count meta-argument for creating multiple resource instances and discusses issues with modifying the underlying list used with count.
In this article, we explore Terraform’s count meta-argument, demonstrating how it can be used to create multiple resource instances and discussing potential issues when modifying the underlying list used with count. This guide covers both static and dynamic count techniques to help you manage resources efficiently.
One of the simplest ways to create multiple instances of a resource is by using a static count. Below is an example that creates three instances of a local file resource:
When you run terraform plan, Terraform plans to create three resources:
Copy
Ask AI
$ terraform plan[Output Truncated]Terraform will perform the following actions:...# local_file.pet[2] will be created+ resource "local_file" "pet" { + directory_permission = "0777" + file_permission = "0777" + filename = "/root/pets.txt" + id = (known after apply)}Plan: 3 to add, 0 to change, 0 to destroy.
Each resource is indexed as pet[0], pet[1], and pet[2]. Although three resources are created, all instances share the same file name, which means Terraform creates the same file three times rather than three unique files.
Creating Unique Resources by Using a List Variable
To generate unique resources, update the variable definition to a list and reference each element using count.index. Here is the modified configuration:
Dynamically Adjusting the Count with the length() Function
A drawback of the previous approach is its fixed count. If you wish to add more file names, the configuration would still create only three resources. To adapt dynamically to the number of elements, use the built-in length() function:
When running terraform apply, Terraform will automatically create five resources—one for each element in the list. To revert back to testing with three elements, simply modify the list back to three filenames:
After applying these changes, the /root directory will properly contain the three files.
Modifying a list that drives the count parameter can lead to resource replacements due to index shifts. This may trigger unwanted service interruptions or data loss if the resources are critical.
After a successful apply, Terraform recognizes three resources: pet[0], pet[1], and pet[2]. Now, if you remove the first element (/root/pets.txt), the updated variable becomes:
This behavior occurs because the resource indices are directly linked to the order of the list elements. Removing an element causes subsequent elements to shift, resulting in the unnecessary replacement or destruction of resources.
In this article, we demonstrated how to use the count meta-argument in Terraform to create multiple resource instances. We examined both static and dynamic count methods using a list variable and the length() function, respectively. Additionally, we discussed a common pitfall—removing an element from the list can trigger resource replacements due to index shifting.Practice using the count meta-argument in your Terraform projects to automate resource creation and better manage infrastructure changes.For more detailed information on Terraform and its resource management, refer to the Terraform Documentation.