Skip to content

Commit 7a4569f

Browse files
authored
Merge pull request #138 from MaterializeInc/aws-rds-customer-managed-kms
add customer managed kms encryption to rds and update readmes
2 parents a5ec939 + f9cd71c commit 7a4569f

File tree

6 files changed

+90
-2
lines changed

6 files changed

+90
-2
lines changed

aws/examples/simple/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,13 @@ output "external_login_password_mz_system" {
116116
value = random_password.external_login_password_mz_system.result
117117
sensitive = true
118118
}
119+
120+
output "rds_kms_key_alias" {
121+
description = "KMS Key used to encrypt the RDS instance"
122+
value = module.database.kms_key_alias
123+
}
124+
125+
output "rds_kms_key_arn" {
126+
description = "KMS Key used to encrypt the RDS instance"
127+
value = module.database.kms_key_arn
128+
}

aws/modules/database/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
| Name | Type |
2323
|------|------|
24+
| [aws_kms_alias.rds](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
25+
| [aws_kms_key.rds](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
2426
| [aws_security_group.database](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
2527
| [aws_security_group_rule.allow_all_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
2628
| [aws_security_group_rule.eks_cluster_postgres_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
@@ -35,11 +37,15 @@
3537
| <a name="input_backup_window"></a> [backup\_window](#input\_backup\_window) | Preferred backup window | `string` | `"03:00-06:00"` | no |
3638
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of the EKS cluster | `string` | n/a | yes |
3739
| <a name="input_cluster_security_group_id"></a> [cluster\_security\_group\_id](#input\_cluster\_security\_group\_id) | Security group ID of the EKS cluster | `string` | n/a | yes |
40+
| <a name="input_create_kms_key"></a> [create\_kms\_key](#input\_create\_kms\_key) | Whether to create a new KMS key for RDS encryption. If false and kms\_key\_id is not specified, the default AWS managed key will be used. | `bool` | `false` | no |
3841
| <a name="input_database_name"></a> [database\_name](#input\_database\_name) | Name of the database to create | `string` | n/a | yes |
3942
| <a name="input_database_password"></a> [database\_password](#input\_database\_password) | Password for the database | `string` | n/a | yes |
4043
| <a name="input_database_subnet_ids"></a> [database\_subnet\_ids](#input\_database\_subnet\_ids) | List of subnet IDs for the database | `list(string)` | n/a | yes |
4144
| <a name="input_database_username"></a> [database\_username](#input\_database\_username) | Username for the database | `string` | n/a | yes |
4245
| <a name="input_instance_class"></a> [instance\_class](#input\_instance\_class) | Instance class for the RDS instance | `string` | n/a | yes |
46+
| <a name="input_kms_key_deletion_window_in_days"></a> [kms\_key\_deletion\_window\_in\_days](#input\_kms\_key\_deletion\_window\_in\_days) | The waiting period, specified in number of days, after which AWS KMS deletes the KMS key. Valid values are 7-30 days. | `number` | `30` | no |
47+
| <a name="input_kms_key_enable_rotation"></a> [kms\_key\_enable\_rotation](#input\_kms\_key\_enable\_rotation) | Specifies whether key rotation is enabled for the KMS key. | `bool` | `true` | no |
48+
| <a name="input_kms_key_id"></a> [kms\_key\_id](#input\_kms\_key\_id) | The ARN for the KMS encryption key. If not specified and create\_kms\_key is false, the default AWS managed key will be used. | `string` | `null` | no |
4349
| <a name="input_maintenance_window"></a> [maintenance\_window](#input\_maintenance\_window) | Preferred maintenance window | `string` | `"Mon:00:00-Mon:03:00"` | no |
4450
| <a name="input_max_allocated_storage"></a> [max\_allocated\_storage](#input\_max\_allocated\_storage) | Maximum storage for autoscaling (in GB) | `number` | `100` | no |
4551
| <a name="input_multi_az"></a> [multi\_az](#input\_multi\_az) | Enable multi-AZ deployment | `bool` | `false` | no |
@@ -59,3 +65,6 @@
5965
| <a name="output_db_instance_port"></a> [db\_instance\_port](#output\_db\_instance\_port) | The database port |
6066
| <a name="output_db_instance_username"></a> [db\_instance\_username](#output\_db\_instance\_username) | The master username for the database |
6167
| <a name="output_db_security_group_id"></a> [db\_security\_group\_id](#output\_db\_security\_group\_id) | The security group ID of the database |
68+
| <a name="output_kms_key_alias"></a> [kms\_key\_alias](#output\_kms\_key\_alias) | The alias of the KMS key used for RDS encryption |
69+
| <a name="output_kms_key_arn"></a> [kms\_key\_arn](#output\_kms\_key\_arn) | The ARN of the KMS key used for RDS encryption |
70+
| <a name="output_kms_key_id"></a> [kms\_key\_id](#output\_kms\_key\_id) | The ID of the KMS key used for RDS encryption (only if created by this module) |

aws/modules/database/main.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
# KMS key for RDS encryption at rest
2+
# NOTE: Once an RDS instance is created with this KMS key, it cannot be changed.
3+
# Deleting this key while the database exists will make the database inaccessible.
4+
# The deletion_window_in_days provides a recovery period - use `aws kms cancel-key-deletion`
5+
# to recover if deleted accidentally. See: https://repost.aws/knowledge-center/update-encryption-key-rds
6+
resource "aws_kms_key" "rds" {
7+
count = var.create_kms_key ? 1 : 0
8+
9+
description = "KMS key for RDS encryption - ${var.name_prefix}"
10+
deletion_window_in_days = var.kms_key_deletion_window_in_days
11+
enable_key_rotation = var.kms_key_enable_rotation
12+
13+
tags = merge(var.tags, {
14+
Name = "${var.name_prefix}-rds-kms-key"
15+
})
16+
}
17+
18+
resource "aws_kms_alias" "rds" {
19+
count = var.create_kms_key ? 1 : 0
20+
21+
name = "alias/${var.name_prefix}-rds"
22+
target_key_id = aws_kms_key.rds[0].key_id
23+
}
24+
25+
locals {
26+
# Use created KMS key if create_kms_key is true, otherwise use provided kms_key_id (or null for AWS-managed key)
27+
kms_key_arn = var.create_kms_key ? aws_kms_key.rds[0].arn : var.kms_key_id
28+
}
29+
130
module "db" {
231
source = "terraform-aws-modules/rds/aws"
332
version = "~> 6.0"
@@ -16,6 +45,7 @@ module "db" {
1645
allocated_storage = var.allocated_storage
1746
max_allocated_storage = var.max_allocated_storage
1847
storage_encrypted = true
48+
kms_key_id = local.kms_key_arn
1949

2050
db_name = var.database_name
2151
username = var.database_username

aws/modules/database/outputs.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,18 @@ output "db_security_group_id" {
2828
description = "The security group ID of the database"
2929
value = aws_security_group.database.id
3030
}
31+
32+
output "kms_key_arn" {
33+
description = "The ARN of the KMS key used for RDS encryption"
34+
value = local.kms_key_arn
35+
}
36+
37+
output "kms_key_id" {
38+
description = "The ID of the KMS key used for RDS encryption (only if created by this module)"
39+
value = var.create_kms_key ? aws_kms_key.rds[0].key_id : null
40+
}
41+
42+
output "kms_key_alias" {
43+
description = "The alias of the KMS key used for RDS encryption"
44+
value = var.create_kms_key ? aws_kms_alias.rds[0].name : null
45+
}

aws/modules/database/variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,27 @@ variable "tags" {
112112
type = map(string)
113113
default = {}
114114
}
115+
116+
variable "kms_key_id" {
117+
description = "The ARN for the KMS encryption key. If not specified and create_kms_key is false, the default AWS managed key will be used."
118+
type = string
119+
default = null
120+
}
121+
122+
variable "create_kms_key" {
123+
description = "Whether to create a new KMS key for RDS encryption. If false and kms_key_id is not specified, the default AWS managed key will be used."
124+
type = bool
125+
default = false
126+
}
127+
128+
variable "kms_key_deletion_window_in_days" {
129+
description = "The waiting period, specified in number of days, after which AWS KMS deletes the KMS key. Valid values are 7-30 days."
130+
type = number
131+
default = 30
132+
}
133+
134+
variable "kms_key_enable_rotation" {
135+
description = "Specifies whether key rotation is enabled for the KMS key."
136+
type = bool
137+
default = true
138+
}

kubernetes/modules/materialize-instance/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ No modules.
4545
| <a name="input_instance_namespace"></a> [instance\_namespace](#input\_instance\_namespace) | Kubernetes namespace for the instance. | `string` | n/a | yes |
4646
| <a name="input_issuer_ref"></a> [issuer\_ref](#input\_issuer\_ref) | Reference to a cert-manager Issuer or ClusterIssuer. | <pre>object({<br/> name = string<br/> kind = string<br/> })</pre> | `null` | no |
4747
| <a name="input_license_key"></a> [license\_key](#input\_license\_key) | Materialize license key | `string` | `null` | no |
48-
| <a name="input_memory_limit"></a> [memory\_limit](#input\_memory\_limit) | Memory limit for environmentd | `string` | `"1Gi"` | no |
49-
| <a name="input_memory_request"></a> [memory\_request](#input\_memory\_request) | Memory request for environmentd | `string` | `"1Gi"` | no |
48+
| <a name="input_memory_limit"></a> [memory\_limit](#input\_memory\_limit) | Memory limit for environmentd | `string` | `"4Gi"` | no |
49+
| <a name="input_memory_request"></a> [memory\_request](#input\_memory\_request) | Memory request for environmentd | `string` | `"4095Mi"` | no |
5050
| <a name="input_metadata_backend_url"></a> [metadata\_backend\_url](#input\_metadata\_backend\_url) | PostgreSQL connection URL for metadata backend | `string` | n/a | yes |
5151
| <a name="input_persist_backend_url"></a> [persist\_backend\_url](#input\_persist\_backend\_url) | S3 connection URL for persist backend | `string` | n/a | yes |
5252
| <a name="input_pod_labels"></a> [pod\_labels](#input\_pod\_labels) | Labels for the materialize instance pod | `map(string)` | `{}` | no |

0 commit comments

Comments
 (0)