Is using count considered a bad practice? Which are good use cases?


I’m working on a blog post highlighting the differences between count and for each.

I created three users using the following code:

variable "user_names_count" {
  default =   [ "user1", "user2", "user3"]

I renamed user2 to user4:

resource "aws_iam_user" "example_count" {
  count = length(var.user_names_count)
  name  = var.user_names_count[count.index]

And I got the following output:

# aws_iam_user.example_count[1] will be updated in-place
~ resource "aws_iam_user" "example_count" {
      arn           = "arn:aws:iam::247111851647:user/user2"
      force_destroy = false
      id            = "user2"
    ~ name          = "user2" -> "user4"
      path          = "/"
      tags          = {}
      unique_id     = "AIDATTCIDTZ73DUTRE7PM"

Plan: 0 to add, 1 to change, 0 to destroy.

I understand this is due to the count index. What I don’t understand is:

Which are adequate use cases for using count? I couldn’t find anything about this in the official documentation.


We have some documentation on when to use count vs for_each. In summary, use count only when all resources instances are identical (or very close). If you find yourself using the index to look up data for the resource instance, this is a sign that you should be using for_each.