Skip to content

Commit ba9e403

Browse files
committed
feat: Create a KMS key for shareable secrets
1 parent 0cc3897 commit ba9e403

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

kms.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
data "aws_iam_policy_document" "master" {
2+
count = length(local.arns) > 0 ? 1 : 0
3+
4+
statement {
5+
principals {
6+
type = "AWS"
7+
identifiers = [data.aws_caller_identity.current.account_id]
8+
}
9+
10+
actions = ["kms:*"]
11+
resources = ["*"]
12+
}
13+
14+
statement {
15+
principals {
16+
type = "AWS"
17+
identifiers = flatten(values(local.arns))
18+
}
19+
20+
actions = [
21+
"kms:Decrypt",
22+
"kms:DescribeKey",
23+
]
24+
resources = ["*"]
25+
}
26+
}
27+
28+
resource "aws_kms_key" "master" {
29+
count = length(local.arns) > 0 ? 1 : 0
30+
31+
description = "The master key for ${var.app_name} application"
32+
policy = data.aws_iam_policy_document.master[0].json
33+
34+
tags = var.tags
35+
}
36+
37+
resource "aws_kms_alias" "master" {
38+
count = length(local.arns) > 0 ? 1 : 0
39+
40+
name = "alias/${var.app_name}"
41+
42+
target_key_id = aws_kms_key.master[0].key_id
43+
}

main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ resource "aws_secretsmanager_secret" "app" {
2020
name_prefix = "${var.app_name}-${each.key}"
2121
description = "The ${title(replace(each.key, "-", " "))} secret for ${var.app_name} application"
2222

23+
kms_key_id = length(local.arns) > 0 ? aws_kms_key.master[0].key_id : null
24+
2325
policy = lookup(local.arns, each.key, null) == null ? null : data.aws_iam_policy_document.access[each.key].json
2426

2527
tags = merge(var.tags, { "service" = var.app_name })

0 commit comments

Comments
 (0)