|
1 | 1 | locals {
|
2 |
| - port = var.port == "" ? (var.engine == "aurora-postgresql" ? 5432 : 3306) : var.port |
| 2 | + port = coalesce(var.port, (var.engine == "aurora-postgresql" ? 5432 : 3306)) |
3 | 3 |
|
4 | 4 | db_subnet_group_name = var.create_db_subnet_group ? join("", aws_db_subnet_group.this.*.name) : var.db_subnet_group_name
|
5 | 5 | internal_db_subnet_group_name = try(coalesce(var.db_subnet_group_name, var.name), "")
|
|
8 | 8 |
|
9 | 9 | rds_enhanced_monitoring_arn = var.create_monitoring_role ? join("", aws_iam_role.rds_enhanced_monitoring.*.arn) : var.monitoring_role_arn
|
10 | 10 | rds_security_group_id = join("", aws_security_group.this.*.id)
|
11 |
| - |
12 |
| - |
13 |
| - is_serverless = var.engine_mode == "serverless" |
| 11 | + is_serverless = var.engine_mode == "serverless" |
14 | 12 | }
|
15 | 13 |
|
16 | 14 | # Ref. https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#genref-aws-service-namespaces
|
@@ -292,28 +290,48 @@ resource "aws_security_group" "this" {
|
292 | 290 | })
|
293 | 291 | }
|
294 | 292 |
|
| 293 | +# TODO - change to map of ingress rules under one resource at next breaking change |
295 | 294 | resource "aws_security_group_rule" "default_ingress" {
|
296 | 295 | count = var.create_cluster && var.create_security_group ? length(var.allowed_security_groups) : 0
|
297 | 296 |
|
298 | 297 | description = "From allowed SGs"
|
299 | 298 |
|
300 | 299 | type = "ingress"
|
301 |
| - from_port = element(concat(aws_rds_cluster.this.*.port, [""]), 0) |
302 |
| - to_port = element(concat(aws_rds_cluster.this.*.port, [""]), 0) |
| 300 | + from_port = local.port |
| 301 | + to_port = local.port |
303 | 302 | protocol = "tcp"
|
304 | 303 | source_security_group_id = element(var.allowed_security_groups, count.index)
|
305 | 304 | security_group_id = local.rds_security_group_id
|
306 | 305 | }
|
307 | 306 |
|
| 307 | +# TODO - change to map of ingress rules under one resource at next breaking change |
308 | 308 | resource "aws_security_group_rule" "cidr_ingress" {
|
309 | 309 | count = var.create_cluster && var.create_security_group && length(var.allowed_cidr_blocks) > 0 ? 1 : 0
|
310 | 310 |
|
311 | 311 | description = "From allowed CIDRs"
|
312 | 312 |
|
313 | 313 | type = "ingress"
|
314 |
| - from_port = element(concat(aws_rds_cluster.this.*.port, [""]), 0) |
315 |
| - to_port = element(concat(aws_rds_cluster.this.*.port, [""]), 0) |
| 314 | + from_port = local.port |
| 315 | + to_port = local.port |
316 | 316 | protocol = "tcp"
|
317 | 317 | cidr_blocks = var.allowed_cidr_blocks
|
318 | 318 | security_group_id = local.rds_security_group_id
|
319 | 319 | }
|
| 320 | + |
| 321 | +resource "aws_security_group_rule" "egress" { |
| 322 | + for_each = var.create_cluster && var.create_security_group ? var.security_group_egress_rules : {} |
| 323 | + |
| 324 | + # required |
| 325 | + type = "egress" |
| 326 | + from_port = lookup(each.value, "from_port", local.port) |
| 327 | + to_port = lookup(each.value, "to_port", local.port) |
| 328 | + protocol = "tcp" |
| 329 | + security_group_id = local.rds_security_group_id |
| 330 | + |
| 331 | + # optional |
| 332 | + cidr_blocks = lookup(each.value, "cidr_blocks", null) |
| 333 | + description = lookup(each.value, "description", null) |
| 334 | + ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null) |
| 335 | + prefix_list_ids = lookup(each.value, "prefix_list_ids", null) |
| 336 | + source_security_group_id = lookup(each.value, "source_security_group_id", null) |
| 337 | +} |
0 commit comments