1
+ # ###----------------------------------------------------------------------------------
2
+ # # Provider block added, Use the Amazon Web Services (AWS) provider to interact with the many resources supported by AWS.
3
+ # ###----------------------------------------------------------------------------------
4
+ provider "aws" {
5
+ region = " us-east-1"
6
+ }
7
+
8
+ # ###----------------------------------------------------------------------------------
9
+ # # AWS External KMS Key Replica.
10
+ # # Should be deployed in different region as of primary key.
11
+ # ###----------------------------------------------------------------------------------
12
+ module "kms_key" {
13
+ source = " ./../../"
14
+ name = " kms"
15
+ environment = " test"
16
+ deletion_window_in_days = 7
17
+ alias = " alias/replicate_key"
18
+ kms_key_enabled = false
19
+ create_replica_external_enabled = true
20
+ enabled = true
21
+ multi_region = false
22
+ key_material_base64 = " Wblj06fduthWggmsT0cLVoIMOkeLbc2kVfMud77i/JY="
23
+ primary_key_arn = " arn:aws:kms:xxxxxxxxxxxxxxxxxxxxx"
24
+ policy = data. aws_iam_policy_document . default . json
25
+ }
26
+
27
+ data "aws_caller_identity" "current" {}
28
+ data "aws_partition" "current" {}
29
+
30
+ # #----------------------------------------------------------------------------------
31
+ # # Data block called to get Permissions that will be used in creating policy.
32
+ # #----------------------------------------------------------------------------------
33
+ data "aws_iam_policy_document" "default" {
34
+ version = " 2012-10-17"
35
+ statement {
36
+ sid = " Enable IAM User Permissions"
37
+ effect = " Allow"
38
+ principals {
39
+ type = " AWS"
40
+ identifiers = [
41
+ format (
42
+ " arn:%s:iam::%s:root" ,
43
+ join (" " , data. aws_partition . current . * . partition ),
44
+ data. aws_caller_identity . current . account_id
45
+ )
46
+ ]
47
+ }
48
+ actions = [" kms:*" ]
49
+ resources = [" *" ]
50
+ }
51
+ statement {
52
+ sid = " Allow CloudTrail to encrypt logs"
53
+ effect = " Allow"
54
+ principals {
55
+ type = " Service"
56
+ identifiers = [" cloudtrail.amazonaws.com" ]
57
+ }
58
+ actions = [" kms:GenerateDataKey*" ]
59
+ resources = [" *" ]
60
+ condition {
61
+ test = " StringLike"
62
+ variable = " kms:EncryptionContext:aws:cloudtrail:arn"
63
+ values = [" arn:aws:cloudtrail:*:XXXXXXXXXXXX:trail/*" ]
64
+ }
65
+ }
66
+
67
+ statement {
68
+ sid = " Allow CloudTrail to describe key"
69
+ effect = " Allow"
70
+ principals {
71
+ type = " Service"
72
+ identifiers = [" cloudtrail.amazonaws.com" ]
73
+ }
74
+ actions = [" kms:DescribeKey" ]
75
+ resources = [" *" ]
76
+ }
77
+
78
+ statement {
79
+ sid = " Allow principals in the account to decrypt log files"
80
+ effect = " Allow"
81
+ principals {
82
+ type = " AWS"
83
+ identifiers = [
84
+ format (
85
+ " arn:%s:iam::%s:root" ,
86
+ join (" " , data. aws_partition . current . * . partition ),
87
+ data. aws_caller_identity . current . account_id
88
+ )
89
+ ]
90
+ }
91
+ actions = [
92
+ " kms:Decrypt" ,
93
+ " kms:ReEncryptFrom"
94
+ ]
95
+ resources = [" *" ]
96
+ condition {
97
+ test = " StringEquals"
98
+ variable = " kms:CallerAccount"
99
+ values = [
100
+ " XXXXXXXXXXXX" ]
101
+ }
102
+ condition {
103
+ test = " StringLike"
104
+ variable = " kms:EncryptionContext:aws:cloudtrail:arn"
105
+ values = [" arn:aws:cloudtrail:*:XXXXXXXXXXXX:trail/*" ]
106
+ }
107
+ }
108
+
109
+ statement {
110
+ sid = " Allow alias creation during setup"
111
+ effect = " Allow"
112
+ principals {
113
+ type = " AWS"
114
+ identifiers = [
115
+ format (
116
+ " arn:%s:iam::%s:root" ,
117
+ join (" " , data. aws_partition . current . * . partition ),
118
+ data. aws_caller_identity . current . account_id
119
+ )
120
+ ]
121
+ }
122
+ actions = [" kms:CreateAlias" ]
123
+ resources = [" *" ]
124
+ }
125
+ }
0 commit comments