-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathkms-key.tf
90 lines (80 loc) · 2.03 KB
/
kms-key.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* As this is intended to enable a Key Administrator in a multi-account structure
the action and resource definition is broad */
data "aws_iam_policy_document" "this" {
statement {
sid = "Enable IAM User Permissions"
effect = "Allow"
actions = ["kms:*"]
resources = ["*"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${var.account_id}:root"]
}
}
statement {
sid = "Allow access for Key Administrators"
effect = "Allow"
actions = ["kms:*"]
resources = ["*"]
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${var.account_id}:role/${local.kms_admin_role_name}"
]
}
}
statement {
sid = "Allow use of the key"
effect = "Allow"
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt",
"kms:GenerateDataKey",
"kms:DescribeKey",
"kms:CreateGrant"
]
resources = ["*"]
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${var.account_id}:role/${local.kms_admin_role_name}"
]
}
}
statement {
sid = "Allow attachment of persistent resources"
effect = "Allow"
actions = [
"kms:CreateGrant",
"kms:ListGrants",
"kms:RevokeGrant"
]
resources = ["*"]
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${var.account_id}:role/${local.kms_admin_role_name}"
]
}
condition {
test = "Bool"
variable = "kms:GrantIsForAWSResource"
values = ["true"]
}
}
}
# Creates and manages KMS CMK
resource "aws_kms_key" "this" {
description = "EC2 Image Builder key"
is_enabled = true
enable_key_rotation = true
tags = local.core_tags
policy = data.aws_iam_policy_document.this.json
deletion_window_in_days = 30
}
# Add an alias to the key
resource "aws_kms_alias" "this" {
name = "alias/${var.kms_key_alias}"
target_key_id = aws_kms_key.this.key_id
}