-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2.tf
211 lines (175 loc) · 5.51 KB
/
ec2.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
locals {
cratedb_password = var.cratedb_password == null ? random_password.cratedb_password.result : var.cratedb_password
}
resource "random_password" "cratedb_password" {
length = 16
special = true
override_special = "_%@"
}
resource "tls_private_key" "ssl" {
algorithm = "RSA"
rsa_bits = 2048
}
resource "tls_self_signed_cert" "ssl" {
private_key_pem = tls_private_key.ssl.private_key_pem
# Set to two years. If the number is too high, there appears to be an overflow resulting in a date in the past
validity_period_hours = 17532
subject {
organization = var.config.team
}
allowed_uses = [
"key_encipherment",
"digital_signature",
"server_auth",
]
}
data "cloudinit_config" "config" {
gzip = true
base64_encode = true
# Cloud Init script for initializing CrateDB
part {
filename = "init.cfg"
content_type = "text/cloud-config"
content = templatefile("${path.module}/scripts/cloud-init-cratedb-${var.cratedb_tar_download_url == null ? "rpm" : "tar"}.tftpl",
{
user_provisioning_file = indent(6, file(("${path.module}/scripts/user_provisioning.sh")))
crate_download_url = var.cratedb_tar_download_url
crate_user = local.config.crate_username
crate_pass = local.cratedb_password
crate_heap_size = var.crate.heap_size_gb
crate_cluster_name = var.crate.cluster_name
crate_cluster_size = var.crate.cluster_size
crate_nodes_ips = indent(12, yamlencode(aws_network_interface.interface[*].private_ip))
crate_ssl_enable = var.crate.ssl_enable
crate_protocol = var.crate.ssl_enable ? "https" : "http"
crate_ssl_certificate = base64encode(tls_self_signed_cert.ssl.cert_pem)
crate_ssl_private_key = base64encode(tls_private_key.ssl.private_key_pem)
}
)
}
}
resource "aws_security_group" "cratedb" {
name = "${local.config.component_name}-sg"
description = "Allow inbound CrateDB traffic"
vpc_id = var.vpc_id
ingress {
description = "CrateDB-HTTP"
from_port = 4200
to_port = 4200
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
description = "CrateDB-PostgreSQL"
from_port = 5432
to_port = 5432
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
description = "CrateDB-Transport"
from_port = 4300
to_port = 4300
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
description = "CrateDB-JMX"
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
description = "node_exporter"
from_port = 9100
to_port = 9100
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.ssh_access ? ["0.0.0.0/0"] : []
ipv6_cidr_blocks = var.ssh_access ? ["::/0"] : []
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
}
data "aws_ami" "amazon_linux" {
most_recent = true
filter {
name = "name"
values = ["al2023-ami*"]
}
filter {
name = "architecture"
values = [var.instance_architecture]
}
owners = ["amazon"]
}
resource "aws_network_interface" "interface" {
count = var.crate.cluster_size
subnet_id = element(var.subnet_ids, count.index)
security_groups = [aws_security_group.cratedb.id]
tags = {
Name = "${local.config.component_name}-if-${count.index}"
}
}
resource "aws_instance" "cratedb_node" {
count = var.crate.cluster_size
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
key_name = var.ssh_keypair
availability_zone = element(var.availability_zones, count.index)
user_data = data.cloudinit_config.config.rendered
monitoring = var.enable_utility_vm
iam_instance_profile = var.instance_profile
network_interface {
network_interface_id = aws_network_interface.interface[count.index].id
device_index = 0
}
root_block_device {
volume_size = 50
}
ebs_block_device {
device_name = "/dev/sdh"
volume_size = var.disk_size_gb
volume_type = var.disk_type
iops = var.disk_iops
throughput = var.disk_throughput
}
lifecycle {
ignore_changes = [user_data, ami]
}
tags = {
Name = "${local.config.component_name}-node-${count.index}"
}
}
resource "aws_lb_target_group_attachment" "http" {
count = var.crate.cluster_size
target_group_arn = aws_lb_target_group.http.arn
target_id = aws_instance.cratedb_node[count.index].private_ip
}
resource "aws_lb_target_group_attachment" "postgresql" {
count = var.crate.cluster_size
target_group_arn = aws_lb_target_group.postgresql.arn
target_id = aws_instance.cratedb_node[count.index].private_ip
}
resource "aws_lb_target_group_attachment" "jmx" {
count = var.crate.cluster_size
target_group_arn = aws_lb_target_group.jmx.arn
target_id = aws_instance.cratedb_node[count.index].private_ip
}