Skip to content

Commit bca1ce6

Browse files
Samuel CHNIBERandyshinn
authored andcommitted
feature-#224-Implement ingress_with_prefix_list_ids and egress_with_prefix_list_ids
1 parent c0d92f5 commit bca1ce6

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

main.tf

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,67 @@ resource "aws_security_group_rule" "computed_ingress_with_self" {
430430
var.rules[lookup(var.computed_ingress_with_self[count.index], "rule", "_")][2],
431431
)
432432
}
433+
# Security group rules with "prefix_list_ids", but without "cidr_blocks", "self" or "source_security_group_id"
434+
resource "aws_security_group_rule" "ingress_with_prefix_list_ids" {
435+
count = var.create ? length(var.ingress_with_prefix_list_ids) : 0
436+
437+
security_group_id = local.this_sg_id
438+
type = "ingress"
439+
440+
prefix_list_ids = var.ingress_prefix_list_ids
441+
description = lookup(
442+
var.ingress_with_prefix_list_ids[count.index],
443+
"description",
444+
"Ingress Rule",
445+
)
446+
447+
from_port = lookup(
448+
var.ingress_with_prefix_list_ids[count.index],
449+
"from_port",
450+
var.rules[lookup(var.ingress_with_prefix_list_ids[count.index], "rule", "_")][0],
451+
)
452+
to_port = lookup(
453+
var.ingress_with_prefix_list_ids[count.index],
454+
"to_port",
455+
var.rules[lookup(var.ingress_with_prefix_list_ids[count.index], "rule", "_")][1],
456+
)
457+
protocol = lookup(
458+
var.ingress_with_prefix_list_ids[count.index],
459+
"protocol",
460+
var.rules[lookup(var.ingress_with_prefix_list_ids[count.index], "rule", "_")][2],
461+
)
462+
}
463+
464+
# Computed - Security group rules with "prefix_list_ids", but without "cidr_blocks", "self" or "source_security_group_id"
465+
resource "aws_security_group_rule" "computed_ingress_with_prefix_list_ids" {
466+
count = var.create ? var.number_of_computed_ingress_with_prefix_list_ids : 0
467+
468+
security_group_id = local.this_sg_id
469+
type = "ingress"
470+
471+
prefix_list_ids = var.ingress_prefix_list_ids
472+
description = lookup(
473+
var.ingress_with_prefix_list_ids[count.index],
474+
"description",
475+
"Ingress Rule",
476+
)
477+
478+
from_port = lookup(
479+
var.ingress_with_prefix_list_ids[count.index],
480+
"from_port",
481+
var.rules[lookup(var.ingress_with_prefix_list_ids[count.index], "rule", "_")][0],
482+
)
483+
to_port = lookup(
484+
var.ingress_with_prefix_list_ids[count.index],
485+
"to_port",
486+
var.rules[lookup(var.ingress_with_prefix_list_ids[count.index], "rule", "_")][1],
487+
)
488+
protocol = lookup(
489+
var.ingress_with_prefix_list_ids[count.index],
490+
"protocol",
491+
var.rules[lookup(var.ingress_with_prefix_list_ids[count.index], "rule", "_")][2],
492+
)
493+
}
433494

434495
#################
435496
# End of ingress
@@ -807,6 +868,93 @@ resource "aws_security_group_rule" "computed_egress_with_self" {
807868
)
808869
}
809870

