This object does not have an attribute named "id"

Hi, can someone help me to fix the error ?

│ Error: Unsupported attribute
│   on line 106, in resource "aws_volume_attachment" "this":
│  106:   instance_id = element(module.ec2_instance.*.id, count.index)
│ This object does not have an attribute named "id".
module "ec2_instance" {
  source  = "terraform-aws-modules/ec2-instance/aws"
  version = "~> 3.0"

  name                   = local.server_name
  for_each               = data.aws_subnet_ids.private_subnets.ids
  subnet_id              = each.value
  ami                    =
  instance_type          = local.ec2_instance_type

resource "aws_ebs_volume" "this" {
  count             = length(local.azs)
  availability_zone = element(local.azs, count.index)
  size              = local.ebs_size

resource "aws_volume_attachment" "this" {
  count       = length(local.azs)
  volume_id   = element(aws_ebs_volume.this.*.id, count.index)
  instance_id = element(module.ec2_instance.*.id, count.index)


You are trying to use count.index which is a numeric entry lookup with the module, but that is using for_each rather than count.

You need to either use count for the module or use for_each for the volume/volume attachment.

Thanks, that worked

Also maybe you can help me with the correct tagging instance to subnet?

I want to place 3 instances in the region eu-west-1 which has 3 AZs

Instances should be placed per AZ, but now i have instance3 tagged with a subnet from AZ #2, instance1 tagged with a subnet from AZ #3 and instance2 tagged with a subnet from AZ #1