How can I create multiple instances with 2 network interfaces on AWS?


I’m just trying to create some AWS instances (count > 1) but I’m just wondering how could I create multiple instances based on count variable with 2 network interfaces each one:

My TF code is like:

resource "aws_instance" "myinstance"{
 count = "2"

resource "aws_network_interface" "firstnic" {
 subnet_id       = xxxx
 security_groups = xxxx
 attachment {
  instance     = "${aws_instance.myinstance[count.index].id}"
  device_index = "0"

resource "aws_network_interface" "secondnic" {
 subnet_id       = xxxxx
 security_groups = xxxxx
 attachment {
  instance     = "${aws_instance.myinstance[count.index].id}"
  device_index = "1"

So, if I try to validate this code I get this error for each network instance:

Error: Reference to "count" in non-counted context

  on line 107, in resource "aws_network_interface" "firstnic":
 107:     instance     = "${aws_instance.myinstance[count.index].id}"

The "count" object can be used only in "resource" and "data" blocks, and only  when the "count" argument is set.

Any advice? How could I create two NICs for each instance?

Thx u all!