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.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Creating Multiple Instances with a Static Count
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:terraform plan, Terraform plans to create three resources:
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 usingcount.index. Here is the modified configuration:
- The first iteration (index 0) creates
/root/pets.txt. - The second iteration (index 1) creates
/root/dogs.txt. - The third iteration (index 2) creates
/root/cats.txt.
terraform apply, listing the /root directory produces:
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-inlength() function:
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:
/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.
A Pitfall: List Element Removal and Resource Replacement
When using count with a list, removing an element causes a shift in resource indices. Consider this configuration:pet[0], pet[1], and pet[2]. Now, if you remove the first element (/root/pets.txt), the updated variable becomes:
terraform plan now shows:
- The resource at index 0 is updated from
/root/pets.txtto/root/dogs.txt. - The resource at index 1 is updated from
/root/dogs.txtto/root/cats.txt. - The resource at index 2 is marked for destruction as there is no corresponding list element.
Viewing Resource Details
It is often useful to confirm resource details created with count. Adding an output variable lets you verify each resource’s configuration:terraform output, you should see similar details for each resource:
Conclusion
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 thelength() 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.