-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add prefix-list capability to custom
- Loading branch information
Showing
9 changed files
with
225 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| locals { | ||
| vpc_networks = var.use_vpc_cidr ? [data.aws_vpc.this_vpc[0].cidr_block] : [] | ||
| external_ingress_networks = compact(concat(local.vpc_networks, local.ingress_networks)) | ||
| ingress_sg_names = zipmap(var.ingress_security_groups, data.aws_security_group.ingress_security_groups[*].name) | ||
| egress_sg_names = zipmap(var.egress_security_groups, data.aws_security_group.egress_security_groups[*].name) | ||
| # self = var.enable_self ? local.self_ports : [] | ||
| } | ||
|
|
||
| resource "aws_security_group" "this_security_group" { | ||
| name = local.name | ||
| description = var.description | ||
| vpc_id = var.vpc_id | ||
|
|
||
| #--- | ||
| # ingress | ||
| #--- | ||
| # ingresss external port list (list + vpc if enabaled) | ||
| dynamic "ingress" { | ||
| for_each = local.port_map["external"] | ||
| iterator = p | ||
| content { | ||
| description = "${local.short_description}: ${p.value["description"]}" | ||
| from_port = p.value["from"] | ||
| to_port = p.value["to"] | ||
| protocol = p.value["proto"] | ||
| cidr_blocks = length(p.value["cidr"]) == 0 ? distinct(flatten(compact(concat(local.external_ingress_networks, var.ingress_networks)))) : distinct(flatten(compact(concat(p.value["cidr"], var.ingress_networks)))) | ||
| } | ||
| } | ||
|
|
||
| # ingress module-defined ports | ||
| dynamic "ingress" { | ||
| for_each = local.port_map["module_ports"] | ||
| iterator = p | ||
| content { | ||
| description = "${local.short_description}: ${p.value["description"]}" | ||
| from_port = p.value["from"] | ||
| to_port = p.value["to"] | ||
| protocol = p.value["proto"] | ||
| cidr_blocks = length(p.value["cidr"]) == 0 ? distinct(flatten(compact(concat(local.external_ingress_networks, var.ingress_networks)))) : distinct(flatten(compact(concat(p.value["cidr"], var.ingress_networks)))) | ||
| } | ||
| } | ||
|
|
||
| # ingress_ports | ||
| dynamic "ingress" { | ||
| for_each = local.port_map["ingress_ports"] | ||
| iterator = p | ||
| content { | ||
| description = "${local.short_description}: ${p.value["description"]}" | ||
| from_port = p.value["from"] | ||
| to_port = p.value["to"] | ||
| protocol = p.value["proto"] | ||
| cidr_blocks = length(p.value["cidr"]) == 0 ? distinct(flatten(compact(concat(local.external_ingress_networks, var.ingress_networks)))) : distinct(flatten(compact(concat(p.value["cidr"], var.ingress_networks)))) | ||
| } | ||
| } | ||
|
|
||
| # ingress map | ||
| dynamic "ingress" { | ||
| for_each = local.port_map["ingress_map"] | ||
| iterator = p | ||
| content { | ||
| description = "${local.short_description}: ${p.value["description"]}" | ||
| from_port = p.value["from"] | ||
| to_port = p.value["to"] | ||
| protocol = p.value["proto"] | ||
| cidr_blocks = length(p.value["cidr"]) == 0 ? distinct(flatten(compact(concat(local.external_ingress_networks, var.ingress_networks)))) : distinct(flatten(compact(concat(p.value["cidr"], var.ingress_networks)))) | ||
| } | ||
| } | ||
|
|
||
| # ingress security group ids (all) | ||
| dynamic "ingress" { | ||
| for_each = local.ingress_sg | ||
| iterator = sg | ||
| content { | ||
| description = "${local.short_description}: ${local.ingress_sg_names[sg.value]}" | ||
| from_port = 0 | ||
| to_port = 0 | ||
| protocol = -1 | ||
| security_groups = [sg.value] | ||
| } | ||
| } | ||
|
|
||
| #--- | ||
| # ingress self | ||
| #--- | ||
| # ingress self port list | ||
| dynamic "ingress" { | ||
| for_each = var.enable_self ? local.self_port_map["ingress_ports"] : [] | ||
| iterator = sg | ||
| content { | ||
| description = "${local.short_description}: self ${sg.value["description"]}" | ||
| from_port = sg.value["from"] | ||
| to_port = sg.value["to"] | ||
| protocol = sg.value["proto"] | ||
| self = true | ||
| } | ||
| } | ||
|
|
||
| # ingress self port map | ||
| dynamic "ingress" { | ||
| for_each = var.enable_self ? local.self_port_map["ingress_map"] : [] | ||
| iterator = sg | ||
| content { | ||
| description = "${local.short_description}: self ${sg.value["description"]}" | ||
| from_port = sg.value["from"] | ||
| to_port = sg.value["to"] | ||
| protocol = sg.value["proto"] | ||
| self = true | ||
| } | ||
| } | ||
|
|
||
| # ingress self port default | ||
| dynamic "ingress" { | ||
| for_each = var.enable_self ? local.self_port_map["default"] : [] | ||
| iterator = sg | ||
| content { | ||
| description = "${local.short_description}: self ${sg.value["description"]}" | ||
| from_port = sg.value["from"] | ||
| to_port = sg.value["to"] | ||
| protocol = sg.value["proto"] | ||
| self = true | ||
| } | ||
| } | ||
|
|
||
| # inress with prefix lists | ||
| dynamic "ingress" { | ||
| for_each = length(var.ingress_prefix_list_names) && length(var.ingress_prefix_list_ports) > 0 ? toset(var.ingress_prefix_list_ports) : toset([]) | ||
| iterator = p | ||
| content { | ||
| description = try(p.value.label, local.short_description) | ||
| from_port = p.value.from | ||
| to_port = p.value.to | ||
| protocol = p.value.proto | ||
| prefix_list_ids = [for pl in data.aws_ec2_managed_prefix_list.inress : pl.id] | ||
| } | ||
| } | ||
|
|
||
| # egress all (with flag enable_default_egress) | ||
| dynamic "egress" { | ||
| for_each = var.enable_default_egress ? [1] : [] | ||
| iterator = sg | ||
| content { | ||
| description = "${local.short_description}: All" | ||
| from_port = 0 | ||
| to_port = 0 | ||
| protocol = -1 | ||
| cidr_blocks = distinct(flatten(compact(concat(local.egress_networks, var.egress_networks)))) | ||
| } | ||
| } | ||
|
|
||
| # egress security group ids (all) | ||
| dynamic "egress" { | ||
| for_each = local.egress_sg | ||
| iterator = sg | ||
| content { | ||
| description = "${local.short_description}: ${local.egress_sg_names[sg]}" | ||
| from_port = 0 | ||
| to_port = 0 | ||
| protocol = -1 | ||
| security_groups = [sg] | ||
| } | ||
| } | ||
|
|
||
| # egress with prefix lists | ||
| dynamic "egress" { | ||
| for_each = length(var.egress_prefix_list_names) && length(var.egress_prefix_list_ports) > 0 ? toset(var.egress_prefix_list_ports) : toset([]) | ||
| iterator = p | ||
| content { | ||
| description = try(p.value.label, local.short_description) | ||
| from_port = p.value.from | ||
| to_port = p.value.to | ||
| protocol = p.value.proto | ||
| prefix_list_ids = [for pl in data.aws_ec2_managed_prefix_list.egress : pl.id] | ||
| } | ||
| } | ||
|
|
||
| tags = merge( | ||
| var.tags, | ||
| { | ||
| "Name" = "sg-${local.name}" | ||
| "boc:created_by" = "terraform" | ||
| "boc:tf_module_version" = local._module_version | ||
| "boc:vpc:info" = join(" ", compact([var.vpc_id, var.vpc_full_name])) | ||
| } | ||
| ) | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../common/data.prefix_lists.tf |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| locals { | ||
| name = var.name != "" ? var.name : local._defaults["name"] | ||
| is_modular = var.name == "" || length(regexall("^m-", var.name)) > 0 | ||
| enable_self = var.enable_self ? ! local.is_modular : false | ||
| enable_self = var.enable_self ? !local.is_modular : false | ||
| description = var.description != "" ? var.description : local._defaults["description"] | ||
| short_description = var.short_description != "" ? var.short_description : local._defaults["short_description"] | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../common/variables.prefix_lists.tf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| variable "ingress_prefix_list_ports" { | ||
| description = "List of port objects (from,to,proto,label) for ingress prefix lists" | ||
| type = list(object({ | ||
| from = number | ||
| to = number | ||
| proto = optional(string, "tcp") | ||
| label = optional(string) | ||
| })) | ||
| default = [] | ||
| } | ||
|
|
||
| variable "egress_prefix_list_ports" { | ||
| description = "List of port objects (from,to,proto,label) for egress prefix lists" | ||
| type = list(object({ | ||
| from = number | ||
| to = number | ||
| proto = optional(string, "tcp") | ||
| label = optional(string) | ||
| })) | ||
| default = [{ from = 0, to = 0, proto = -1, label = "all" }] | ||
| } | ||
|
|