-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
229 lines (196 loc) · 6.63 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
locals {
dynamo_hash_key = "LockID"
dynamo_enable_autoscaler = false
dynamo_attributes = concat(module.this.attributes, ["lock"])
bucket_versioning_enabled = true
bucket_arn = format("arn:aws:s3:::%s", module.this.id)
bucket_attributes = concat(module.this.attributes, [])
bucket_kms_arn = "*"
bucket_kms_key = var.state_kms == "master" ? "" : (var.state_kms == "auto" ? module.state_auth_kms_key.key_arn : var.state_kms)
bucket_kms_attributes = concat(module.this.attributes, ["key"])
bucket_policies = [
data.aws_iam_policy_document.state_policy_root
]
bucket_kms_policies = [
data.aws_iam_policy_document.state_kms_policy_root
]
}
data "aws_caller_identity" "provider" {}
module "state_lock" {
source = "cloudposse/dynamodb/aws"
version = "0.33.0"
enabled = var.lock
attributes = local.dynamo_attributes
hash_key = local.dynamo_hash_key
enable_autoscaler = local.dynamo_enable_autoscaler
context = module.this.context
billing_mode = var.dynamo_billing_mode
autoscale_min_read_capacity = var.dynamo_min_read_capacity
autoscale_min_write_capacity = var.dynamo_min_write_capacity
}
data "aws_iam_policy_document" "state_policy_root" {
statement {
sid = "AllowManageRootStateBucket"
effect = "Allow"
actions = [
"s3:*",
]
resources = [
format("%s", local.bucket_arn),
format("%s/*", local.bucket_arn)
]
principals {
type = "AWS"
identifiers = [
format("arn:aws:iam::%s:root", data.aws_caller_identity.provider.account_id)
]
}
}
}
# Merge multiple aws_iam_policy_document to one aws_iam_policy_document and set resource in all statement to s3 bucket arn
data "aws_iam_policy_document" "state_bucket_policy" {
dynamic "statement" {
for_each = flatten([
for policy in concat(var.state_policies, local.bucket_policies) : policy.statement
])
content {
sid = lookup(statement.value, "sid", null)
effect = lookup(statement.value, "effect", null)
actions = lookup(statement.value, "actions", null)
not_actions = lookup(statement.value, "not_actions", null)
resources = [
format("%s", local.bucket_arn),
format("%s/*", local.bucket_arn)
]
dynamic "principals" {
for_each = toset([
for principal in statement.value.principals : {
for key, value in principal : key => value
}
if lookup(statement.value, "principals", null) != null
])
content {
identifiers = principals.value.identifiers
type = principals.value.type
}
}
dynamic "not_principals" {
for_each = toset([
for not_principals in statement.value.not_principals : {
for key, value in not_principals : key => value
}
if lookup(statement.value, "not_principals", null) != null
])
content {
identifiers = not_principals.value.identifiers
type = not_principals.value.type
}
}
dynamic "condition" {
for_each = toset([
for condition in statement.value.condition : {
for key, value in condition : key => value
}
if lookup(statement.value, "condition", null) != null
])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
}
module "state_bucket" {
source = "cloudposse/s3-bucket/aws"
version = "3.1.2"
enabled = var.state
attributes = local.bucket_attributes
versioning_enabled = local.bucket_versioning_enabled
sse_algorithm = var.state_sse
kms_master_key_arn = local.bucket_kms_key
source_policy_documents = [data.aws_iam_policy_document.state_bucket_policy.json]
allow_ssl_requests_only = var.allow_ssl_requests_only
s3_object_ownership = "BucketOwnerEnforced"
context = module.this.context
}
data "aws_iam_policy_document" "state_kms_policy_root" {
statement {
sid = "AllowManageRootStateBucketKMS"
effect = "Allow"
actions = [
"kms:*",
]
resources = [
format("%s", local.bucket_arn),
format("%s/*", local.bucket_arn)
]
principals {
type = "AWS"
identifiers = [
format("arn:aws:iam::%s:root", data.aws_caller_identity.provider.account_id)
]
}
}
}
# Merge multiple aws_iam_policy_document to one aws_iam_policy_document and set resource in all statement to kms arn
data "aws_iam_policy_document" "state_kms_policy" {
dynamic "statement" {
for_each = flatten([
for policy in concat(var.state_kms_policies, local.bucket_kms_policies) : policy.statement
])
content {
sid = lookup(statement.value, "sid", null)
effect = lookup(statement.value, "effect", null)
actions = lookup(statement.value, "actions", null)
not_actions = lookup(statement.value, "not_actions", null)
resources = [local.bucket_kms_arn]
dynamic "principals" {
for_each = toset([
for principal in statement.value.principals : {
for key, value in principal : key => value
}
if lookup(statement.value, "principals", null) != null
])
content {
identifiers = principals.value.identifiers
type = principals.value.type
}
}
dynamic "not_principals" {
for_each = toset([
for not_principals in statement.value.not_principals : {
for key, value in not_principals : key => value
}
if lookup(statement.value, "not_principals", null) != null
])
content {
identifiers = not_principals.value.identifiers
type = not_principals.value.type
}
}
dynamic "condition" {
for_each = toset([
for condition in statement.value.condition : {
for key, value in condition : key => value
}
if lookup(statement.value, "condition", null) != null
])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
}
module "state_auth_kms_key" {
source = "cloudposse/kms-key/aws"
version = "0.12.1"
enabled = var.state_kms == "auto" ? true : false
attributes = local.bucket_kms_attributes
policy = data.aws_iam_policy_document.state_kms_policy.json
context = module.this.context
}