How to list out subnet ids

I am currently on 0.11.14 and working to move to 0.12.18 and i hit a snag on a current code setup. I am making a data call to get the subnet ids from a vpc. See below

data "aws_subnet_ids" "task" {
  vpc_id = var.app_vpc_id

  tags = {
    id = "task"
  }
}

Then I am trying to pass those subnet ids in as environment variables to a lambda function…

resource "aws_lambda_function" "function_name" {

  environment {
    variables = {
      SUBNET_1         = element(tolist(data.aws_subnet_ids.task.ids), count.index + 0)
      SUBNET_2         = element(tolist(data.aws_subnet_ids.task.ids), count.index + 1)
      SUBNET_3         = element(tolist(data.aws_subnet_ids.task.ids), count.index + 2)
    }
  }

This setup works fine on 0.11.14 but on 0.12.18 it is giving me an error:

SUBNET_1         = element(tolist(data.aws_subnet_ids.task.ids), count.index + 0)

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

SUBNET_2         = element(tolist(data.aws_subnet_ids.task.ids), count.index + 1)

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

SUBNET_2         = element(tolist(data.aws_subnet_ids.task.ids), count.index + 2)

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

Any suggestion on how I can do this in 0.12?

It’s not entirely clear where count was coming from in your example. Would this work?

  environment {
    variables = {
      SUBNET_1         = element(tolist(data.aws_subnet_ids.task.ids),  0)
      SUBNET_2         = element(tolist(data.aws_subnet_ids.task.ids),  1)
      SUBNET_3         = element(tolist(data.aws_subnet_ids.task.ids),  2)
    }
  }

I hope this helps.

Depending on the actual output from the data source, you might be able to simplify further:

  environment {
    variables = {
      SUBNET_1         = data.aws_subnet_ids.task.ids[0]
      SUBNET_2         = data.aws_subnet_ids.task.ids[1]
      SUBNET_3         = data.aws_subnet_ids.task.ids[2]
    }
  }

Perfect… you first example seems to have worked. Well I am past that error working on others so I have not been able to validate it is working as expected. But thanks for your input.