<p>As far as your error goes.... <code>count</code> is a reserved name. It's only accessible when the block you're writing has a <code>count</code> set!</p>
<p>You just need to specify the <code>count</code> of <code>aws_internet_gateway</code> data resources you're trying to get. Your code implies there is more than one.</p>
<pre><code>data &quot;aws_internet_gateway&quot; &quot;zyz_igw&quot; {
   count = 1000000000
   ...
</code></pre>

CC BY-SA 4.0
2020-03-23T03:31:44.313Z
Thanks @thisguy123, I am in fact new. I have made a mention about another question when I was trying to get an answer there. Then I read I could not ask onto a question and forgot to add the link: https://devops.stackexchange.com/questions/8937/terraform-upgrade-to-0-12-error-reference-to-count-in-non-counted-context.
Jarvis Campbell
CC BY-SA 4.0
2020-03-23T03:32:57.780Z
I did what you suggested and got several other errors:

```
Error: Reference to undeclared resource

  on modules/tp-chi/main.tf line 274, in resource "aws_default_route_table" "this":
 274:   default_route_table_id = aws_vpc.this[index.count].default_route_table_id

A managed resource "index" "count" has not been declared in tp-chi.

```
Jarvis Campbell
CC BY-SA 4.0
2020-03-23T03:35:46.190Z
AND 

Error: Missing resource instance key

  on modules/tp-chi/main.tf line 279, in resource "aws_default_route_table" "this":
 279:     gateway_id = data.aws_internet_gateway.tp-chi_igw.id

Because data.aws_internet_gateway.tp-chi_igw has "count" set, its attributes
must be accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
    data.aws_internet_gateway.tp-chi_igw[count.index]
Jarvis Campbell
CC BY-SA 4.0
2020-03-23T03:08:26.227Z
2022-05-02T22:41:01.157Z
2022-05-02T22:41:01.157Z
thisguy123
<p>here's my part of my code that I am having issues with. Unfortunately I could not understand the relationship between the last answer, despite being almost what I need, and my situation.</p>

<pre><code>data "aws_internet_gateway" "zyz_igw" {
  depends_on = [aws_internet_gateway.this]

    filter {
    name   = "attachment.vpc-id"
    values = aws_vpc.this[count.index]
    #values = [aws_vpc.this[count.index]]   #based on https://www.terraform.io/docs/providers/oci/guides/filters.html
  }

  filter {
    name   = "tag:Name"
    values = ["ZYZ_Mirror_ZYZ-tp-zyz-IGW"]
  }
}
</code></pre>

<p>The error I am getting is the following:</p>

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

  on modules/xyz/main.tf line 292, in data "aws_internet_gateway" "zyz_igw":
 292:     values = aws_vpc.this[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>I get confused since I believe my issue may be with the resource and not sure how to change the data. I also saw something related to 'for_each' to use that instead of 'count' on 0.12. But the in the very beginning of my code I have the normal 'aws_vpc' resource:</p>

<pre><code>resource "aws_vpc" "this" {
  count = var.zyz_create_vpc ? 1 : 0
</code></pre>

<amazon-web-services><terraform><data>
Terraform error Reference to “count” in non-counted context for resources not necessarily data
Jarvis Campbell
# Terraform error Reference to “count” in non-counted context for resources not necessarily data

(Asked by: username_0 on 2020-03-23)

here's my part of my code that I am having issues with. Unfortunately I could not understand the relationship between the last answer, despite being almost what I need, and my situation.
data "aws_internet_gateway" "zyz_igw" {
  depends_on = [aws_internet_gateway.this]

    filter {
    name   = "attachment.vpc-id"
    values = aws_vpc.this[count.index]
    #values = [aws_vpc.this[count.index]]   #based on https://www.terraform.io/docs/providers/oci/guides/filters.html
  }

  filter {
    name   = "tag:Name"
    values = ["ZYZ_Mirror_ZYZ-tp-zyz-IGW"]
  }
}

The error I am getting is the following:
Error: Reference to "count" in non-counted context

  on modules/xyz/main.tf line 292, in data "aws_internet_gateway" "zyz_igw":
 292:     values = aws_vpc.this[count.index]

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

I get confused since I believe my issue may be with the resource and not sure how to change the data. I also saw something related to 'for_each' to use that instead of 'count' on 0.12. But the in the very beginning of my code I have the normal 'aws_vpc' resource:
resource "aws_vpc" "this" {
  count = var.zyz_create_vpc ? 1 : 0

**Tags:** amazon-web-services, terraform, data


---

## 1 answers

### Answer by username_1 on 2020-03-23. Score: 2

As far as your error goes.... count is a reserved name. It's only accessible when the block you're writing has a count set!
You just need to specify the count of aws_internet_gateway data resources you're trying to get. Your code implies there is more than one.
data "aws_internet_gateway" "zyz_igw" {
   count = 1000000000
   ...

> **Comments:**
> **username_0**: Thanks @username_1, I am in fact new. I have made a mention about another question when I was trying to get an answer there. Then I read I could not ask onto a question and forgot to add the link: https://devops.stackexchange.com/questions/8937/terraform-upgrade-to-0-12-error-reference-to-count-in-non-counted-context.  
> **username_0**: I did what you suggested and got several other errors:  ``` Error: Reference to undeclared resource    on modules/tp-chi/main.tf line 274, in resource "aws_default_route_table" "this":  274:   default_route_table_id = aws_vpc.this[index.count].default_route_table_id  A managed resource "index" "count" has not been declared in tp-chi.  ```  
> **username_0**: AND   Error: Missing resource instance key    on modules/tp-chi/main.tf line 279, in resource "aws_default_route_table" "this":  279:     gateway_id = data.aws_internet_gateway.tp-chi_igw.id  Because data.aws_internet_gateway.tp-chi_igw has "count" set, its attributes must be accessed on specific instances.  For example, to correlate with indices of a referring resource, use:     data.aws_internet_gateway.tp-chi_igw[count.index]  

---

