This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kafka.tf
106 lines (92 loc) · 3.45 KB
/
kafka.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
# @author: Alejandro Galue <[email protected]>
data "template_file" "kafka" {
count = length(var.kafka_ip_addresses)
template = file("${path.module}/templates/kafka.tpl")
vars = {
node_id = count.index + 1
hostname = element(keys(var.kafka_ip_addresses), count.index)
domainname = aws_route53_zone.private.name
dependencies = join(
",",
formatlist("%v:2181", aws_route53_record.zookeeper_private.*.name),
)
zookeeper_connect = "${join(
",",
formatlist("%v:2181", aws_route53_record.zookeeper_private.*.name),
)}/kafka"
num_partitions = var.settings["kafka_num_partitions"]
replication_factor = var.settings["kafka_replication_factor"]
min_insync_replicas = var.settings["kafka_min_insync_replicas"]
security_protocol = var.settings["kafka_security_protocol"]
security_mechanisms = var.settings["kafka_security_mechanisms"]
admin_password = var.settings["kafka_admin_password"]
user_name = var.settings["kafka_user_name"]
user_password = var.settings["kafka_user_password"]
max_message_size = var.settings["kafka_max_message_size"]
}
}
resource "aws_instance" "kafka" {
count = length(var.kafka_ip_addresses)
ami = data.aws_ami.kafka.image_id
instance_type = var.instance_types["kafka"]
subnet_id = aws_subnet.public.id
key_name = var.aws_key_name
private_ip = element(values(var.kafka_ip_addresses), count.index)
user_data = element(data.template_file.kafka.*.rendered, count.index)
associate_public_ip_address = true
vpc_security_group_ids = [
aws_security_group.common.id,
aws_security_group.kafka.id,
]
depends_on = [aws_route53_record.kafka_private]
root_block_device {
volume_type = "gp2"
volume_size = var.disk_space["kafka"]
}
connection {
host = coalesce(self.public_ip, self.private_ip)
type = "ssh"
user = "ec2-user"
private_key = file(var.aws_private_key)
}
timeouts {
create = "30m"
delete = "15m"
}
tags = {
Name = "Terraform Kafka Server ${count.index + 1}"
Environment = "Test"
Department = "Support"
}
}
resource "aws_route53_record" "kafka" {
count = length(var.kafka_ip_addresses)
zone_id = aws_route53_zone.main.zone_id
name = "${element(keys(var.kafka_ip_addresses), count.index)}.${aws_route53_zone.main.name}"
type = "A"
ttl = var.dns_ttl
records = [
element(aws_instance.kafka.*.public_ip, count.index),
]
}
resource "aws_route53_record" "kafka_private" {
count = length(var.kafka_ip_addresses)
zone_id = aws_route53_zone.private.zone_id
name = "${element(keys(var.kafka_ip_addresses), count.index)}.${aws_route53_zone.private.name}"
type = "A"
ttl = var.dns_ttl
# TF-UPGRADE-TODO: In Terraform v0.10 and earlier, it was sometimes necessary to
# force an interpolation expression to be interpreted as a list by wrapping it
# in an extra set of list brackets. That form was supported for compatibilty in
# v0.11, but is no longer supported in Terraform v0.12.
#
# If the expression in the following list itself returns a list, remove the
# brackets to avoid interpretation as a list of lists. If the expression
# returns a single list item then leave it as-is and remove this TODO comment.
records = [
element(values(var.kafka_ip_addresses), count.index),
]
}
output "kafka" {
value = join(",", aws_instance.kafka.*.public_ip)
}