This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
123 lines (99 loc) · 2.33 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
data "aws_caller_identity" "current" {
}
data "aws_region" "current" {
}
data "aws_iam_policy_document" "key_policy" {
statement {
sid = "Allow account root access"
principals {
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
type = "AWS"
}
actions = ["*"]
resources = ["*"]
}
statement {
sid = "Allow access for key administrators"
principals {
identifiers = compact(var.principals)
type = "AWS"
}
actions = [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:TagResource",
"kms:UntagResource",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion",
]
resources = ["*"]
}
statement {
sid = "Allow use of the key by Elasticsearch Service"
principals {
identifiers = ["*"]
type = "AWS"
}
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey",
]
resources = ["*"]
condition {
test = "StringEquals"
values = ["es.${data.aws_region.current.name}.amazonaws.com"]
variable = "kms:ViaService"
}
condition {
test = "StringEquals"
values = concat(
[data.aws_caller_identity.current.account_id],
var.additional_account_ids
)
variable = "kms:CallerAccount"
}
}
statement {
sid = "Allow attachment of persistent resources"
principals {
identifiers = formatlist(
"arn:aws:iam::%s:root",
concat(
[data.aws_caller_identity.current.account_id],
var.additional_account_ids
)
)
type = "AWS"
}
actions = [
"kms:CreateGrant",
"kms:ListGrants",
"kms:RevokeGrant",
]
resources = ["*"]
condition {
test = "Bool"
values = ["true"]
variable = "kms:GrantIsForAWSResource"
}
}
}
resource "aws_kms_key" "key" {
enable_key_rotation = true
policy = data.aws_iam_policy_document.key_policy.json
}
resource "aws_kms_alias" "alias" {
name = "alias/${var.alias_name}"
target_key_id = aws_kms_key.key.key_id
}