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?