-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paths3.tf
193 lines (152 loc) · 3.91 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
locals {
create_s3_bucket = var.s3_bucket == null
bucket_name = local.create_s3_bucket ? "${local.name_prefix}${data.aws_region.current.name}-${random_string.this[0].id}" : var.s3_bucket.id
bucket_arn = "arn:${data.aws_partition.current.id}:s3:::${local.bucket_name}"
aws_logs_arn = "${local.bucket_arn}/${local.s3_exported_prefix}AWSLogs/${data.aws_caller_identity.current.account_id}/*"
s3_bucket = local.create_s3_bucket ? {
id = module.s3_bucket[0].s3_bucket_id
arn = module.s3_bucket[0].s3_bucket_arn
} : var.s3_bucket
}
resource "random_string" "this" {
count = local.create_s3_bucket ? 1 : 0
length = 8
upper = false
special = false
}
moved {
from = module.s3_bucket
to = module.s3_bucket[0]
}
module "s3_bucket" {
count = local.create_s3_bucket ? 1 : 0
source = "terraform-aws-modules/s3-bucket/aws"
version = "~> 4.0"
bucket = local.bucket_name
acl = "log-delivery-write"
force_destroy = true
lifecycle_rule = var.s3_lifecycle_rule
logging = var.s3_logging ? {
target_bucket = local.bucket_name
target_prefix = "${local.s3_exported_prefix}S3ServerLogs/"
} : {}
server_side_encryption_configuration = {
rule = {
apply_server_side_encryption_by_default = {
sse_algorithm = "AES256"
}
}
}
control_object_ownership = true
object_ownership = "ObjectWriter"
attach_elb_log_delivery_policy = true
attach_lb_log_delivery_policy = true
attach_policy = true
policy = data.aws_iam_policy_document.bucket.json
tags = var.tags
}
data "aws_iam_policy_document" "bucket" {
statement {
sid = "AWSCloudTrailWrite"
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
actions = [
"s3:PutObject",
]
resources = [
local.aws_logs_arn,
]
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
statement {
sid = "AWSCloudTrailAclCheck"
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
actions = [
"s3:GetBucketAcl",
]
resources = [
local.bucket_arn,
]
}
statement {
sid = "AWSConfigWrite"
effect = "Allow"
principals {
type = "Service"
identifiers = ["config.amazonaws.com"]
}
actions = [
"s3:PutObject",
]
resources = [
local.aws_logs_arn,
]
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
statement {
sid = "AWSConfigAclCheck"
effect = "Allow"
principals {
type = "Service"
identifiers = ["config.amazonaws.com"]
}
actions = [
"s3:GetBucketAcl",
]
resources = [
local.bucket_arn,
]
}
statement {
sid = "AWSRedshiftWrite"
effect = "Allow"
principals {
type = "Service"
identifiers = ["redshift.amazonaws.com"]
}
actions = [
"s3:PutObject",
]
resources = [
local.aws_logs_arn,
]
}
statement {
sid = "AWSRedshiftAclCheck"
effect = "Allow"
principals {
type = "Service"
identifiers = ["redshift.amazonaws.com"]
}
actions = [
"s3:GetBucketAcl",
]
resources = [
local.bucket_arn,
]
}
}
module "observe_lambda_s3_bucket_subscription" {
source = "observeinc/lambda/aws//modules/s3_bucket_subscription"
version = "3.6.0"
lambda = module.observe_lambda.lambda_function
bucket_arns = concat([local.s3_bucket.arn], var.subscribed_s3_bucket_arns)
iam_name_prefix = local.name_prefix
filter_prefix = local.s3_exported_prefix
enable_eventbridge = var.enable_s3_bucket_eventbridge
}