-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscylladb-instances.tf
147 lines (120 loc) · 4.15 KB
/
scylladb-instances.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
resource "tls_private_key" "private_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
key_name = "ScyllaDB-Enterprise-DEMO-key"
public_key = tls_private_key.private_key.public_key_openssh
}
resource "aws_instance" "scylladb_seed" {
count = 1
ami = var.scylla_ami_id
instance_type = var.scylla_node_type
key_name = aws_key_pair.generated_key.key_name
subnet_id = element(aws_subnet.public_subnet.*.id, count.index)
security_groups = [aws_security_group.sg.id]
tags = {
Name = "${var.custom_name}-ScyllaDBInstance-Seed-${count.index}"
"Project" = "${var.custom_name}"
"Type" = "Scylla"
"Group" = "Seed"
}
user_data = <<EOF
{
"scylla_yaml": {
"cluster_name": "${var.custom_name}",
"internode_compression": "all",
},
"start_scylla_on_first_boot": true
}
EOF
provisioner "file" {
source = "./stress/schema.cql"
destination = "/home/ubuntu/schema.cql"
}
provisioner "file" {
source = "./ansible/restore_snapshot.sh"
destination = "/home/ubuntu/restore_snapshot.sh"
}
# Set up an SSH connection to each EC2 instance using the scyllaadm user and the private key.
# The coalesce function is used to select the public IP address of ScyllaDB Nodes.
connection {
type = "ssh"
user = "ubuntu"
private_key = tls_private_key.private_key.private_key_pem
host = coalesce(self.public_ip, self.private_ip)
agent = true
}
}
resource "aws_instance" "scylladb_nonseeds" {
count = var.scylla_node_count - 1
ami = var.scylla_ami_id
instance_type = var.scylla_node_type
key_name = aws_key_pair.generated_key.key_name
subnet_id = element(aws_subnet.public_subnet.*.id, count.index)
security_groups = [aws_security_group.sg.id]
tags = {
Name = "${var.custom_name}-ScyllaDBInstance-${count.index}"
"Project" = "${var.custom_name}"
"Type" = "Scylla"
"Group" = "NonSeed"
}
user_data = <<EOF
{
"scylla_yaml": {
"cluster_name": "${var.custom_name}",
"seed_provider": [{"class_name": "org.apache.cassandra.locator.SimpleSeedProvider",
"parameters": [{"seeds": "${aws_instance.scylladb_seed[0].private_ip}"}]}],
"internode_compression": "all",
},
"start_scylla_on_first_boot": false
}
EOF
depends_on = [aws_instance.scylladb_seed]
}
# Generate private key file for Ansible
resource "local_file" "keyfile_ansible_config" {
content = <<-DOC
-----BEGIN RSA PRIVATE KEY-----
${tls_private_key.private_key.private_key_pem}
-----END RSA PRIVATE KEY-----
DOC
filename = "./ansible/key.pem"
}
# Gerenate Ansible config file
resource "local_file" "file_ansible_config" {
content = <<-DOC
# Ansible config
# Generated by Terraform configuration
[defaults]
home=./
inventory=inventory.ini
host_key_checking=False
interpreter_python=auto_silent
force_valid_group_names=ignore
private_key_file=key.pem
remote_user=scyllaadm
DOC
filename = "./ansible/ansible.cfg"
}
# Generate Ansible inventory file
resource "local_file" "file_ansible_inventory" {
depends_on = [aws_instance.scylladb_seed, aws_instance.scylladb_nonseeds, aws_instance.loader_instance, aws_eip.eip, aws_eip_association.eip_association]
content = <<-DOC
# Ansible inventory containing IP addresses from Terraform
# Generated by Terraform configuration
[original_cluster]
${join("\n", concat(aws_instance.scylladb_seed.*.public_ip, slice(aws_instance.scylladb_nonseeds.*.public_ip, 0, 2)))}
[scale_out]
${join("\n", slice(aws_instance.scylladb_nonseeds.*.public_ip, 2, (var.scylla_node_count) - 1))}
[stress]
${join("\n", aws_eip.eip.*.public_ip)}
DOC
filename = "./ansible/inventory.ini"
}
output "scylla_ips" {
value = join(",", [join(",", aws_instance.scylladb_seed.*.private_ip), join(",", aws_instance.scylladb_nonseeds.*.private_ip)])
}
output "scylla_public_ips" {
value = join(",", [join(",", aws_instance.scylladb_seed.*.public_ip), join(",", aws_instance.scylladb_nonseeds.*.public_ip)])
}