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

1 Answer 1


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
  • 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: devops.stackexchange.com/questions/8937/…. Commented Mar 23, 2020 at 3:31
  • 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. ``` Commented Mar 23, 2020 at 3:32
  • 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] Commented Mar 23, 2020 at 3:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.