简体   繁体   中英

Error Elements of a set are identified only by their value and don't have any separate index or key to select with,

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?

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 su.net, we can fix this by only attempting to Read one su.net:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM