-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.tf
275 lines (250 loc) · 6.86 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
#
# Terraform module to create a YugabyteDB cluster on AWS.
#
# This script does not use an autoscaling group. It just
# creates the necessary machines and configures them.
#
# Required parameters:
# region_name
# cluster_name
# ssh_keypair
# ssh_private_key
# subnet_ids
# vpc_id
#
# Other useful options:
# associate_public_ip_address [default: "true"]
# custom_security_group_id
# num_instances [default: 3]
#
#
#########################################################
#
# Choose the most recent Amazon Linux AMI.
#
#########################################################
terraform {
required_version = ">= 0.12"
}
provider "aws" {
version = "~> 3.0"
region = var.region_name
}
data "aws_ami" "yugabyte_ami" {
most_recent = true
owners = ["aws-marketplace"]
filter {
name = "name"
values = [
"CentOS Linux 7 x86_64 HVM EBS *",
]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
}
#########################################################
#
# Create the security groups needed.
#
#########################################################
resource "aws_security_group" "yugabyte" {
name = "${var.prefix}${var.cluster_name}"
vpc_id = var.vpc_id
ingress {
from_port = 7000
to_port = 7000
protocol = "tcp"
self = true
cidr_blocks = var.allowed_sources
}
ingress {
from_port = 9000
to_port = 9000
protocol = "tcp"
self = true
cidr_blocks = var.allowed_sources
}
ingress {
from_port = 6379
to_port = 6379
protocol = "tcp"
self = true
cidr_blocks = var.allowed_sources
}
ingress {
from_port = 9042
to_port = 9042
protocol = "tcp"
self = true
cidr_blocks = var.allowed_sources
}
ingress {
from_port = 5433
to_port = 5433
protocol = "tcp"
self = true
cidr_blocks = var.allowed_sources
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
self = true
cidr_blocks = var.allowed_sources
}
lifecycle {
create_before_destroy = true
}
tags = {
Name = "${var.prefix}${var.cluster_name}"
YugaByte = "true"
Service = "YugaByte"
}
}
resource "aws_security_group" "yugabyte_intra" {
name = "${var.prefix}${var.cluster_name}-intra"
vpc_id = var.vpc_id
ingress {
from_port = 7100
to_port = 7100
protocol = "tcp"
self = true
}
ingress {
from_port = 9100
to_port = 9100
protocol = "tcp"
self = true
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
lifecycle {
create_before_destroy = true
}
tags = {
Name = "${var.prefix}${var.cluster_name}-intra"
YugaByte = "true"
Service = "YugaByte"
}
}
#########################################################
#
# Create the required nodes.
#
#########################################################
resource "aws_instance" "yugabyte_nodes" {
count = var.num_instances
ami = data.aws_ami.yugabyte_ami.id
associate_public_ip_address = var.associate_public_ip_address
instance_type = var.instance_type
key_name = var.ssh_keypair
availability_zone = element(var.availability_zones, count.index)
subnet_id = element(var.subnet_ids, count.index)
vpc_security_group_ids = [
aws_security_group.yugabyte.id,
aws_security_group.yugabyte_intra.id,
]
root_block_device {
volume_size = var.root_volume_size
volume_type = var.root_volume_type
iops = var.root_volume_iops
}
tags = {
Name = "${var.prefix}${var.cluster_name}-n${format("%d", count.index + 1)}"
YugaByte = "true"
Service = "YugaByte"
}
provisioner "file" {
source = "${path.module}/utilities/scripts/install_software.sh"
destination = "/home/${var.ssh_user}/install_software.sh"
connection {
host = self.public_ip
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
}
}
provisioner "file" {
source = "${path.module}/utilities/scripts/create_universe.sh"
destination = "/home/${var.ssh_user}/create_universe.sh"
connection {
host = self.public_ip
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
}
}
provisioner "file" {
source = "${path.module}/utilities/scripts/start_tserver.sh"
destination = "/home/${var.ssh_user}/start_tserver.sh"
connection {
host = self.public_ip
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
}
}
provisioner "file" {
source = "${path.module}/utilities/scripts/start_master.sh"
destination = "/home/${var.ssh_user}/start_master.sh"
connection {
host = self.public_ip
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
}
}
provisioner "remote-exec" {
inline = [
"chmod +x /home/${var.ssh_user}/install_software.sh",
"chmod +x /home/${var.ssh_user}/create_universe.sh",
"chmod +x /home/${var.ssh_user}/start_tserver.sh",
"chmod +x /home/${var.ssh_user}/start_master.sh",
"sudo yum install -y wget",
"/home/${var.ssh_user}/install_software.sh '${var.yb_version}'",
]
connection {
host = self.public_ip
type = "ssh"
user = var.ssh_user
private_key = file(var.ssh_private_key)
}
}
lifecycle {
create_before_destroy = true
}
}
#########################################################
#
# Configure the nodes into a universe.
#
#########################################################
locals {
ssh_ip_list = var.use_public_ip_for_ssh == "true" ? join(
" ", aws_instance.yugabyte_nodes.*.public_ip,
) : join(" ", aws_instance.yugabyte_nodes.*.private_ip)
config_ip_list = join(" ", aws_instance.yugabyte_nodes.*.private_ip)
az_list = join(" ", aws_instance.yugabyte_nodes.*.availability_zone)
}
resource "null_resource" "create_yugabyte_universe" {
# Define the trigger condition to run the resource block
triggers = {
cluster_instance_ids = join(",", aws_instance.yugabyte_nodes.*.id)
}
# Execute after the nodes are provisioned and the software installed.
depends_on = [aws_instance.yugabyte_nodes]
provisioner "local-exec" {
# Bootstrap script called with private_ip of each node in the clutser
command = "${path.module}/utilities/scripts/create_universe.sh 'aws' '${var.region_name}' ${var.replication_factor} '${local.config_ip_list}' '${local.ssh_ip_list}' '${local.az_list}' ${var.ssh_user} ${var.ssh_private_key}"
}
}