forked from yugabyte/terraform-aws-yugabyte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
215 lines (192 loc) · 5 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
#
# Terraform module to create a YugaByte cluster on AWS.
#
# This script does not use an autoscaling group. It just
# creates the necessary machines and configures them.
#
# Required parameters:
# cluster_name
# ssh_keypair
# ssh_key_path
# 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.
#
#########################################################
data "aws_ami" "yugabyte_ami" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = [
"amzn-ami-hvm-*-x86_64-gp2",
]
}
filter {
name = "owner-alias"
values = [
"amazon",
]
}
}
#########################################################
#
# 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
}
ingress {
from_port = 9000
to_port = 9000
protocol = "tcp"
self = true
}
ingress {
from_port = 6379
to_port = 6379
protocol = "tcp"
self = true
}
ingress {
from_port = 9042
to_port = 9042
protocol = "tcp"
self = true
}
ingress {
from_port = 5433
to_port = 5433
protocol = "tcp"
self = true
}
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}"
subnet_id = "${element(var.subnet_ids, count.index)}"
vpc_security_group_ids = [
"${aws_security_group.yugabyte.id}",
"${aws_security_group.yugabyte_intra.id}",
"${var.custom_security_group_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}/scripts/install_software.sh"
destination = "/home/ec2-user/install_software.sh"
connection {
type = "ssh"
user = "ec2-user"
private_key = "${file(var.ssh_key_path)}"
}
}
provisioner "file" {
source = "${path.module}/scripts/create_universe.sh"
destination = "/home/ec2-user/create_universe.sh"
connection {
type = "ssh"
user = "ec2-user"
private_key = "${file(var.ssh_key_path)}"
}
}
provisioner "remote-exec" {
inline = [
"chmod +x /home/ec2-user/install_software.sh",
"chmod +x /home/ec2-user/create_universe.sh",
"/home/ec2-user/install_software.sh args",
]
connection {
type = "ssh"
user = "ec2-user"
private_key = "${file(var.ssh_key_path)}"
}
}
}
#########################################################
#
# 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)}"
}
resource "null_resource" "create_yugabyte_universe" {
# 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}/scripts/create_universe.sh ${var.replication_factor} '${local.config_ip_list}' '${local.ssh_ip_list}' ec2-user ${var.ssh_key_path}"
}
}