-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathec2_utility.tf
217 lines (182 loc) · 6.17 KB
/
ec2_utility.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
# Depending on if the first subnet is a public one or not, the EC2 instance might
# not be able to get a public IP address. Hence, we also attach it to the load balancer.
locals {
ssh_alternative_port = 2222
prometheus_password = var.prometheus_password == null ? random_password.prometheus_password.result : var.prometheus_password
prometheus_config = {
"basic_auth_users" : {
"admin" : bcrypt(local.prometheus_password)
}
}
prometheus_ssl_config = {
"tls_server_config" : {
"cert_file" : "/etc/certificate.pem",
"key_file" : "/etc/private_key.pem"
}
}
sql_exporter_config = {
"global" : {
"scrape_timeout_offset" : "500ms",
"min_interval" : "0s",
"max_connections" : 3,
"max_idle_connections" : 3
},
"target" : {
"data_source_name" : "postgres://${local.config.crate_username}:${urlencode(local.cratedb_password)}@${aws_lb.loadbalancer.dns_name}:5432/crate?sslmode=${var.crate.ssl_enable ? "require" : "disable"}",
"collectors" : ["cratedb_standard"]
},
"collector_files" : ["*.collector.yml"]
}
}
resource "random_password" "prometheus_password" {
length = 16
special = true
override_special = "_%@"
}
data "cloudinit_config" "config_utilities" {
gzip = true
base64_encode = true
part {
filename = "init.cfg"
content_type = "text/cloud-config"
content = templatefile("${path.module}/scripts/cloud-init-utilities.tftpl",
{
crate_host = aws_lb.loadbalancer.dns_name
crate_user = local.config.crate_username
crate_password = local.cratedb_password
crate_ssl = var.crate.ssl_enable
prometheus_config = indent(6, yamlencode(var.prometheus_ssl ? merge(local.prometheus_config, local.prometheus_ssl_config) : local.prometheus_config))
sql_exporter_config = indent(6, yamlencode(local.sql_exporter_config))
jmx_targets = indent(16, yamlencode(formatlist("%s:8080", aws_network_interface.interface[*].private_ip)))
node_exporter_targets = indent(16, yamlencode(formatlist("%s:9100", aws_network_interface.interface[*].private_ip)))
ssl_certificate = base64encode(tls_self_signed_cert.ssl.cert_pem)
ssl_private_key = base64encode(tls_private_key.ssl.private_key_pem)
}
)
}
}
resource "aws_security_group" "utilities" {
name = "${local.config.component_name}-util-sg"
description = "Allow inbound SSH traffic"
vpc_id = var.vpc_id
count = var.enable_utility_vm ? 1 : 0
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
description = "Prometheus"
from_port = 9090
to_port = 9090
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
description = "Locust"
from_port = 8089
to_port = 8089
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
}
resource "aws_network_interface" "utilities_interface" {
count = var.enable_utility_vm ? 1 : 0
subnet_id = var.subnet_ids[count.index]
security_groups = [aws_security_group.utilities[count.index].id]
tags = {
Name = "${local.config.component_name}-if-${count.index}"
}
}
# Not reusing the AMI from CrateDB nodes, as the architecture can differ (amd64 vs arm64)
data "aws_ami" "amazon_linux_utilities" {
most_recent = true
filter {
name = "name"
values = ["al2023-ami*"]
}
filter {
name = "architecture"
values = [var.utility_vm.instance_architecture]
}
owners = ["amazon"]
}
resource "aws_instance" "utilities" {
count = var.enable_utility_vm ? 1 : 0
ami = data.aws_ami.amazon_linux_utilities.id
instance_type = var.utility_vm.instance_type
key_name = var.ssh_keypair
availability_zone = element(var.availability_zones, count.index)
user_data = data.cloudinit_config.config_utilities.rendered
network_interface {
network_interface_id = aws_network_interface.utilities_interface[count.index].id
device_index = 0
}
root_block_device {
volume_size = var.utility_vm.disk_size_gb
}
lifecycle {
ignore_changes = [user_data, ami]
}
tags = {
Name = "${local.config.component_name}-utilities"
}
}
resource "aws_lb_target_group" "utilities" {
count = var.enable_utility_vm ? 1 : 0
name = "${local.config.component_name}-target-utilities"
port = 22
protocol = "TCP"
vpc_id = var.vpc_id
}
resource "aws_lb_target_group" "prometheus" {
count = var.enable_utility_vm ? 1 : 0
name = "${local.config.component_name}-target-prometheus"
port = 9090
protocol = "TCP"
vpc_id = var.vpc_id
}
resource "aws_lb_target_group_attachment" "utilities" {
count = var.enable_utility_vm ? 1 : 0
target_group_arn = aws_lb_target_group.utilities[count.index].arn
target_id = aws_instance.utilities[count.index].id
port = 22
}
resource "aws_lb_target_group_attachment" "prometheus" {
count = var.enable_utility_vm ? 1 : 0
target_group_arn = aws_lb_target_group.prometheus[count.index].arn
target_id = aws_instance.utilities[count.index].id
port = 9090
}
resource "aws_lb_listener" "utilities" {
count = var.enable_utility_vm ? 1 : 0
load_balancer_arn = aws_lb.loadbalancer.arn
port = local.ssh_alternative_port
protocol = "TCP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.utilities[count.index].arn
}
}
resource "aws_lb_listener" "prometheus" {
count = var.enable_utility_vm ? 1 : 0
load_balancer_arn = aws_lb.loadbalancer.arn
port = 9090
protocol = "TCP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.prometheus[count.index].arn
}
}