-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
21-buckets.tf
222 lines (179 loc) · 5.64 KB
/
21-buckets.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
# Random string generation
resource "random_id" "random_id_1" {
byte_length = 5
}
# S3 bucket creation
resource "aws_s3_bucket" "bucket_1" {
# S3 bucket name
# Note that S3 buckets in AWS are global, which means that it is not possible to use a name that has been used by someone else
bucket = "terraform-state-storage-gitlab-1-${random_id.random_id_1.hex}"
object_lock_enabled = true
tags = {
Name = "terraform-state-storage-gitlab-1"
}
depends_on = [
random_id.random_id_1,
aws_kms_key.kms_key_1,
aws_s3_bucket.log_bucket_1
]
}
# S3 bucket versioning
resource "aws_s3_bucket_versioning" "bucket_1_versioning" {
bucket = aws_s3_bucket.bucket_1.id
versioning_configuration {
status = "Enabled"
}
depends_on = [aws_s3_bucket.bucket_1]
}
# S3 bucket public access configuration
resource "aws_s3_bucket_public_access_block" "bucket_1_public_access_block_block" {
bucket = aws_s3_bucket.bucket_1.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
depends_on = [aws_s3_bucket.bucket_1]
}
# S3 bucket server-side encryption configuration
resource "aws_s3_bucket_server_side_encryption_configuration" "bucket_1_sse_configuration" {
bucket = aws_s3_bucket.bucket_1.id
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.kms_key_1.arn
sse_algorithm = "aws:kms"
}
}
depends_on = [
aws_s3_bucket.bucket_1,
aws_kms_key.kms_key_1
]
}
# Create a new S3 bucket to store logs
# tfsec:ignore:aws-s3-enable-bucket-logging
resource "aws_s3_bucket" "log_bucket_1" {
bucket = "terraform-state-storage-logs-gitlab-1-${random_id.random_id_1.hex}"
object_lock_enabled = true
}
# S3 bucket versioning
resource "aws_s3_bucket_versioning" "log_bucket_1_versioning" {
bucket = aws_s3_bucket.log_bucket_1.id
versioning_configuration {
status = "Enabled"
}
depends_on = [aws_s3_bucket.log_bucket_1]
}
# S3 bucket public access configuration
resource "aws_s3_bucket_public_access_block" "log_bucket_1_public_access_block_block" {
bucket = aws_s3_bucket.log_bucket_1.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
depends_on = [aws_s3_bucket.log_bucket_1]
}
# Log bucket policy to allow write access for log delivery
resource "aws_s3_bucket_policy" "log_bucket_1_policy" {
bucket = aws_s3_bucket.log_bucket_1.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Service = "delivery.logs.amazonaws.com"
}
Action = "s3:PutObject"
Resource = "${aws_s3_bucket.log_bucket_1.arn}/*"
}
]
})
depends_on = [aws_s3_bucket.log_bucket_1]
}
# S3 bucket server-side encryption configuration
resource "aws_s3_bucket_server_side_encryption_configuration" "log_bucket_1_sse_configuration" {
bucket = aws_s3_bucket.log_bucket_1.id
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.kms_key_1.arn
sse_algorithm = "aws:kms"
}
}
depends_on = [
aws_s3_bucket.log_bucket_1,
aws_kms_key.kms_key_1
]
}
# Enable logging for the main S3 bucket and set the log bucket as the target for log storage
resource "aws_s3_bucket_logging" "log_bucket_1_logging" {
bucket = aws_s3_bucket.bucket_1.id
# Specify the target bucket for storing logs and the prefix for log files
target_bucket = aws_s3_bucket.log_bucket_1.id
target_prefix = "log/"
depends_on = [
aws_s3_bucket.log_bucket_1,
aws_s3_bucket.bucket_1
]
}
# Create a new S3 bucket to store logs
# tfsec:ignore:aws-s3-enable-bucket-logging
resource "aws_s3_bucket" "log_bucket_2" {
bucket = "vpc-flow-logs-gitlab-1-${random_id.random_id_1.hex}"
object_lock_enabled = true
}
# S3 bucket versioning
resource "aws_s3_bucket_versioning" "log_bucket_2_versioning" {
bucket = aws_s3_bucket.log_bucket_2.id
versioning_configuration {
status = "Enabled"
}
depends_on = [aws_s3_bucket.log_bucket_2]
}
# S3 bucket public access configuration
resource "aws_s3_bucket_public_access_block" "log_bucket_2_public_access_block_block" {
bucket = aws_s3_bucket.log_bucket_2.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
depends_on = [aws_s3_bucket.log_bucket_2]
}
# Log bucket policy to allow write access for log delivery
resource "aws_s3_bucket_policy" "log_bucket_2_policy" {
bucket = aws_s3_bucket.log_bucket_2.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow",
Principal = {
Service = "logs.amazonaws.com"
},
Action = "s3:PutObject",
Resource = "${aws_s3_bucket.log_bucket_2.arn}/*"
}
]
})
depends_on = [aws_s3_bucket.log_bucket_2]
}
# S3 bucket server-side encryption configuration
resource "aws_s3_bucket_server_side_encryption_configuration" "log_bucket_2_sse_configuration" {
bucket = aws_s3_bucket.log_bucket_2.id
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.kms_key_1.arn
sse_algorithm = "aws:kms"
}
}
depends_on = [
aws_s3_bucket.log_bucket_2,
aws_kms_key.kms_key_1
]
}
# Create a VPC Flow Log resource to enable flow logs for the VPC
resource "aws_flow_log" "vpc_1_flow_logs" {
log_destination = aws_s3_bucket.log_bucket_2.arn
log_destination_type = "s3"
traffic_type = "ALL"
vpc_id = aws_vpc.vpc_1.id
depends_on = [aws_s3_bucket.log_bucket_2]
}