Skip to content

Commit 8276e1a

Browse files
authored
feat: Add security group egress rule support, fix documentation links (#249)
1 parent 3d996a2 commit 8276e1a

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
2-
- repo: git://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.52.0
2+
- repo: https://github.com/antonbabenko/pre-commit-terraform
3+
rev: v1.55.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate
@@ -22,7 +22,7 @@ repos:
2222
- '--args=--only=terraform_required_providers'
2323
- '--args=--only=terraform_standard_module_structure'
2424
- '--args=--only=terraform_workspace_remote'
25-
- repo: git://github.com/pre-commit/pre-commit-hooks
25+
- repo: https://github.com/pre-commit/pre-commit-hooks
2626
rev: v4.0.1
2727
hooks:
2828
- id: check-merge-conflict

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ module "cluster" {
204204

205205
## Examples
206206

207-
- [Autoscaling](examples/autoscaling): A PostgreSQL cluster with enhanced monitoring and autoscaling enabled
208-
- [Global Cluster](examples/global_cluster): A PostgreSQL global cluster with clusters provisioned in two different region
209-
- [MySQL](examples/mysql): A simple MySQL cluster
210-
- [PostgreSQL](examples/postgresql): A simple PostgreSQL cluster
211-
- [S3 Import](examples/s3_import): A MySQL cluster created from a Percona Xtrabackup stored in S3
212-
- [Serverless](examples/serverless): Serverless PostgreSQL and MySQL clusters
207+
- [Autoscaling](https://github.com/terraform-aws-modules/terraform-aws-rds-aurora/tree/master/examples/autoscaling): A PostgreSQL cluster with enhanced monitoring and autoscaling enabled
208+
- [Global Cluster](https://github.com/terraform-aws-modules/terraform-aws-rds-aurora/tree/master/examples/global_cluster): A PostgreSQL global cluster with clusters provisioned in two different region
209+
- [MySQL](https://github.com/terraform-aws-modules/terraform-aws-rds-aurora/tree/master/examples/mysql): A simple MySQL cluster
210+
- [PostgreSQL](https://github.com/terraform-aws-modules/terraform-aws-rds-aurora/tree/master/examples/postgresql): A simple PostgreSQL cluster
211+
- [S3 Import](https://github.com/terraform-aws-modules/terraform-aws-rds-aurora/tree/master/examples/s3_import): A MySQL cluster created from a Percona Xtrabackup stored in S3
212+
- [Serverless](https://github.com/terraform-aws-modules/terraform-aws-rds-aurora/tree/master/examples/serverless): Serverless PostgreSQL and MySQL clusters
213213

214214
## Documentation
215215

@@ -251,6 +251,7 @@ No modules.
251251
| [aws_security_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
252252
| [aws_security_group_rule.cidr_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
253253
| [aws_security_group_rule.default_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
254+
| [aws_security_group_rule.egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
254255
| [random_id.snapshot_identifier](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource |
255256
| [random_password.master_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |
256257
| [aws_iam_policy_document.monitoring_rds_assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -333,6 +334,7 @@ No modules.
333334
| <a name="input_s3_import"></a> [s3\_import](#input\_s3\_import) | Configuration map used to restore from a Percona Xtrabackup in S3 (only MySQL is supported) | `map(string)` | `null` | no |
334335
| <a name="input_scaling_configuration"></a> [scaling\_configuration](#input\_scaling\_configuration) | Map of nested attributes with scaling properties. Only valid when `engine_mode` is set to `serverless` | `map(string)` | `{}` | no |
335336
| <a name="input_security_group_description"></a> [security\_group\_description](#input\_security\_group\_description) | The description of the security group. If value is set to empty string it will contain cluster name in the description | `string` | `null` | no |
337+
| <a name="input_security_group_egress_rules"></a> [security\_group\_egress\_rules](#input\_security\_group\_egress\_rules) | A map of security group egress rule defintions to add to the security group created | `map(any)` | `{}` | no |
336338
| <a name="input_security_group_tags"></a> [security\_group\_tags](#input\_security\_group\_tags) | Additional tags for the security group | `map(string)` | `{}` | no |
337339
| <a name="input_skip_final_snapshot"></a> [skip\_final\_snapshot](#input\_skip\_final\_snapshot) | Determines whether a final snapshot is created before the cluster is deleted. If true is specified, no snapshot is created | `bool` | `null` | no |
338340
| <a name="input_snapshot_identifier"></a> [snapshot\_identifier](#input\_snapshot\_identifier) | Specifies whether or not to create this cluster from a snapshot. You can use either the name or ARN when specifying a DB cluster snapshot, or the ARN when specifying a DB snapshot | `string` | `null` | no |

examples/postgresql/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ module "aurora" {
8383
create_db_subnet_group = false
8484
create_security_group = true
8585
allowed_cidr_blocks = module.vpc.private_subnets_cidr_blocks
86+
security_group_egress_rules = {
87+
to_cidrs = {
88+
cidr_blocks = ["10.33.0.0/28"]
89+
description = "Egress to corporate printer closet"
90+
}
91+
}
8692

8793
iam_database_authentication_enabled = true
8894
master_password = random_password.master.result

main.tf

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
locals {
2-
port = var.port == "" ? (var.engine == "aurora-postgresql" ? 5432 : 3306) : var.port
2+
port = coalesce(var.port, (var.engine == "aurora-postgresql" ? 5432 : 3306))
33

44
db_subnet_group_name = var.create_db_subnet_group ? join("", aws_db_subnet_group.this.*.name) : var.db_subnet_group_name
55
internal_db_subnet_group_name = try(coalesce(var.db_subnet_group_name, var.name), "")
@@ -8,9 +8,7 @@ locals {
88

99
rds_enhanced_monitoring_arn = var.create_monitoring_role ? join("", aws_iam_role.rds_enhanced_monitoring.*.arn) : var.monitoring_role_arn
1010
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"
1412
}
1513

1614
# 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" {
292290
})
293291
}
294292

293+
# TODO - change to map of ingress rules under one resource at next breaking change
295294
resource "aws_security_group_rule" "default_ingress" {
296295
count = var.create_cluster && var.create_security_group ? length(var.allowed_security_groups) : 0
297296

298297
description = "From allowed SGs"
299298

300299
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
303302
protocol = "tcp"
304303
source_security_group_id = element(var.allowed_security_groups, count.index)
305304
security_group_id = local.rds_security_group_id
306305
}
307306

307+
# TODO - change to map of ingress rules under one resource at next breaking change
308308
resource "aws_security_group_rule" "cidr_ingress" {
309309
count = var.create_cluster && var.create_security_group && length(var.allowed_cidr_blocks) > 0 ? 1 : 0
310310

311311
description = "From allowed CIDRs"
312312

313313
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
316316
protocol = "tcp"
317317
cidr_blocks = var.allowed_cidr_blocks
318318
security_group_id = local.rds_security_group_id
319319
}
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+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,9 @@ variable "allowed_cidr_blocks" {
497497
type = list(string)
498498
default = []
499499
}
500+
501+
variable "security_group_egress_rules" {
502+
description = "A map of security group egress rule defintions to add to the security group created"
503+
type = map(any)
504+
default = {}
505+
}

0 commit comments

Comments
 (0)