Home > front end >  Error Elements of a set are identified only by their value and don't have any separate index or
Error Elements of a set are identified only by their value and don't have any separate index or

Time:01-17

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?

CodePudding user response:

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.

  • Related