Skip to content

Commit b5d144f

Browse files
committed
feat: Allow using an existing KMS key.
1 parent b71751c commit b5d144f

File tree

7 files changed

+40
-13
lines changed

7 files changed

+40
-13
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ tofu init -upgrade
6161
## Inputs
6262

6363
| Name | Description | Type | Default | Required |
64-
|---------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|---------------|---------|----------|
64+
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------- | -------- |
6565
| project | Name of the project. | `string` | n/a | yes |
6666
| add_suffix | Apply a random suffix to the secret name. Useful when secrets may need to be replaced, but makes identify secrets by name alone more difficult. | `bool` | `true` | no |
6767
| environment | Environment for the project. | `string` | `"dev"` | no |
6868
| key_recovery_period | Number of days to recover the KMS key after deletion. | `number` | `30` | no |
69+
| kms_key_arn | Optional KMS key ARN to use for encryption. If not provided, a new KMS key will be created. | `string` | `null` | no |
6970
| [secrets] | Secrets to be created. | `map(object)` | `{}` | no |
7071
| service | Optional service that these resources are supporting. Example: `"api"`, `"web"`, `"worker"` | `string` | n/a | no |
7172
| tags | Optional tags to be applied to all resources. | `list` | `[]` | no |
@@ -107,7 +108,7 @@ This would result in a key named `my/example/key-` before the random suffix is
107108
applied.
108109

109110
| Name | Description | Type | Default | Required |
110-
|------------------------|---------------------------------------------------------------|----------|---------|----------|
111+
| ---------------------- | ------------------------------------------------------------- | -------- | ------- | -------- |
111112
| description | Description of the secret. | `string` | n/a | yes |
112113
| create_random_password | Creates a random password as the staring value. | `bool` | `false` | no |
113114
| name | Name to use as the prefix for the secret. | `string` | `""` | no |
@@ -116,11 +117,11 @@ applied.
116117

117118
## Outputs
118119

119-
| Name | Description | Type |
120-
|---------------|-----------------------------------------------|---------------|
121-
| kms_key_alias | Alias for of the KMS key used for encryption. | `string` |
122-
| kms_key_arn | ARN for of the KMS key used for encryption. | `string` |
123-
| secrets | A map of created secrets. | `map(object)` |
120+
| Name | Description | Type |
121+
| ------------- | -------------------------------------------------------------------------------- | ------------- |
122+
| kms_key_alias | Alias for the created KMS key. If `kms_key_arn`is provided, this will be `null`. | `string` |
123+
| kms_key_arn | ARN of the KMS key used for encryption. | `string` |
124+
| secrets | A map of created secrets. | `map(object)` |
124125

125126
[2.0.0]: CHANGELOG.md#200-2025-08-19
126127
[badge-release]: https://img.shields.io/github/v/release/codeforamerica/tofu-modules-aws-secrets?logo=github&label=Latest%20Release

data.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ data "aws_caller_identity" "identity" {}
33
data "aws_partition" "current" {}
44

55
data "aws_region" "current" {}
6+
7+
data "aws_kms_key" "secrets" {
8+
for_each = var.kms_key_arn != null ? toset(["this"]) : toset([])
9+
10+
key_id = var.kms_key_arn
11+
}

kms.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
resource "aws_kms_key" "secrets" {
2+
for_each = var.kms_key_arn == null ? toset(["this"]) : toset([])
3+
24
description = "Secrets encryption key for ${var.project} ${var.environment}"
35
deletion_window_in_days = var.key_recovery_period
46
enable_key_rotation = true
@@ -12,6 +14,8 @@ resource "aws_kms_key" "secrets" {
1214
}
1315

1416
resource "aws_kms_alias" "secrets" {
17+
for_each = var.kms_key_arn == null ? toset(["this"]) : toset([])
18+
1519
name = "alias/${var.project}/${var.environment}/${var.service != "" ? "${var.service}/" : ""}secrets"
16-
target_key_id = aws_kms_key.secrets.id
20+
target_key_id = aws_kms_key.secrets["this"].id
1721
}

locals.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
locals {
2+
kms_key_arn = var.kms_key_arn != null ? var.kms_key_arn : aws_kms_key.secrets["this"].arn
3+
kms_key_id = var.kms_key_arn != null ? data.aws_kms_key.secrets["this"].id : aws_kms_key.secrets["this"].id
4+
}

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module "secrets_manager" {
1515
create_random_password = each.value.create_random_password
1616
description = each.value.description
1717
recovery_window_in_days = each.value.recovery_window
18-
kms_key_id = aws_kms_alias.secrets.id
18+
kms_key_id = local.kms_key_id
1919
secret_string = each.value.start_value
2020

2121
ignore_secret_changes = true

outputs.tf

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
output "kms_key_alias" {
2-
description = "Alias for of the KMS key used for encryption."
3-
value = aws_kms_alias.secrets.name
2+
description = <<-EOT
3+
Alias for the created KMS key. If `kms_key_arn`is provided, this will be
4+
`null`.
5+
EOT
6+
value = var.kms_key_arn == null ? aws_kms_alias.secrets["this"].name : null
47
}
58

69
output "kms_key_arn" {
7-
description = "ARN for of the KMS key used for encryption."
8-
value = aws_kms_key.secrets.arn
10+
description = "ARN of the KMS key used for encryption."
11+
value = local.kms_key_arn
912
}
1013

1114
output "secrets" {

variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ variable "key_recovery_period" {
2121
}
2222
}
2323

24+
variable "kms_key_arn" {
25+
type = string
26+
description = <<-EOT
27+
Optional KMS key ARN to use for encryption. If not provided, a new KMS key
28+
will be created.
29+
EOT
30+
default = null
31+
}
32+
2433
variable "project" {
2534
type = string
2635
description = "Project that these resources are supporting."

0 commit comments

Comments
 (0)