@@ -5,6 +5,10 @@ locals {
55
66 is_t_instance_type = replace (var. instance_type , " /^t(2|3|3a|4g){1}\\ ..*$/" , " 1" ) == " 1" ? true : false
77
8+ network_interfaces = var. network_interface != null ? var. network_interface : {}
9+ primary_network_interface = one ([for k , v in local . network_interfaces : v if coalesce (v. device_index , k) == 0 ]... )
10+ secondary_network_interfaces = { for k , v in local . network_interfaces : k => v if coalesce (v. device_index , k) != 0 }
11+ 812 ami = try (coalesce (var. ami , try (nonsensitive (data. aws_ssm_parameter . this [0 ]. value ), null )), null )
913
1014 instance_tags = merge (
@@ -28,16 +32,6 @@ locals {
2832 )
2933}
3034
31- # Compute primary and additional network interfaces (by device_index)
32- locals {
33- network_interfaces = var. network_interface != null ? {
34- for k , v in var . network_interface : tostring (try (v. device_index , tonumber (k))) => v
35- } : {}
36- 37- primary_network_interface = try (local. network_interfaces [" 0" ], null )
38- additional_network_interfaces = { for k , v in local . network_interfaces : k => v if k != " 0" }
39- }
40- 4135data "aws_ssm_parameter" "this" {
4236 count = local. create && var. ami == null ? 1 : 0
4337
@@ -176,12 +170,11 @@ resource "aws_instance" "this" {
176170
177171 monitoring = var. monitoring
178172
179- dynamic "primary_network_interface" {
180- for_each = var. network_interface != null && local. primary_network_interface != null ? [local . primary_network_interface ] : []
181- 173+ dynamic "network_interface" {
174+ for_each = var. network_interface != null ? [local . primary_network_interface ] : []
182175 content {
183- delete_on_termination = primary_network_interface . value . delete_on_termination
184- network_interface_id = primary_network_interface . value . network_interface_id
176+ device_index = 0
177+ network_interface_id = network_interface . value . network_interface_id
185178 }
186179 }
187180
@@ -363,12 +356,11 @@ resource "aws_instance" "ignore_ami" {
363356
364357 monitoring = var. monitoring
365358
366- dynamic "primary_network_interface" {
367- for_each = var. network_interface != null && local. primary_network_interface != null ? [local . primary_network_interface ] : []
368- 359+ dynamic "network_interface" {
360+ for_each = var. network_interface != null ? [local . primary_network_interface ] : []
369361 content {
370- delete_on_termination = primary_network_interface . value . delete_on_termination
371- network_interface_id = primary_network_interface . value . network_interface_id
362+ device_index = 0
363+ network_interface_id = network_interface . value . network_interface_id
372364 }
373365 }
374366
@@ -547,12 +539,11 @@ resource "aws_spot_instance_request" "this" {
547539
548540 monitoring = var. monitoring
549541
550- dynamic "primary_network_interface" {
551- for_each = var. network_interface != null && local. primary_network_interface != null ? [local . primary_network_interface ] : []
552- 542+ dynamic "network_interface" {
543+ for_each = var. network_interface != null ? [local . primary_network_interface ] : []
553544 content {
554- delete_on_termination = primary_network_interface . value . delete_on_termination
555- network_interface_id = primary_network_interface . value . network_interface_id
545+ device_index = 0
546+ network_interface_id = network_interface . value . network_interface_id
556547 }
557548 }
558549
@@ -617,20 +608,6 @@ resource "aws_ec2_tag" "spot_instance" {
617608 value = each. value
618609}
619610
620- # ###############################################################################
621- # Additional Network Interface Attachments
622- # ###############################################################################
623- 624- resource "aws_network_interface_attachment" "this" {
625- for_each = local. create && var. network_interface != null ? local. additional_network_interfaces : {}
626- 627- region = var. region
628- 629- instance_id = local. instance_id
630- network_interface_id = each. value . network_interface_id
631- device_index = try (each. value . device_index , tonumber (each. key ))
632- }
633- 634611# ###############################################################################
635612# EBS Volume(s)
636613# ###############################################################################
@@ -822,6 +799,18 @@ resource "aws_vpc_security_group_ingress_rule" "this" {
822799 to_port = try (coalesce (each. value . to_port , each. value . from_port ), null )
823800}
824801
802+ # ###############################################################################
803+ # Network Interface Attachment
804+ # ###############################################################################
805+ 806+ resource "aws_network_interface_attachment" "this" {
807+ for_each = var. create && var. create_network_interface_attachment ? local. secondary_network_interfaces : {}
808+ 809+ instance_id = local. instance_id
810+ network_interface_id = each. value . network_interface_id
811+ device_index = each. value . device_index
812+ }
813+ 825814# ###############################################################################
826815# Elastic IP
827816# ###############################################################################
0 commit comments