871+
# Security group rules with "egress_prefix_list_ids", but without "cidr_blocks", "self" or "source_security_group_id"
872+
resource "aws_security_group_rule" "egress_with_prefix_list_ids" {
873+
count = var.create ? length(var.egress_with_prefix_list_ids) : 0
874+
875+
security_group_id = local.this_sg_id
876+
type = "egress"
877+
878+
prefix_list_ids = var.egress_prefix_list_ids
879+
description = lookup(
880+
var.egress_with_prefix_list_ids[count.index],
881+
"description",
882+
"Egress Rule",
883+
)
884+
885+
from_port = lookup(
886+
var.egress_with_prefix_list_ids[count.index],
887+
"from_port",
888+
var.rules[lookup(
889+
var.egress_with_prefix_list_ids[count.index],
890+
"rule",
891+
"_",
892+
)][0],
893+
)
894+
to_port = lookup(
895+
var.egress_with_prefix_list_ids[count.index],
896+
"to_port",
897+
var.rules[lookup(
898+
var.egress_with_prefix_list_ids[count.index],
899+
"rule",
900+
"_",
901+
)][1],
902+
)
903+
protocol = lookup(
904+
var.egress_with_prefix_list_ids[count.index],
905+
"protocol",
906+
var.rules[lookup(
907+
var.egress_with_prefix_list_ids[count.index],
908+
"rule",
909+
"_",
910+
)][2],
911+
)
912+
}
913+
914+
# Computed - Security group rules with "source_security_group_id", but without "cidr_blocks", "self" or "source_security_group_id"
915+
resource "aws_security_group_rule" "computed_egress_with_prefix_list_ids" {
916+
count = var.create ? var.number_of_computed_egress_with_prefix_list_ids : 0
917+
918+
security_group_id = local.this_sg_id
919+
type = "egress"
920+
921+
source_security_group_id = var.computed_egress_with_prefix_list_ids[count.index]["source_security_group_id"]
922+
prefix_list_ids = var.egress_prefix_list_ids
923+
description = lookup(
924+
var.computed_egress_with_prefix_list_ids[count.index],
925+
"description",
926+
"Egress Rule",
927+
)
928+
929+
from_port = lookup(
930+
var.computed_egress_with_prefix_list_ids[count.index],
931+
"from_port",
932+
var.rules[lookup(
933+
var.computed_egress_with_prefix_list_ids[count.index],
934+
"rule",
935+
"_",
936+
)][0],
937+
)
938+
to_port = lookup(
939+
var.computed_egress_with_prefix_list_ids[count.index],
940+
"to_port",
941+
var.rules[lookup(
942+
var.computed_egress_with_prefix_list_ids[count.index],
943+
"rule",
944+
"_",
945+
)][1],
946+
)
947+
protocol = lookup(
948+
var.computed_egress_with_prefix_list_ids[count.index],
949+
"protocol",
950+
var.rules[lookup(
951+
var.computed_egress_with_prefix_list_ids[count.index],
952+
"rule",
953+
"_",
954+
)][2],
955+
)
956+
}
957+
810958
################
811959
# End of egress
812960
################

variables.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ variable "ingress_prefix_list_ids" {
118118
default = []
119119
}
120120

121+
variable "ingress_with_prefix_list_ids" {
122+
description = "List of ingress rules to create where 'prefix_list_ids' is used only"
123+
type = list(map(string))
124+
default = []
125+
}
126+
121127
###################
122128
# Computed Ingress
123129
###################
@@ -151,6 +157,12 @@ variable "computed_ingress_with_source_security_group_id" {
151157
default = []
152158
}
153159

160+
variable "computed_ingress_with_prefix_list_ids" {
161+
description = "List of computed ingress rules to create where 'prefix_list_ids' is used"
162+
type = list(map(string))
163+
default = []
164+
}
165+
154166
###################################
155167
# Number of computed ingress rules
156168
###################################
@@ -184,6 +196,12 @@ variable "number_of_computed_ingress_with_source_security_group_id" {
184196
default = 0
185197
}
186198

199+
variable "number_of_computed_ingress_with_prefix_list_ids" {
200+
description = "Number of computed ingress rules to create where 'prefix_list_ids' is used"
201+
type = number
202+
default = 0
203+
}
204+
187205
#########
188206
# Egress
189207
#########
@@ -217,6 +235,12 @@ variable "egress_with_source_security_group_id" {
217235
default = []
218236
}
219237

238+
variable "egress_with_prefix_list_ids" {
239+
description = "List of egress rules to create where 'prefix_list_ids' is used only"
240+
type = list(map(string))
241+
default = []
242+
}
243+
220244
variable "egress_cidr_blocks" {
221245
description = "List of IPv4 CIDR ranges to use on all egress rules"
222246
type = list(string)
@@ -268,6 +292,12 @@ variable "computed_egress_with_source_security_group_id" {
268292
default = []
269293
}
270294

295+
variable "computed_egress_with_prefix_list_ids" {
296+
description = "List of computed egress rules to create where 'prefix_list_ids' is used only"
297+
type = list(map(string))
298+
default = []
299+
}
300+
271301
##################################
272302
# Number of computed egress rules
273303
##################################
@@ -301,6 +331,12 @@ variable "number_of_computed_egress_with_source_security_group_id" {
301331
default = 0
302332
}
303333

334+
variable "number_of_computed_egress_with_prefix_list_ids" {
335+
description = "Number of computed egress rules to create where 'prefix_list_ids' is used only"
336+
type = number
337+
default = 0
338+
}
339+
304340
variable "putin_khuylo" {
305341
description = "Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo!"
306342
type = bool

0 commit comments

Comments
 (0)