-
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.
- Loading branch information
Showing
6 changed files
with
153 additions
and
61 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,2 @@ | ||
| # v1.0 -- 20210421 | ||
|
|
||
| * add module version, update tags | ||
| # v1.0.0 -- 20210421 | ||
| - add module version, update tags |
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 |
|---|---|---|
| @@ -1,52 +1,119 @@ | ||
| /** | ||
| * # About | ||
| * | ||
| * This describes how to use the aws-common-security-groups submodule for rds-oracle | ||
| * | ||
| * # Usage | ||
| * | ||
| * ```code | ||
| * module "rds-postgres" { | ||
| * source = "git::https://vc1.csvd.census.gov/terraform-modules/aws-common-security-groups.git//rds-postgres" | ||
| * | ||
| * # name = "m-rds-postgres" | ||
| * # About rds-postgres | ||
| * | ||
| * This describes how to use the aws-common-security-groups submodule for rds-postgres. | ||
| * | ||
| * Default and auxilliary ports are included in this. They are opened to everything. | ||
| * | ||
| * ## Usage | ||
| * | ||
| * ```hcl | ||
| * module "postgres" { | ||
| * source = "git@github.e.it.census.gov:terraform-modules/aws-common-security-groups.git//rds-postgres" | ||
| * | ||
| * # name = "rds-postgres" | ||
| * vpc_id = var.vpc_id | ||
| * # Name, CostAllocation, and Environment are pre-set | ||
| * # Name, CostAllocation, and Environment are pre-set, but they can be overriden | ||
| * # tags = { } | ||
| * } | ||
| * ``` | ||
| */ | ||
|
|
||
| data "aws_vpc" "this_vpc" { | ||
| count = var.use_vpc_cidr ? 1 : 0 | ||
| id = var.vpc_id | ||
| } | ||
|
|
||
| data "aws_security_group" "ingress_security_groups" { | ||
| count = length(var.ingress_security_groups) | ||
| id = element(var.ingress_security_groups, count.index) | ||
| } | ||
|
|
||
| data "aws_security_group" "egress_security_groups" { | ||
| count = length(var.egress_security_groups) | ||
| id = element(var.egress_security_groups, count.index) | ||
| } | ||
|
|
||
| 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 ? [1] : [] | ||
| short_description = var.short_description == "" ? var.description : var.short_description | ||
| } | ||
|
|
||
| resource "aws_security_group" "this_security_group" { | ||
| name = local.name | ||
| description = local.description | ||
| description = var.description | ||
| vpc_id = var.vpc_id | ||
|
|
||
| # portlist | ||
| # ingresss external port list (list + vpc if enabaled) | ||
| dynamic "ingress" { | ||
| for_each = local.ports_map | ||
| for_each = local.port_map["external"] | ||
| iterator = p | ||
| content { | ||
| description = "${local.description}: ${p.value["description"]}" | ||
| 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 ? local.ingress_networks : p.value["cidr"] | ||
| cidr_blocks = length(p.value["cidr"]) == 0 ? local.external_ingress_networks : p.value["cidr"] | ||
| } | ||
| } | ||
|
|
||
| # 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 (list with one or zero items) | ||
| dynamic "ingress" { | ||
| for_each = local.self | ||
| iterator = sg | ||
| content { | ||
| description = "${local.short_description}: from self" | ||
| from_port = 0 | ||
| to_port = 0 | ||
| protocol = -1 | ||
| self = true | ||
| } | ||
| } | ||
|
|
||
| # egress all | ||
| egress { | ||
| description = "${local.description}: All" | ||
| description = "${local.short_description}: All" | ||
| from_port = 0 | ||
| to_port = 0 | ||
| protocol = -1 | ||
| cidr_blocks = local.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] | ||
| } | ||
| } | ||
|
|
||
| tags = merge( | ||
| map("Name", "sg-${local.name}"), | ||
| var.tags, | ||
| map("boc:tf_module_version", var._module_version), | ||
| map("boc:created_by", "terraform"), | ||
| map("boc:tf_module_version", local._module_version), | ||
| map("boc:vpc:info", join(" ", compact(list(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 |
|---|---|---|
| @@ -1,11 +1,33 @@ | ||
| # ports = list of list of | ||
| # from_port | ||
| # to_port | ||
| # proto | ||
| # description | ||
| # cidr_block | ||
| # list of: all, external (more added as needed) | ||
|
|
||
| locals { | ||
| description = "module: PostGres common ports" | ||
| name = var.name | ||
| description = "module: PostgreSQL common ports" | ||
| n_all = ["0.0.0.0/0"] | ||
| n_census = ["148.129.0.0/16", "192.168.0.0/16", "172.16.0.0/12", "10.0.0.0/8"] | ||
| source_groups = ["all", "external"] | ||
|
|
||
| name = var.name | ||
| ports = [ | ||
| [5482, 5482, "tcp", "postgres-db", []], | ||
| [5482, 5482, "tcp", "postgres-db", local.n_all, ["external"]], | ||
| ] | ||
| ingress_networks = var.networks | ||
|
|
||
| # these are ignored | ||
| ingress_networks = var.ingress_networks | ||
| egress_networks = var.egress_networks | ||
| ports_fields = ["from", "to", "proto", "description", "cidr"] | ||
| ports_map = [for p in local.ports : zipmap(local.ports_fields, p)] | ||
|
|
||
| # these are ignored | ||
| ingress_sg = var.ingress_security_groups | ||
| egress_sg = var.egress_security_groups | ||
|
|
||
| p_fields = ["from", "to", "proto", "description", "cidr", "source_group"] | ||
| p_map = [for p in local.ports : zipmap(local.p_fields, p)] | ||
| port_map = { for s in local.source_groups : | ||
| s => [for p in local.p_map : p if contains(p["source_group"], s)] | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| variable "_module_version" { | ||
| description = "Module version number" | ||
| type = string | ||
| default = "1.0" | ||
| locals { | ||
| _module_version = "1.0.0" | ||
| } |