count and for_each—to efficiently create multiple instances of a resource using the same configuration block. Understanding these options will help you manage resource deployments more dynamically and reliably.
Using Count
Thecount meta-argument allows you to create multiple copies of a resource. In the example below, we launch three EC2 instances by setting count to 3:
terraform apply, Terraform records these instances in the state file as a list. This means that each resource is identified by its index, for example:
aws_instance.web[0]aws_instance.web[1]aws_instance.web[2]
Dynamically Setting Count Using a List Variable
To streamline your configuration, you can use a list variable instead of a hardcoded count. The following configuration uses thelength function to determine the number of instances based on the number of elements in the webservers list:
webservers list, with each instance tagged according to its corresponding name.
Using a dynamic list makes your Terraform configuration more flexible and easier to maintain when scaling resources.
Potential Drawback of Using Count
One important limitation of thecount meta-argument is that it organizes resources based on list indices. If the order of the elements changes or an element is removed, Terraform may update the wrong resource or destroy the unintended instance.
For example, consider the updated configuration when "web1" is removed from the list:
terraform plan may generate an execution plan like this:
Since resources are managed as a list when using
count, removing or reordering items can lead to unintended updates or inadvertent deletion of resources. Always verify your plan before applying such changes.Using For_Each
Thefor_each meta-argument offers an alternative approach by creating a resource for each element in a set or map. Unlike count, for_each stores resources in a map keyed by each element’s value, eliminating issues caused by index reordering.
Consider the following configuration that uses for_each to loop through the webservers variable:
for_each:
- The
webserversvariable is defined as a set (or map) to ensure unique keys. - Each resource is identified by its key, such as
aws_instance.web["web1"]. - Removing an element (like
"web1") only destroys that specific resource without affecting the others.
terraform state list displays resources as:
"web1"—only affects the corresponding resource.
Conclusion
In this article, we explored two strategies for provisioning multiple instances in Terraform: usingcount and using for_each. While count is straightforward, it can introduce complications when handling dynamic lists due to index-based identification. In contrast, for_each facilitates more predictable resource management by leveraging set or map keys.
By understanding these distinctions, you can choose the most appropriate approach for your Terraform configurations and avoid unintended resource modifications. Stay tuned for our next article, where we’ll dive deeper into advanced Terraform concepts and best practices.
For more detailed Terraform documentation and best practices, visit the Terraform Registry or HashiCorp’s documentation.