HiveBrain v1.2.0
Get Started
← Back to all entries
debugterraformMinor

Terraform error Reference to “count” in non-counted context for resources not necessarily data

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
referenceerrorresourcesnecessarilynoncounteddataforcontextcount

Problem

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

Solution

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
   ...

Code Snippets

data "aws_internet_gateway" "zyz_igw" {
   count = 1000000000
   ...

Context

StackExchange DevOps Q#11138, answer score: 2

Revisions (0)

No revisions yet.