Skip to content

Commit 2e1c9a3

Browse files
committed
fix: Add create_kms_key variable.
1 parent b5d144f commit 2e1c9a3

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,17 @@ tofu init -upgrade
6060

6161
## Inputs
6262

63-
| Name | Description | Type | Default | Required |
64-
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------- | -------- |
65-
| project | Name of the project. | `string` | n/a | yes |
66-
| 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 |
67-
| environment | Environment for the project. | `string` | `"dev"` | no |
68-
| 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 |
70-
| [secrets] | Secrets to be created. | `map(object)` | `{}` | no |
71-
| service | Optional service that these resources are supporting. Example: `"api"`, `"web"`, `"worker"` | `string` | n/a | no |
72-
| tags | Optional tags to be applied to all resources. | `list` | `[]` | no |
63+
| Name | Description | Type | Default | Required |
64+
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------- | ----------- |
65+
| project | Name of the project. | `string` | n/a | yes |
66+
| kms_key_arn | ARN for an existing KMS key to use for encryption. Required if `create_kms_key` is set to `false`; ignored otherwise. | `string` | `null` | conditional |
67+
| 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 |
68+
| create_kms_key | Whether to create a new KMS key for encrypting secrets. If set to `false`, `kms_key_arn` must be provided. | `bool` | `true` | no |
69+
| environment | Environment for the project. | `string` | `"dev"` | no |
70+
| key_recovery_period | Recovery period for deleted KMS keys in days. Must be between 7 and 30. Only used if `create_kms_key` is set to `true`. | `number` | `30` | no |
71+
| [secrets] | Secrets to be created. | `map(object)` | `{}` | no |
72+
| service | Optional service that these resources are supporting. Example: `"api"`, `"web"`, `"worker"` | `string` | n/a | no |
73+
| tags | Optional tags to be applied to all resources. | `list` | `[]` | no |
7374

7475
### secrets
7576

data.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ data "aws_partition" "current" {}
55
data "aws_region" "current" {}
66

77
data "aws_kms_key" "secrets" {
8-
for_each = var.kms_key_arn != null ? toset(["this"]) : toset([])
8+
for_each = var.create_kms_key ? toset([]) : toset(["this"])
99

1010
key_id = var.kms_key_arn
1111
}

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ output "kms_key_alias" {
33
Alias for the created KMS key. If `kms_key_arn`is provided, this will be
44
`null`.
55
EOT
6-
value = var.kms_key_arn == null ? aws_kms_alias.secrets["this"].name : null
6+
value = var.create_kms_key ? aws_kms_alias.secrets["this"].name : null
77
}
88

99
output "kms_key_arn" {

variables.tf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ variable "add_suffix" {
44
default = true
55
}
66

7+
variable "create_kms_key" {
8+
type = bool
9+
description = <<-EOT
10+
Whether to create a new KMS key for encrypting secrets. If set to `false`,
11+
`kms_key_arn` must be provided.
12+
EOT
13+
default = true
14+
}
15+
716
variable "environment" {
817
type = string
918
description = "Environment for the deployment."
@@ -13,7 +22,10 @@ variable "environment" {
1322
variable "key_recovery_period" {
1423
type = number
1524
default = 30
16-
description = "Recovery period for deleted KMS keys in days. Must be between 7 and 30."
25+
description = <<-EOT
26+
Recovery period for deleted KMS keys in days. Must be between 7 and 30. Only
27+
used if `create_kms_key` is set to `true`.
28+
EOT
1729

1830
validation {
1931
condition = var.key_recovery_period > 6 && var.key_recovery_period < 31
@@ -24,8 +36,8 @@ variable "key_recovery_period" {
2436
variable "kms_key_arn" {
2537
type = string
2638
description = <<-EOT
27-
Optional KMS key ARN to use for encryption. If not provided, a new KMS key
28-
will be created.
39+
ARN for an existing KMS key to use for encryption. Required if
40+
`create_kms_key` is set to `false`; ignored otherwise.
2941
EOT
3042
default = null
3143
}

0 commit comments

Comments
 (0)