-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy paths3.tf
170 lines (129 loc) · 3.72 KB
/
s3.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
#region Uploads
resource "aws_s3_bucket" "uploads" {
for_each = local.sites
bucket_prefix = "webcms-${each.key}-uploads-"
versioning {
enabled = true
}
lifecycle_rule {
enabled = true
noncurrent_version_expiration {
days = 90
}
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
# Optionally replicate to another bucket. The dynamic block here makes
# replication optional: if a key is missing for a given site-language pair,
# then replication will not be enabled.
dynamic "replication_configuration" {
for_each = toset(try([var.s3_replication_destination[each.key]], []))
content {
role = var.s3_replication_role
rules {
status = "Enabled"
delete_marker_replication_status = "Enabled"
filter {}
destination {
bucket = replication_configuration.value
}
}
}
}
tags = var.tags
}
# This policy allows anonymous reads to the /files/ prefix of the uploads bucket, which
# we need in order to satisfy s3fs - it only uses one bucket for both public and private
# files.
data "aws_iam_policy_document" "uploads_policy" {
for_each = local.sites
version = "2012-10-17"
statement {
sid = "AddPerm"
effect = "Allow"
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.uploads[each.key].arn}/files/*", "${aws_s3_bucket.uploads[each.key].arn}/archive/*"]
principals {
type = "*"
identifiers = ["*"]
}
}
}
resource "aws_s3_bucket_policy" "uploads_policy" {
for_each = local.sites
bucket = aws_s3_bucket.uploads[each.key].bucket
policy = data.aws_iam_policy_document.uploads_policy[each.key].json
}
#endregion
#region Load balancer logs
resource "aws_s3_bucket" "elb_logs" {
count = var.lb_logging_bucket == null ? 1 : 0
bucket_prefix = "webcms-${var.environment}-elb-logs-"
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
tags = var.tags
}
# Don't allow any public access to the ELB logging bucket
resource "aws_s3_bucket_public_access_block" "elb_logs" {
count = var.lb_logging_bucket == null ? 1 : 0
bucket = aws_s3_bucket.elb_logs[0].bucket
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
data "aws_iam_policy_document" "elb_logs_access" {
count = var.lb_logging_bucket == null ? 1 : 0
version = "2012-10-17"
statement {
sid = "rootDelivery"
effect = "Allow"
actions = ["s3:PutObject"]
resources = ["${aws_s3_bucket.elb_logs[0].arn}/AWSLogs/*"]
principals {
type = "AWS"
identifiers = [data.aws_elb_service_account.main.arn]
}
}
statement {
sid = "elbDelivery"
effect = "Allow"
actions = ["s3:PutObject"]
resources = ["${aws_s3_bucket.elb_logs[0].arn}/AWSLogs/*"]
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
statement {
sid = "aclAccess"
effect = "Allow"
actions = ["s3:GetBucketAcl"]
resources = [aws_s3_bucket.elb_logs[0].arn]
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
}
}
resource "aws_s3_bucket_policy" "elb_logs_delivery" {
count = var.lb_logging_bucket == null ? 1 : 0
bucket = aws_s3_bucket.elb_logs[0].bucket
policy = data.aws_iam_policy_document.elb_logs_access[0].json
}
#endregion