Terraform: aws_subnet_ids is not iterable

Created on 30 May 2019  ยท  5Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.12.0
+ provider.aws v2.12.0

Terraform Configuration Files


data "aws_subnet_ids" "example" {
  vpc_id = "${var.vpc_id}"
}

data "aws_subnet" "example" {
  count = "${length(data.aws_subnet_ids.example.ids)}"
  id    = "${data.aws_subnet_ids.example.ids[count.index]}"
}

output "subnet_cidr_blocks" {
  value = ["${data.aws_subnet.example.*.cidr_block}"]
}

Debug Output

Crash Output

Error: Invalid index

  on vpc2.tf line 7, in data "aws_subnet" "example":
   7:   id    = "${data.aws_subnet_ids.example.ids[count.index]}"
    |----------------
    | count.index is 9
    | data.aws_subnet_ids.example.ids is set of string with 10 elements

This value does not have any indices.


Error: Invalid index

  on vpc2.tf line 7, in data "aws_subnet" "example":
   7:   id    = "${data.aws_subnet_ids.example.ids[count.index]}"
    |----------------
    | count.index is 0
    | data.aws_subnet_ids.example.ids is set of string with 10 elements

This value does not have any indices.


Error: Invalid index

  on vpc2.tf line 7, in data "aws_subnet" "example":
   7:   id    = "${data.aws_subnet_ids.example.ids[count.index]}"
    |----------------
    | count.index is 1
    | data.aws_subnet_ids.example.ids is set of string with 10 elements

2019/05/30 16:43:17 [TRACE] statemgr.Filesystem: removing lock metadata file .terraform.tfstate.lock.info
This value does not have any indices.


Error: Invalid index

2019/05/30 16:43:17 [TRACE] statemgr.Filesystem: unlocking terraform.tfstate using fcntl flock
  on vpc2.tf line 7, in data "aws_subnet" "example":
   7:   id    = "${data.aws_subnet_ids.example.ids[count.index]}"
    |----------------
    | count.index is 8
    | data.aws_subnet_ids.example.ids is set of string with 10 elements

This value does not have any indices.


Error: Invalid index

  on vpc2.tf line 7, in data "aws_subnet" "example":
   7:   id    = "${data.aws_subnet_ids.example.ids[count.index]}"
    |----------------
    | count.index is 5
    | data.aws_subnet_ids.example.ids is set of string with 10 elements

This value does not have any indices.


Error: Invalid index

  on vpc2.tf line 7, in data "aws_subnet" "example":
   7:   id    = "${data.aws_subnet_ids.example.ids[count.index]}"
    |----------------
    | count.index is 7
    | data.aws_subnet_ids.example.ids is set of string with 10 elements

This value does not have any indices.


Error: Invalid index

  on vpc2.tf line 7, in data "aws_subnet" "example":
   7:   id    = "${data.aws_subnet_ids.example.ids[count.index]}"
    |----------------
    | count.index is 6
    | data.aws_subnet_ids.example.ids is set of string with 10 elements

This value does not have any indices.


Error: Invalid index

  on vpc2.tf line 7, in data "aws_subnet" "example":
   7:   id    = "${data.aws_subnet_ids.example.ids[count.index]}"
2019-05-30T16:43:17.563-0400 [DEBUG] plugin: plugin process exited: path=.terraform/plugins/darwin_amd64/terraform-provider-aws_v2.12.0_x4 pid=70561
2019-05-30T16:43:17.563-0400 [DEBUG] plugin: plugin exited
    |----------------
    | count.index is 3
    | data.aws_subnet_ids.example.ids is set of string with 10 elements

This value does not have any indices.


Error: Invalid index

  on vpc2.tf line 7, in data "aws_subnet" "example":
   7:   id    = "${data.aws_subnet_ids.example.ids[count.index]}"
    |----------------
    | count.index is 2
    | data.aws_subnet_ids.example.ids is set of string with 10 elements

This value does not have any indices.


Error: Invalid index

  on vpc2.tf line 7, in data "aws_subnet" "example":
   7:   id    = "${data.aws_subnet_ids.example.ids[count.index]}"
    |----------------
    | count.index is 4
    | data.aws_subnet_ids.example.ids is set of string with 10 elements

This value does not have any indices.

Expected Behavior


I should be able to loop through a set of subnets returned by aws_subnet_ids. I encountered this error in my own code while upgrading from 0.11 to 0.12 but was able to replicate it using the exact example code on the aws_subnet_ids page (https://www.terraform.io/docs/providers/aws/d/subnet_ids.html)

Actual Behavior


It crashes saying that the object has no indices. So either some behavior changed or the documentation is wrong. I have tried everything I can think of to fix this but nothing works.

Steps to Reproduce

  • terraform init
  • terraform apply
  • Additional Context

    References

    question

    Most helpful comment

    The documentation and example code for aws_subnet_ids should be updated to reflect this.

    All 5 comments

    Hi @nfplatzke !
    I'm sorry for this confusing behavior!
    As the error states, data.aws_subnet_ids.example.ids is set, not a list. A set is _iterable_ but not _indexable_, and in this instance the fastest workaround is to convert the set to a list with the tolist() function:

    data "aws_subnet" "example" {
      count = length(data.aws_subnet_ids.example.ids)
      id    = tolist(data.aws_subnet_ids.example.ids)[count.index]
    }
    

    The documentation and example code for aws_subnet_ids should be updated to reflect this.

    That's a good point @nfplatzke. Can you open a new issue in the aws provider repository with this request? The provider documentation is in the provider's repository.

    I just ran into this. Would it be possible to revert this change to become a list again?

    tolist won't work in some cases because it does not sort the entries of a set deterministically. Thus, calling this multiple times in a template may yield non-deterministic results.

    https://www.terraform.io/docs/configuration/functions/tolist.html

    Since set elements are not ordered, the resulting list will have an undefined order that will be consistent within a particular run of Terraform

    I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

    If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

    Was this page helpful?
    0 / 5 - 0 ratings

    Related issues

    rjinski picture rjinski  ยท  3Comments

    larstobi picture larstobi  ยท  3Comments

    carl-youngblood picture carl-youngblood  ยท  3Comments

    zeninfinity picture zeninfinity  ยท  3Comments

    c4milo picture c4milo  ยท  3Comments