3

I'm getting this error when I do terraform apply

Error: Invalid index
│
│   on deployBuildAWS.tf line 57, in resource "aws_instance" "packer_instance":
│   57:   subnet_id              = data.aws_subnet_ids.packer_subnet.ids[0]
│
│ Elements of a set are identified only by their value and don't have any separate index or key to select with, so it's only possible to perform operations across all elements of the set.

Here are the relevant bits of my code

data "aws_subnet_ids" "packer_subnet" {
  vpc_id = data.aws_vpcs.packer_vpc.ids[0]

  tags = {
    service = "packerBuildDeploySubnet"
  }
}

...

resource "aws_instance" "packer_instance" {
  ami                    = data.aws_ami.packer_image.id
  instance_type          = "t2.small"
  subnet_id              = data.aws_subnet_ids.packer_subnet.ids[0]
  vpc_security_group_ids = ["${data.aws_security_groups.packer_security_group.ids[0]}"]
  key_name               = var.packerKeyName


}

I've tried the following but it didn't improve the situation

    data "aws_subnet_ids" "packer_subnet" {
     
    
     for_each = data.aws_subnet_ids.packer_subnet.ids
    id = each.value
    }

For completeness , here are some other blocks of code

    data "aws_vpcs" "packer_vpc" {
      tags = {
        service = "packerBuildDeployVPC"
      }
    }
    
    data "aws_subnet_ids" "packer_subnet" {
      vpc_id = data.aws_vpcs.packer_vpc.ids[0]
    
      tags = {
        service = "packerBuildDeploySubnet"
      }
    }
    
    data "aws_security_groups" "packer_security_group" {
      tags = {
        service = "packerBuildDeploySecurityGroup"
      }
    }

I'm using terraform 1.3.7 and packer 1.8.4...I've tried with various different versions of Terraform but still getting very similar errors.. Provider =

provider "registry.terraform.io/hashicorp/aws" {
  version     = "4.50.0"

I'm obviously quite new to terraform, can anybody point me in the right direction?

Mick8695
  • 357
  • 2
  • 5
  • 19

1 Answers1

4

The error message is correct: the set type in any language is unordered and therefore elements cannot be accessed by index. Since you only want to select one subnet, we can fix this by only attempting to Read one subnet:

data "aws_subnet" "packer_subnet" { # Read single AWS subnet
  vpc_id = data.aws_vpcs.packer_vpc.ids[0]

  tags = {
    service = "packerBuildDeploySubnet"
  }
}

resource "aws_instance" "packer_instance" {
  ami                    = data.aws_ami.packer_image.id
  instance_type          = "t2.small"
  subnet_id              = data.aws_subnet.packer_subnet.id # string value in data attribute
  vpc_security_group_ids = ["${data.aws_security_groups.packer_security_group.ids[0]}"]
  key_name               = var.packerKeyName
}

Note you will likely have similar issues with data.ws_security_groups.packer_security_group and data.aws_vpcs.packer_vpc which are not shown in the question, and therefore will need to apply similar fixes to those as well.

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
  • Hi Matt, thanks for this , I'll give it a try and come back with the results – Mick8695 Jan 16 '23 at 20:57
  • works like a charm.....much appreciated....but can you explain what you mean when you say "set type in any language is unordered and therefore elements cannot be accessed by index" ? – Mick8695 Jan 16 '23 at 21:17
  • 1
    Lists can use indexes, but sets are unordered hence they don't have indexes and you can't fetch elements of a set the same way you would when using lists. I think. :) – Marko E Jan 16 '23 at 21:29