-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
195 lines (161 loc) · 4.62 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
# For now not published in the registry, see create-tf-rc-file.sh.
scylladbcloud = {
source = "registry.terraform.io/scylladb/scylladbcloud"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = var.aws_region
shared_credentials_files = ["$HOME/.aws/credentials"]
default_tags {
tags = {
Owner = "Benchmarking"
RunByUser = "Benchmarking"
}
}
}
# Name of the key pair containing private SSH key
resource "random_string" "key_name" {
length = 16
special = false
lower = true
}
# Generate a key
resource "tls_private_key" "ssh_key" {
algorithm = "ED25519"
}
# Public key part uploaded to AWS
resource "aws_key_pair" "ssh_key_pair" {
key_name = random_string.key_name.result
public_key = tls_private_key.ssh_key.public_key_openssh
}
# Private key part written locally to access the instances
resource "local_sensitive_file" "private_key" {
content = tls_private_key.ssh_key.private_key_openssh
filename = "private_key"
}
# This selects the right AMI
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
# This is the VPC for loaders
resource "aws_vpc" "loader_vpc" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
}
resource "aws_internet_gateway" "loader_igw" {
vpc_id = aws_vpc.loader_vpc.id
}
resource "aws_route_table" "main_rt" {
vpc_id = aws_vpc.loader_vpc.id
}
resource "aws_route" "internet_rt" {
route_table_id = aws_route_table.main_rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.loader_igw.id
}
resource "aws_security_group" "public_sg" {
name = "benchmarking-sg-${random_string.key_name.result}"
description = "Allows to reach loaders from the Internet"
vpc_id = aws_vpc.loader_vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
# depends_on = [
# aws_vpc.loader_vpc
# ]
}
# Subnet for each loader (needs to be per AZ)
resource "aws_subnet" "loader_subnet" {
cidr_block = format("10.0.%d.0/24", count.index)
availability_zone = element(var.loader_avaliability_zones, count.index)
vpc_id = aws_vpc.loader_vpc.id
map_public_ip_on_launch = true # auto assigns public IPs, needed to be able to SSH to the loader instances
count = var.loader_instances_count
depends_on = [aws_internet_gateway.loader_igw]
}
# This links route table to subnet
resource "aws_route_table_association" "main_rt_assoc" {
route_table_id = aws_route_table.main_rt.id
subnet_id = element(aws_subnet.loader_subnet.*.id, count.index)
count = var.loader_instances_count
}
# Instances which run ycsb
resource "aws_instance" "loader" {
ami = data.aws_ami.ubuntu.id
instance_type = var.loader_instance_type
key_name = random_string.key_name.result
subnet_id = element(aws_subnet.loader_subnet.*.id, count.index)
vpc_security_group_ids = [aws_security_group.public_sg.id]
count = var.loader_instances_count
tags = {
Name = "Loader"
NodeType = "loader"
}
iam_instance_profile = var.dynamo_testing ? aws_iam_instance_profile.dynamo_profile[0].name : null
}
# This runs provisioning steps
resource "null_resource" "loader" {
# Changes to any instance requires re-provisioning
triggers = {
loader_instance_ids = join(",", aws_instance.loader.*.id)
# Use code below to force recreating null_resource
# always_run = "${timestamp()}"
}
count = var.loader_instances_count
connection {
type = "ssh"
host = element(aws_instance.loader.*.public_ip, count.index)
user = "ubuntu"
private_key = tls_private_key.ssh_key.private_key_openssh
timeout = "1m"
}
# Make ubuntu user owning our binaries
provisioner "remote-exec" {
inline = [
"sudo mkdir /opt/scylla",
"sudo chown ubuntu /opt/scylla",
]
}
# Install dependecies needed for ycsb
provisioner "file" {
source = "opt/deps.sh"
destination = "/opt/scylla/deps.sh"
}
provisioner "remote-exec" {
inline = [
"sudo bash -C /opt/scylla/deps.sh"
]
}
# Copy opt directory containing executables
provisioner "file" {
source = "opt/"
destination = "/opt/scylla"
}
}