-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy paths3.tf
More file actions
201 lines (174 loc) · 7.38 KB
/
Copy paths3.tf
File metadata and controls
201 lines (174 loc) · 7.38 KB
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
/**
* Copyright (C) 2018-2020 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
*/
##
### Apiary S3 policy template
##
locals {
bucket_policy_map = {
for schema in local.schemas_info : schema["schema_name"] => templatefile("${path.module}/templates/apiary-bucket-policy.json", {
#if apiary_shared_schemas is empty or contains current schema, allow customer accounts to access this bucket.
customer_principal = (length(var.apiary_shared_schemas) == 0 || contains(var.apiary_shared_schemas, schema["schema_name"])) && schema["customer_accounts"] != "" ? join("\",\"", formatlist("arn:aws:iam::%s:root", split(",", schema["customer_accounts"]))) : ""
customer_condition = var.apiary_customer_condition
bucket_name = schema["data_bucket"]
encryption = schema["encryption"]
kms_key_arn = schema["encryption"] == "aws:kms" ? aws_kms_key.apiary_kms[schema["schema_name"]].arn : ""
consumer_iamroles = join("\",\"", var.apiary_consumer_iamroles)
conditional_consumer_iamroles = join("\",\"", var.apiary_conditional_consumer_iamroles)
producer_iamroles = replace(lookup(var.apiary_producer_iamroles, schema["schema_name"], ""), ",", "\",\"")
deny_iamroles = join("\",\"", var.apiary_deny_iamroles)
deny_iamrole_actions = join("\",\"", var.apiary_deny_iamrole_actions)
client_roles = replace(lookup(schema, "client_roles", ""), ",", "\",\"")
governance_iamroles = join("\",\"", var.apiary_governance_iamroles)
consumer_prefix_roles = lookup(var.apiary_consumer_prefix_iamroles, schema["schema_name"], {})
common_producer_iamroles = join("\",\"", var.apiary_common_producer_iamroles)
deny_exception_iamroles = lookup(schema, "deny_exception_iamroles", "") == "" ? "" : join("\",\"", compact(concat(split(",", schema["deny_exception_iamroles"]), var.apiary_managed_service_iamroles, var.apiary_governance_iamroles)))
})
}
}
##
### Apiary S3 data buckets
##
resource "aws_s3_bucket" "apiary_data_bucket" {
for_each = {
for schema in local.schemas_info : "${schema["schema_name"]}" => schema
}
bucket = each.value["data_bucket"]
request_payer = "BucketOwner"
policy = local.bucket_policy_map[each.key]
tags = merge(tomap({"Name"=each.value["data_bucket"]}),
var.apiary_tags,
jsondecode(lookup(each.value, "tags", "{}")))
}
resource "aws_s3_bucket_versioning" "apiary_data_bucket_versioning" {
for_each = {
for schema in local.schemas_info : "${schema["schema_name"]}" => schema
if lookup(schema, "s3_versioning_enabled", "") != ""
}
bucket = each.value["data_bucket"]
versioning_configuration {
status = lookup(each.value, "s3_versioning_enabled", "Disabled")
}
}
resource "aws_s3_bucket_lifecycle_configuration" "apiary_data_bucket_versioning_lifecycle" {
for_each = {
for schema in local.schemas_info : "${schema["schema_name"]}" => schema
}
bucket = each.value["data_bucket"]
# Rule for s3 incomplete multipart upload expiration
rule {
id = "expire-incomplete-multipart-uploads"
status = "Enabled"
abort_incomplete_multipart_upload {
days_after_initiation = var.s3_lifecycle_abort_incomplete_multipart_upload_days
}
}
# Rule for s3 versioning expiration
rule {
id = "expire-noncurrent-versions-days"
status = lookup(each.value, "s3_versioning_enabled", "") != "" ? "Enabled" : "Disabled"
noncurrent_version_expiration {
noncurrent_days = tonumber(lookup(each.value, "s3_versioning_expiration_days", var.s3_versioning_expiration_days))
}
}
# Rule s3 delete marker object expiration
rule {
id = "expire-delete-marker"
status = lookup(each.value, "s3_versioning_enabled", "") != "" ? "Enabled" : "Disabled"
expiration {
expired_object_delete_marker = "true"
}
}
# Rule s3 intelligent tiering transition
rule {
id = "cost_optimization_transition"
status = each.value["s3_object_expiration_days_num"] == "-1" || each.value["s3_lifecycle_policy_transition_period"] < each.value["s3_object_expiration_days_num"] ? "Enabled" : "Disabled"
transition {
days = each.value["s3_lifecycle_policy_transition_period"]
storage_class = each.value["s3_storage_class"]
}
}
# Rule s3 object expiration - days cannot be negative
rule {
id = "cost_optimization_expiration"
status = each.value["s3_object_expiration_days_num"] != "-1" ? "Enabled" : "Disabled"
expiration {
days = each.value["s3_object_expiration_days_num"] != "-1" ? each.value["s3_object_expiration_days_num"] : "0"
}
}
}
resource "aws_s3_bucket_inventory" "apiary_bucket" {
for_each = var.s3_enable_inventory == true ? {
for schema in local.schemas_info : "${schema["schema_name"]}" => schema
} : {}
bucket = aws_s3_bucket.apiary_data_bucket[each.key].id
name = local.s3_inventory_prefix
included_object_versions = "All"
schedule {
frequency = "Daily"
}
destination {
bucket {
format = var.s3_inventory_format
bucket_arn = aws_s3_bucket.apiary_inventory_bucket[0].arn
encryption {
sse_s3 {}
}
}
}
optional_fields = var.s3_inventory_optional_fields
}
resource "aws_s3_bucket_public_access_block" "apiary_bucket" {
for_each = {
for schema in local.schemas_info : "${schema["schema_name"]}" => schema
}
bucket = aws_s3_bucket.apiary_data_bucket[each.key].id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_ownership_controls" "apiary_bucket" {
for_each = {
for schema in local.schemas_info : "${schema["schema_name"]}" => schema
}
bucket = aws_s3_bucket.apiary_data_bucket[each.key].id
rule {
object_ownership = "BucketOwnerEnforced"
}
}
resource "aws_s3_bucket_notification" "data_events" {
for_each = var.enable_data_events ? {
for schema in local.schemas_info : "${schema["schema_name"]}" => schema if lookup(schema, "enable_data_events_sqs", "0") == "0"
} : {}
bucket = aws_s3_bucket.apiary_data_bucket[each.key].id
topic {
topic_arn = aws_sns_topic.apiary_data_events[each.key].arn
events = ["s3:ObjectCreated:*", "s3:ObjectRemoved:*"]
}
}
resource "aws_s3_bucket_notification" "data_queue_events" {
for_each = { for schema in local.schemas_info : "${schema["schema_name"]}" => schema if lookup(schema, "enable_data_events_sqs", "0") == "1" }
bucket = aws_s3_bucket.apiary_data_bucket[each.key].id
queue {
queue_arn = aws_sqs_queue.apiary_data_event_queue[0].arn
events = ["s3:ObjectCreated:*", "s3:ObjectRemoved:*"]
}
}
resource "aws_s3_bucket_logging" "apiary_bucket" {
for_each = {
for schema in local.schemas_info : "${schema["schema_name"]}" => schema
}
bucket = each.value["data_bucket"]
target_bucket = local.enable_apiary_s3_log_management ? aws_s3_bucket.apiary_managed_logs_bucket[0].id : var.apiary_log_bucket
target_prefix = "${var.apiary_log_prefix}${each.value["data_bucket"]}/"
}
resource "aws_s3_bucket_metric" "paid_metrics" {
for_each = var.enable_s3_paid_metrics ? {
for schema in local.schemas_info : "${schema["schema_name"]}" => schema
} : {}
bucket = aws_s3_bucket.apiary_data_bucket[each.key].id
name = "EntireBucket"
}