-
Notifications
You must be signed in to change notification settings - Fork 31
/
s3.tf
156 lines (131 loc) · 5.79 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
/**
* 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", "{}")))
logging {
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"]}/"
}
lifecycle_rule {
id = "cost_optimization"
enabled = true
abort_incomplete_multipart_upload_days = var.s3_lifecycle_abort_incomplete_multipart_upload_days
dynamic "transition" {
for_each = each.value["s3_object_expiration_days_num"] == "-1" || each.value["s3_lifecycle_policy_transition_period"] < each.value["s3_object_expiration_days_num"] ? [1] : []
content {
days = each.value["s3_lifecycle_policy_transition_period"]
storage_class = each.value["s3_storage_class"]
}
}
dynamic "expiration" {
for_each = each.value["s3_object_expiration_days_num"] != "-1" ? [1] : []
content {
days = each.value["s3_object_expiration_days_num"]
}
}
}
}
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 = ["Size", "LastModifiedDate", "StorageClass", "ETag", "IntelligentTieringAccessTier"]
}
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_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"
}