-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
main.tf
362 lines (279 loc) · 11 KB
/
main.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
################################################################################
# Cluster
################################################################################
resource "aws_msk_cluster" "this" {
count = var.create ? 1 : 0
broker_node_group_info {
az_distribution = var.broker_node_az_distribution
client_subnets = var.broker_node_client_subnets
dynamic "connectivity_info" {
for_each = length(var.broker_node_connectivity_info) > 0 ? [var.broker_node_connectivity_info] : []
content {
dynamic "public_access" {
for_each = try([connectivity_info.value.public_access], [])
content {
type = try(public_access.value.type, null)
}
}
dynamic "vpc_connectivity" {
for_each = try([connectivity_info.value.vpc_connectivity], [])
content {
dynamic "client_authentication" {
for_each = try([vpc_connectivity.value.client_authentication], [])
content {
dynamic "sasl" {
for_each = try([client_authentication.value.sasl], [])
content {
iam = try(sasl.value.iam, null)
scram = try(sasl.value.scram, null)
}
}
tls = try(client_authentication.value.tls, null)
}
}
}
}
}
}
instance_type = var.broker_node_instance_type
security_groups = var.broker_node_security_groups
dynamic "storage_info" {
for_each = length(var.broker_node_storage_info) > 0 ? [var.broker_node_storage_info] : []
content {
dynamic "ebs_storage_info" {
for_each = try([storage_info.value.ebs_storage_info], [])
content {
dynamic "provisioned_throughput" {
for_each = try([ebs_storage_info.value.provisioned_throughput], [])
content {
enabled = try(provisioned_throughput.value.enabled, null)
volume_throughput = try(provisioned_throughput.value.volume_throughput, null)
}
}
volume_size = try(ebs_storage_info.value.volume_size, 64)
}
}
}
}
}
dynamic "client_authentication" {
for_each = length(var.client_authentication) > 0 ? [var.client_authentication] : []
content {
dynamic "sasl" {
for_each = try([client_authentication.value.sasl], [])
content {
iam = try(sasl.value.iam, null)
scram = try(sasl.value.scram, null)
}
}
dynamic "tls" {
for_each = try([client_authentication.value.tls], [])
content {
certificate_authority_arns = try(tls.value.certificate_authority_arns, null)
}
}
unauthenticated = try(client_authentication.value.unauthenticated, null)
}
}
cluster_name = var.name
configuration_info {
arn = var.create_configuration ? aws_msk_configuration.this[0].arn : var.configuration_arn
revision = var.create_configuration ? aws_msk_configuration.this[0].latest_revision : var.configuration_revision
}
encryption_info {
encryption_at_rest_kms_key_arn = var.encryption_at_rest_kms_key_arn
encryption_in_transit {
client_broker = var.encryption_in_transit_client_broker
in_cluster = var.encryption_in_transit_in_cluster
}
}
enhanced_monitoring = var.enhanced_monitoring
kafka_version = var.kafka_version
logging_info {
broker_logs {
cloudwatch_logs {
enabled = var.cloudwatch_logs_enabled
log_group = var.cloudwatch_logs_enabled ? local.cloudwatch_log_group : null
}
firehose {
enabled = var.firehose_logs_enabled
delivery_stream = var.firehose_delivery_stream
}
s3 {
bucket = var.s3_logs_bucket
enabled = var.s3_logs_enabled
prefix = var.s3_logs_prefix
}
}
}
number_of_broker_nodes = var.number_of_broker_nodes
open_monitoring {
prometheus {
jmx_exporter {
enabled_in_broker = var.jmx_exporter_enabled
}
node_exporter {
enabled_in_broker = var.node_exporter_enabled
}
}
}
storage_mode = var.storage_mode
timeouts {
create = try(var.timeouts.create, null)
update = try(var.timeouts.update, null)
delete = try(var.timeouts.delete, null)
}
# required for appautoscaling
lifecycle {
ignore_changes = [
broker_node_group_info[0].storage_info[0].ebs_storage_info[0].volume_size,
]
}
tags = var.tags
}
################################################################################
# VPC Connection
################################################################################
resource "aws_msk_vpc_connection" "this" {
for_each = { for k, v in var.vpc_connections : k => v if var.create }
authentication = each.value.authentication
client_subnets = each.value.client_subnets
security_groups = each.value.security_groups
target_cluster_arn = aws_msk_cluster.this[0].arn
vpc_id = each.value.vpc_id
tags = merge(var.tags, try(each.value.tags, {}))
}
################################################################################
# Cluster Policy
################################################################################
resource "aws_msk_cluster_policy" "this" {
count = var.create && var.create_cluster_policy ? 1 : 0
cluster_arn = aws_msk_cluster.this[0].arn
policy = data.aws_iam_policy_document.this[0].json
}
data "aws_iam_policy_document" "this" {
count = var.create && var.create_cluster_policy ? 1 : 0
source_policy_documents = var.cluster_source_policy_documents
override_policy_documents = var.cluster_override_policy_documents
dynamic "statement" {
for_each = var.cluster_policy_statements
content {
sid = try(statement.value.sid, null)
actions = try(statement.value.actions, null)
not_actions = try(statement.value.not_actions, null)
effect = try(statement.value.effect, null)
resources = try(statement.value.resources, [aws_msk_cluster.this[0].arn])
not_resources = try(statement.value.not_resources, null)
dynamic "principals" {
for_each = try(statement.value.principals, [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "not_principals" {
for_each = try(statement.value.not_principals, [])
content {
type = not_principals.value.type
identifiers = not_principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
}
}
}
################################################################################
# Configuration
################################################################################
resource "random_id" "this" {
count = var.create && var.create_configuration ? 1 : 0
keepers = {
kafka_version = var.kafka_version
}
byte_length = 8
}
resource "aws_msk_configuration" "this" {
count = var.create && var.create_configuration ? 1 : 0
name = format("%s-%s", coalesce(var.configuration_name, var.name), random_id.this[0].dec)
description = var.configuration_description
kafka_versions = [random_id.this[0].keepers.kafka_version]
server_properties = join("\n", [for k, v in var.configuration_server_properties : format("%s = %s", k, v)])
lifecycle {
create_before_destroy = true
}
}
################################################################################
# Secret(s)
################################################################################
resource "aws_msk_scram_secret_association" "this" {
count = var.create && var.create_scram_secret_association && try(var.client_authentication.sasl.scram, false) ? 1 : 0
cluster_arn = aws_msk_cluster.this[0].arn
secret_arn_list = var.scram_secret_association_secret_arn_list
}
################################################################################
# CloudWatch Log Group
################################################################################
locals {
cloudwatch_log_group = var.create && var.create_cloudwatch_log_group ? aws_cloudwatch_log_group.this[0].name : var.cloudwatch_log_group_name
}
resource "aws_cloudwatch_log_group" "this" {
count = var.create && var.create_cloudwatch_log_group ? 1 : 0
name = coalesce(var.cloudwatch_log_group_name, "/aws/msk/${var.name}")
retention_in_days = var.cloudwatch_log_group_retention_in_days
kms_key_id = var.cloudwatch_log_group_kms_key_id
log_group_class = var.cloudwatch_log_group_class
tags = var.tags
}
################################################################################
# Storage Autoscaling
################################################################################
resource "aws_appautoscaling_target" "this" {
count = var.create && var.enable_storage_autoscaling ? 1 : 0
max_capacity = var.scaling_max_capacity
min_capacity = 1
role_arn = var.scaling_role_arn
resource_id = aws_msk_cluster.this[0].arn
scalable_dimension = "kafka:broker-storage:VolumeSize"
service_namespace = "kafka"
tags = var.tags
}
resource "aws_appautoscaling_policy" "this" {
count = var.create && var.enable_storage_autoscaling ? 1 : 0
name = "${var.name}-broker-storage-scaling"
policy_type = "TargetTrackingScaling"
resource_id = aws_msk_cluster.this[0].arn
scalable_dimension = aws_appautoscaling_target.this[0].scalable_dimension
service_namespace = aws_appautoscaling_target.this[0].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "KafkaBrokerStorageUtilization"
}
target_value = var.scaling_target_value
}
}
################################################################################
# Glue Schema Registry & Schema
################################################################################
resource "aws_glue_registry" "this" {
for_each = { for k, v in var.schema_registries : k => v if var.create && var.create_schema_registry }
registry_name = each.value.name
description = try(each.value.description, null)
tags = merge(var.tags, try(each.value.tags, {}))
}
resource "aws_glue_schema" "this" {
for_each = { for k, v in var.schemas : k => v if var.create && var.create_schema_registry }
schema_name = each.value.schema_name
description = try(each.value.description, null)
registry_arn = aws_glue_registry.this[each.value.schema_registry_name].arn
data_format = try(each.value.data_format, "AVRO")
compatibility = each.value.compatibility
schema_definition = each.value.schema_definition
tags = merge(var.tags, try(each.value.tags, {}))
}