I am unable to implement the creation of multiple instances with a attached multiple network interface. I have a code where three instances are created and 3 network interfaces are attached. I would like to do this using count, for_each. I could no5 find implemented methods on the Internet. I ask you to help me how to implement the code using count
resource "aws_network_interface" "private_elasticsearch" {
subnet_id = aws_subnet.private_subnets.id
private_ips = ["10.245.10.6"]
security_groups = [aws_security_group.elastic_traffic.id,
aws_security_group.general.id]
tags = {
Environment = "Test"
Project = "test"
Name = "elasticsearch-interface"
}
}
resource "aws_network_interface" "private_elasticsearch2" {
subnet_id = aws_subnet.private_subnets.id
private_ips = ["10.245.10.7"]
security_groups = [aws_security_group.elastic_traffic.id, aws_security_group.general.id]
tags = {
Environment = "Test"
Project = "test"
Name = "elasticsearch-interface2"
}
}
resource "aws_network_interface" "private_elasticsearch3" {
subnet_id = aws_subnet.private_subnets.id
private_ips = ["10.245.10.8"]
security_groups = [aws_security_group.elastic_traffic.id, aws_security_group.general.id]
tags = {
Environment = "Test"
Project = "test"
Name = "elasticsearch-interface3"
}
}
resource "aws_instance" "dev-elasticsearch" {
ami = "ami-08bdc08970fcbd34a"
availability_zone = "eu-north-1a"
instance_type = "t3.micro"
network_interface {
device_index=0
network_interface_id = aws_network_interface.private_elasticsearch.id
}
tags = {
Environment = "Test"
Project = "test"
Name = "elasticsearch-instance-dev"
}
}
resource "aws_instance" "dev-elasticsearch-2" {
ami = "ami-08bdc08970fcbd34a"
availability_zone = "eu-north-1a"
instance_type = "t3.micro"
network_interface {
device_index=0
network_interface_id = aws_network_interface.private_elasticsearch2.id
}
tags = {
Environment = "Test"
Project = "test"
Name = "elasticsearch-instance-dev-2"
}
}
resource "aws_instance" "dev-elasticsearch-3" {
ami = "ami-08bdc08970fcbd34a"
availability_zone = "eu-north-1a"
instance_type = "t3.micro"
network_interface {
device_index=0
network_interface_id = aws_network_interface.private_elasticsearch3.id
}
tags = {
Environment = "Test"
Project = "test"
Name = "elasticsearch-instance-dev-3"
}
CodePudding user response:
You can do it with count
(for_each
also possible):
locals {
ips = ["10.245.10.6", "10.245.10.7", "10.245.10.8"]
}
resource "aws_network_interface" "private_elasticsearch" {
count = length(local.ips)
subnet_id = aws_subnet.private_subnets.id
private_ips = [local.ips[count.index]]
security_groups = [aws_security_group.elastic_traffic.id,
aws_security_group.general.id]
tags = {
Environment = "Test"
Project = "test"
Name = "elasticsearch-interface${count.index}"
}
}
resource "aws_instance" "dev-elasticsearch" {
count = length(local.ips)
ami = "ami-08bdc08970fcbd34a"
availability_zone = "eu-north-1a"
instance_type = "t3.micro"
network_interface {
device_index=0
network_interface_id = aws_network_interface.private_elasticsearch[count.index].id
}
tags = {
Environment = "Test"
Project = "test"
Name = "elasticsearch-instance-dev${count.index}"
}
}