This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
88 lines (77 loc) · 2.05 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
locals {
buckets = flatten(var.buckets)
replication_bucket = "arn:aws:s3:::${var.replication_bucket}/*"
}
# S3 bucket replication: role
resource "aws_iam_role" "default" {
name = "AWSS3BucketReplication${var.suffix_name}"
assume_role_policy = data.aws_iam_policy_document.s3-assume-role-policy.json
tags = var.tags
}
# S3 bucket replication: assume role policy
data "aws_iam_policy_document" "s3-assume-role-policy" {
version = "2012-10-17"
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["s3.amazonaws.com"]
}
}
}
resource "aws_iam_policy" "default" {
name = "AWSS3BucketReplicationPolicy${var.suffix_name}"
policy = data.aws_iam_policy_document.default-policy.json
}
# S3 bucket replication: role policy
# tfsec:ignore:aws-iam-no-policy-wildcards
data "aws_iam_policy_document" "default-policy" {
version = "2012-10-17"
statement {
effect = "Allow"
actions = [
"s3:GetReplicationConfiguration",
"s3:ListBucket"
]
resources = local.buckets
}
statement {
effect = "Allow"
actions = [
"s3:GetObjectVersion",
"s3:GetObjectVersionAcl",
"s3:GetObjectVersionForReplication",
"s3:GetObjectLegalHold",
"s3:GetObjectRetention",
"s3:GetObjectVersionTagging"
]
resources = [
for bucket in local.buckets :
"${bucket}/*"
]
}
statement {
effect = "Allow"
actions = [
"s3:ReplicateObject",
"s3:ReplicateDelete",
"s3:ReplicateTags",
"s3:GetObjectVersionTagging",
"s3:ObjectOwnerOverrideToBucketOwner"
]
resources = [var.replication_bucket != "" ? local.replication_bucket : "*"]
condition {
test = "StringLikeIfExists"
variable = "s3:x-amz-server-side-encryption"
values = [
"aws:kms",
"AES256"
]
}
}
}
resource "aws_iam_role_policy_attachment" "default" {
role = aws_iam_role.default.name
policy_arn = aws_iam_policy.default.arn
}