I’m creating mutiple ec2 instances and tag eni … below code works fine if I’m creating 1 ec2, but does not work if trying to create mutiple ec2 and tag eni’s at the same time… please help…
resource “aws_instance” “myec2” {
count = var.no_of_ec2 ? 3 : 4
ami = “ami-0c2b8ca1dad447f8a”
instance_type = “t2.micro”
tags = merge({Name = “myec2_tag_testing${count.index + 1}”},var.tags)
}
resource “aws_ec2_tag” “ecs_node_eni” {
count = var.no_of_ec2 ? 3 : 4
resource_id = aws_instance.myec2[count.index].primary_network_interface_id
for_each = var.tags
key = each.key
value = each.value
}
variable “tags” {
description = “default tags”
type = map(string)
default = {
cost_center = “99999”
car_id = “88888”
owner = “AAAAA”
environment = “DDDDD”
}
}
variable “no_of_ec2” {
type = bool
default = true
}
Getting the below error …
Error: Invalid combination of “count” and “for_each”
on main.tf line 19, in resource “aws_ec2_tag” “ecs_node_eni”:
19: for_each = var.tags
The “count” and “for_each” meta-arguments are mutually-exclusive, only one
should be used to be explicit about the number of resources to be created.