<p>It looks like your underlying goal here is to retrieve the full set of attributes for each of your subnets that are marked as <code>Tier = public</code>. Here's a different way to do that using features from Terraform 0.12.6:</p>

<pre><code>data "aws_subnet_ids" "public" {
  vpc_id = data.aws_vpc.vpc.id

  tags = {
    Tier = "public"
  }
}

data "aws_subnet" "public" {  
  for_each = data.aws_subnet_ids.public.ids

  id = each.value
}
</code></pre>

<p>With the above, you should find that <code>data.aws_subnet.public</code> is a map from subnet id to the attributes of that particular subnet.</p>

<hr>

<p>The reason for the error in your case is that you used <code>count.index</code> but didn't set the <code>count</code> argument, and so there is no count index to return.</p>

<p>The <code>count</code>-based equivalent of the above, which is more similar to your original example, would be this:</p>

<pre><code>data "aws_subnet_ids" "public" {
  vpc_id = data.aws_vpc.vpc.id

  tags = {
    Tier = "public"
  }
}

data "aws_subnet" "public" {  
  count = length(data.aws_subnet_ids.public.ids)

  id = sort(data.aws_subnet_ids.public.ids)[count.index]
}
</code></pre>

<p>The result of this is very similar to the <code>for_each</code>-based example I gave above, but with one significant difference: in this case <code>data.aws_subnet.public</code> will be a <em>list</em> of subnet objects, ordered by the lexical ordering of their subnet ids. That will usually make the result harder to use elsewhere in the configuration, so I'd suggest using the <code>for_each</code> approach unless there's a specific reason why you need a list.</p>

<p>(If you use a map and then later find that you need a list in a <em>specific context</em>, you can always use <code>values(data.aws_subnet.public)</code> to take the values from the map, in the same lexical order.)</p>

CC BY-SA 4.0
2019-08-20T17:47:49.380Z
Thanks for you reply, when I run code with "for_each" I get the same error, but with "sort" works.
Stailas
CC BY-SA 4.0
2019-08-20T17:54:52.890Z
Oh, sorry... I made an editing error when I was copying the code examples around. I'll fix the `for_each` example now.
Martin Atkins
CC BY-SA 4.0
2019-08-20T17:59:29.437Z
Thanks a lot, you are save me!
Stailas
CC BY-SA 4.0
2019-08-20T17:28:42.270Z
2019-08-20T17:55:09.553Z
2019-08-20T17:55:09.553Z
Martin Atkins
<p>I'm trying to upgrade terraform from 0.11 to 0.12 but receiving error:</p>

<pre><code> Error: Reference to "count" in non-counted context

  on main.tf line 33, in data "aws_subnet" "public":
  33:   id = data.aws_subnet_ids.public.ids[count.index]

The "count" object can be used only in "resource" and "data" blocks, and only
when the "count" argument is set.
</code></pre>

<p>Here is my part of code:</p>

<pre><code>data "aws_subnet_ids" "public" {
  vpc_id = data.aws_vpc.vpc.id

  tags = {
    Tier = "public"
  }
}

data "aws_subnet" "public" {  
  id = data.aws_subnet_ids.public.ids[count.index]
}
</code></pre>

<p>Maybe someone know how to fix this error?
Thanks in advance.</p>

<terraform>
Terraform | Upgrade to 0.12 error Reference to "count" in non-counted context
Stailas
# Terraform | Upgrade to 0.12 error Reference to "count" in non-counted context

(Asked by: username_0 on 2019-08-20)

I'm trying to upgrade terraform from 0.11 to 0.12 but receiving error:
 Error: Reference to "count" in non-counted context

  on main.tf line 33, in data "aws_subnet" "public":
  33:   id = data.aws_subnet_ids.public.ids[count.index]

The "count" object can be used only in "resource" and "data" blocks, and only
when the "count" argument is set.

Here is my part of code:
data "aws_subnet_ids" "public" {
  vpc_id = data.aws_vpc.vpc.id

  tags = {
    Tier = "public"
  }
}

data "aws_subnet" "public" {  
  id = data.aws_subnet_ids.public.ids[count.index]
}

Maybe someone know how to fix this error?
Thanks in advance.

**Tags:** terraform


---

## 1 answers

### Answer by username_1 on 2019-08-20. Score: 1 | Accepted answer

It looks like your underlying goal here is to retrieve the full set of attributes for each of your subnets that are marked as Tier = public. Here's a different way to do that using features from Terraform 0.12.6:
data "aws_subnet_ids" "public" {
  vpc_id = data.aws_vpc.vpc.id

  tags = {
    Tier = "public"
  }
}

data "aws_subnet" "public" {  
  for_each = data.aws_subnet_ids.public.ids

  id = each.value
}

With the above, you should find that data.aws_subnet.public is a map from subnet id to the attributes of that particular subnet.

The reason for the error in your case is that you used count.index but didn't set the count argument, and so there is no count index to return.
The count-based equivalent of the above, which is more similar to your original example, would be this:
data "aws_subnet_ids" "public" {
  vpc_id = data.aws_vpc.vpc.id

  tags = {
    Tier = "public"
  }
}

data "aws_subnet" "public" {  
  count = length(data.aws_subnet_ids.public.ids)

  id = sort(data.aws_subnet_ids.public.ids)[count.index]
}

The result of this is very similar to the for_each-based example I gave above, but with one significant difference: in this case data.aws_subnet.public will be a list of subnet objects, ordered by the lexical ordering of their subnet ids. That will usually make the result harder to use elsewhere in the configuration, so I'd suggest using the for_each approach unless there's a specific reason why you need a list.
(If you use a map and then later find that you need a list in a specific context, you can always use values(data.aws_subnet.public) to take the values from the map, in the same lexical order.)

> **Comments:**
> **username_0**: Thanks for you reply, when I run code with "for_each" I get the same error, but with "sort" works.  
> **username_1**: Oh, sorry... I made an editing error when I was copying the code examples around. I'll fix the `for_each` example now.  
> **username_0**: Thanks a lot, you are save me!  

---

