-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths3.tf
173 lines (144 loc) · 4.39 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
171
172
173
################################################################################
### S3
################################################################################
########################################
### Site bucket
########################################
# Create a bucket
resource "aws_s3_bucket" "site" {
bucket = local.bucket_name
# lifecycle {
# prevent_destroy = true
# }
}
# Set bucket versioning
resource "aws_s3_bucket_versioning" "site" {
count = var.bucket_versioning_site == true ? 1 : 0
bucket = aws_s3_bucket.site.id
versioning_configuration {
status = "Enabled"
}
}
# Make sure the bucket is not public
resource "aws_s3_bucket_public_access_block" "site" {
bucket = aws_s3_bucket.site.id
block_public_acls = true
ignore_public_acls = true
block_public_policy = true
restrict_public_buckets = true
}
# Bucket encryption
resource "aws_s3_bucket_server_side_encryption_configuration" "site" {
bucket = aws_s3_bucket.site.bucket
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
# Enable logging to log bucket
resource "aws_s3_bucket_logging" "site" {
bucket = aws_s3_bucket.site.id
target_bucket = aws_s3_bucket.logging.id
target_prefix = "s3_${aws_s3_bucket.site.id}/"
depends_on = [aws_s3_bucket.logging]
}
# Upload a test page (if enabled)
resource "aws_s3_object" "index" {
count = var.upload_index ? 1 : 0
bucket = aws_s3_bucket.site.id
key = "index.html"
source = "${path.module}/files/index.html"
content_type = "text/html"
etag = filemd5("${path.module}/files/index.html")
}
# Upload a robots.txt file (if enabled)
resource "aws_s3_object" "robots" {
count = var.upload_robots ? 1 : 0
bucket = aws_s3_bucket.site.id
key = "robots.txt"
source = "${path.module}/files/robots.txt"
content_type = "text/plain"
etag = filemd5("${path.module}/files/robots.txt")
}
# Upload a 404 file (if enabled)
resource "aws_s3_object" "_404" {
count = var.upload_404 ? 1 : 0
bucket = aws_s3_bucket.site.id
key = "404.html"
source = "${path.module}/files/404.html"
content_type = "text/html"
etag = filemd5("${path.module}/files/404.html")
}
########################################
### Logging bucket
########################################
# Create a bucket
# Ignore KICS scan: S3 Bucket Logging Disabled
# Reason: This bucket is the logging bucket
# kics-scan ignore-line
resource "aws_s3_bucket" "logging" {
bucket = "${local.bucket_name}-logging"
# lifecycle {
# prevent_destroy = true
# }
}
# Set bucket versioning
resource "aws_s3_bucket_versioning" "logging" {
count = var.bucket_versioning_logs == true ? 1 : 0
bucket = aws_s3_bucket.logging.id
versioning_configuration {
status = "Enabled"
}
}
# Make sure the bucket is not public
resource "aws_s3_bucket_public_access_block" "logging" {
bucket = aws_s3_bucket.logging.id
block_public_acls = true
ignore_public_acls = true
block_public_policy = true
restrict_public_buckets = true
}
# Bucket encryption
resource "aws_s3_bucket_server_side_encryption_configuration" "logging" {
bucket = aws_s3_bucket.logging.bucket
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
# Set ownership controls
# https://docs.aws.amazon.com/AmazonS3/latest/userguide/about-object-ownership.html
resource "aws_s3_bucket_ownership_controls" "logging" {
bucket = aws_s3_bucket.logging.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
# Starting in April 2023, you need to to override the best practice and enable ACLs when sending CloudFront logs to S3
# https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/AccessLogs.html#AccessLogsBucketAndFileOwnership
resource "aws_s3_bucket_acl" "logging" {
bucket = aws_s3_bucket.logging.id
acl = "log-delivery-write"
depends_on = [aws_s3_bucket_ownership_controls.logging]
}
# Bucket lifecycle
resource "aws_s3_bucket_lifecycle_configuration" "logging" {
bucket = aws_s3_bucket.logging.id
rule {
id = "move_files_to_IA"
status = "Enabled"
transition {
days = 30
storage_class = "STANDARD_IA"
}
}
rule {
id = "prune_old_files"
status = "Enabled"
expiration {
days = 365
}
}
}