forked from nozaq/terraform-aws-remote-state-s3-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replica.tf
307 lines (254 loc) · 7.87 KB
/
replica.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
locals {
replication_role_count = var.iam_role_arn == null && var.enable_replication ? 1 : 0
}
data "aws_region" "replica" {
count = var.enable_replication ? 1 : 0
provider = aws.replica
}
#---------------------------------------------------------------------------------------------------
# KMS Key to Encrypt S3 Bucket
#---------------------------------------------------------------------------------------------------
resource "aws_kms_key" "replica" {
count = var.enable_replication ? 1 : 0
provider = aws.replica
description = var.kms_key_description
deletion_window_in_days = var.kms_key_deletion_window_in_days
enable_key_rotation = var.kms_key_enable_key_rotation
tags = var.tags
}
#---------------------------------------------------------------------------------------------------
# Roles & Policies
#---------------------------------------------------------------------------------------------------
# IAM Role for Replication
# https://docs.aws.amazon.com/AmazonS3/latest/dev/crr-replication-config-for-kms-objects.html
resource "aws_iam_role" "replication" {
count = local.replication_role_count
name_prefix = var.override_iam_role_name ? null : var.iam_role_name_prefix
name = var.override_iam_role_name ? var.iam_role_name : null
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "s3.amazonaws.com"
},
"Effect": "Allow"
}
]
}
POLICY
permissions_boundary = var.iam_role_permissions_boundary
tags = var.tags
}
resource "aws_iam_policy" "replication" {
count = local.replication_role_count
name_prefix = var.override_iam_policy_name ? null : var.iam_policy_name_prefix
name = var.override_iam_policy_name ? var.iam_policy_name : null
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:GetReplicationConfiguration",
"s3:ListBucket"
],
"Effect": "Allow",
"Resource": [
"${aws_s3_bucket.state.arn}"
]
},
{
"Action": [
"s3:GetObjectVersionForReplication",
"s3:GetObjectVersionAcl"
],
"Effect": "Allow",
"Resource": [
"${aws_s3_bucket.state.arn}/*"
]
},
{
"Action": [
"s3:ReplicateObject",
"s3:ReplicateDelete"
],
"Effect": "Allow",
"Resource": "${aws_s3_bucket.replica[0].arn}/*"
},
{
"Effect": "Allow",
"Action": [
"kms:Decrypt"
],
"Resource": "${aws_kms_key.this.arn}",
"Condition": {
"StringLike": {
"kms:ViaService": "s3.${data.aws_region.state.name}.amazonaws.com",
"kms:EncryptionContext:aws:s3:arn": [
"${aws_s3_bucket.state.arn}/*"
]
}
}
},
{
"Effect": "Allow",
"Action": [
"kms:Encrypt",
"kms:GenerateDataKey"
],
"Resource": "${aws_kms_key.replica[0].arn}",
"Condition": {
"StringLike": {
"kms:ViaService": "s3.${data.aws_region.replica[0].name}.amazonaws.com",
"kms:EncryptionContext:aws:s3:arn": [
"${aws_s3_bucket.replica[0].arn}/*"
]
}
}
}
]
}
POLICY
tags = var.tags
}
resource "aws_iam_policy_attachment" "replication" {
count = local.replication_role_count
name = var.iam_policy_attachment_name
roles = [aws_iam_role.replication[0].name]
policy_arn = aws_iam_policy.replication[0].arn
}
data "aws_iam_policy_document" "replica_force_ssl" {
count = var.enable_replication ? 1 : 0
statement {
sid = "AllowSSLRequestsOnly"
actions = ["s3:*"]
effect = "Deny"
resources = [
aws_s3_bucket.replica[0].arn,
"${aws_s3_bucket.replica[0].arn}/*"
]
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
principals {
type = "*"
identifiers = ["*"]
}
}
}
#---------------------------------------------------------------------------------------------------
# Bucket
#---------------------------------------------------------------------------------------------------
resource "aws_s3_bucket" "replica" {
count = var.enable_replication ? 1 : 0
provider = aws.replica
bucket_prefix = var.override_s3_bucket_name ? null : var.replica_bucket_prefix
bucket = var.override_s3_bucket_name ? var.s3_bucket_name_replica : null
force_destroy = var.s3_bucket_force_destroy
tags = var.tags
}
resource "aws_s3_bucket_ownership_controls" "replica" {
count = var.enable_replication ? 1 : 0
provider = aws.replica
bucket = aws_s3_bucket.replica[0].id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_acl" "replica" {
depends_on = [aws_s3_bucket_ownership_controls.replica]
count = var.enable_replication ? 1 : 0
provider = aws.replica
bucket = aws_s3_bucket.replica[0].id
acl = "private"
}
resource "aws_s3_bucket_versioning" "replica" {
count = var.enable_replication ? 1 : 0
provider = aws.replica
bucket = aws_s3_bucket.replica[0].id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "replica" {
count = var.enable_replication ? 1 : 0
provider = aws.replica
bucket = aws_s3_bucket.replica[0].id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
kms_master_key_id = aws_kms_key.replica[0].arn
}
}
}
resource "aws_s3_bucket_lifecycle_configuration" "replica" {
count = var.enable_replication && local.define_lifecycle_rule ? 1 : 0
provider = aws.replica
bucket = aws_s3_bucket.replica[0].id
rule {
id = "auto-archive"
status = "Enabled"
dynamic "noncurrent_version_transition" {
for_each = var.noncurrent_version_transitions
content {
noncurrent_days = noncurrent_version_transition.value.days
storage_class = noncurrent_version_transition.value.storage_class
}
}
dynamic "noncurrent_version_expiration" {
for_each = var.noncurrent_version_expiration != null ? [var.noncurrent_version_expiration] : []
content {
noncurrent_days = noncurrent_version_expiration.value.days
}
}
}
}
resource "aws_s3_bucket_policy" "replica_force_ssl" {
count = var.enable_replication ? 1 : 0
provider = aws.replica
bucket = aws_s3_bucket.replica[0].id
policy = data.aws_iam_policy_document.replica_force_ssl[0].json
depends_on = [aws_s3_bucket_public_access_block.replica]
}
resource "aws_s3_bucket_public_access_block" "replica" {
count = var.enable_replication ? 1 : 0
provider = aws.replica
bucket = aws_s3_bucket.replica[0].id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_replication_configuration" "state" {
count = var.enable_replication == true ? 1 : 0
bucket = aws_s3_bucket.state.id
role = var.iam_role_arn != null ? var.iam_role_arn : aws_iam_role.replication[0].arn
rule {
id = "replica_configuration"
status = "Enabled"
filter {}
delete_marker_replication {
status = "Disabled"
}
source_selection_criteria {
sse_kms_encrypted_objects {
status = "Enabled"
}
}
destination {
bucket = aws_s3_bucket.replica[0].arn
storage_class = "STANDARD"
encryption_configuration {
replica_kms_key_id = aws_kms_key.replica[0].arn
}
}
}
# Versioning can't be disabled when the replication configuration exists.
# https://docs.aws.amazon.com/AmazonS3/latest/userguide/replication-and-other-bucket-configs.html#replication-and-versioning
depends_on = [aws_s3_bucket_versioning.state,aws_s3_bucket_versioning.replica]
}