generated from adjacentresearchxyz/deployment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
268 lines (224 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.29"
}
}
backend "remote" {
organization = "adajcentresearch"
hostname = "app.terraform.io"
workspaces {
prefix = "adjacent-"
}
}
}
# variable definitions
# variable values are defined for each corresponding server in `./variables`
variable "volume_size" {
type = string
description = "Size of the volume"
default = "500" #GiB
}
variable "instance_type" {
type = string
description = "Type of the instance"
default = "t2.nano"
}
variable "tags" {
type = map(any)
description = "Tags associated with naming the instance"
default = {
tags = {
"Name" : "nixos-base"
"env" : "base"
}
}
}
variable "access_key" {
type = string
description = "AWS Access Key"
}
variable "secret_key" {
type = string
description = "AWS Secret Key"
}
variable "key_name" {
type = string
description = "AWS key to autheticate with"
default = "adjacentresearch_rsa"
}
variable "region" {
description = "The AWS Region"
default = "us-east-1"
}
variable "availability_zone" {
default = "us-east-1a"
description = "The names of the availability zones to use"
}
variable "vpc_cidr" {
default = "10.0.0.0/16"
description = "The CIDR block of the vpc"
}
variable "public_subnet_cidr" {
default = "10.0.10.0/24"
description = "The CIDR block for the public subnet"
}
# Configure the AWS Provider
provider "aws" {
# Keys can also be exported see https://registry.terraform.io/providers/hashicorp/aws/latest/docs#authentication
# export AWS_ACCESS_KEY_ID="anaccesskey"
# export AWS_SECRET_ACCESS_KEY="asecretkey"
access_key = var.access_key
secret_key = var.secret_key
region = var.region
}
module "nixos_image" {
source = "git::https://github.com/tweag/terraform-nixos.git//aws_image_nixos?ref=5f5a0408b299874d6a29d1271e9bffeee4c9ca71"
release = "22.05"
}
# Define VPC
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
enable_dns_support = true
tags = var.tags
}
# Define public subnet
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = var.public_subnet_cidr
availability_zone = var.availability_zone
map_public_ip_on_launch = true
tags = var.tags
}
# Define internet gateway
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.vpc.id
tags = var.tags
}
# Create 2nd route table
resource "aws_route_table" "second_rt" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
tags = var.tags
}
# Associate public subnet with 2nnd route table for internet access
resource "aws_route_table_association" "public_subnet_asso" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.second_rt.id
}
resource "aws_security_group" "default" {
vpc_id = aws_vpc.vpc.id
depends_on = [aws_vpc.vpc]
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# solana gossip ports https://docs.solana.com/running-validator/validator-reqs#required
ingress {
from_port = 8000
to_port = 10000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# solana gossip ports https://docs.solana.com/running-validator/validator-reqs#required
ingress {
from_port = 8000
to_port = 10000
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
# solana RPC ports (HTTP) https://docs.solana.com/running-validator/validator-reqs#optional
ingress {
from_port = 8899
to_port = 8899
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# solana RPC ports (Websocket) https://docs.solana.com/running-validator/validator-reqs#optional
ingress {
from_port = 8900
to_port = 8900
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"]
}
tags = var.tags
}
resource "tls_private_key" "state_ssh_key" {
algorithm = "RSA"
}
resource "aws_instance" "machine" {
ami = module.nixos_image.ami
# ami = "ami-0223db08811f6fb2d" # nixos 22.05 for us-east-1
# ami = "ami-0a743534fa3e51b41" # nixos 22.05 for us-east-2
instance_type = var.instance_type
security_groups = [aws_security_group.default.id]
subnet_id = aws_subnet.public_subnet.id
key_name = var.key_name
tags = var.tags
root_block_device {
volume_size = var.volume_size
}
connection {
type = "ssh"
user = "root"
private_key = file("./adjacentresearch_rsa.pem") # your private key here
host = self.public_ip
}
provisioner "remote-exec" {
inline = [
"nixos-generate-config", # building base `nixos` config
]
}
provisioner "file" {
source = "./nixos/" # note need the trailing slash to fully upload all the directories contents to the destination https://www.terraform.io/language/resources/provisioners/file#directory-uploads
destination = "/etc/nixos/"
}
# add adjacent channel and install solana packages
provisioner "remote-exec" {
inline = [
"nix-channel --add https://github.com/adjacentresearchxyz/nix-channel/archive/main.tar.gz adjacent",
"nix-channel --update",
"nix-env -f https://github.com/adjacentresearchxyz/nix-channel/archive/main.tar.gz -i solana-validator",
]
}
# generate keys
# note: creating on devnet given in order to create vote account and start on mainnet SOL needs to be sent to an the fee payer account
provisioner "remote-exec" {
inline = [
"solana config set --url http://api.devnet.solana.com", # config for mainnet-beta
"solana transaction-count",
"mkdir /etc/nixos/solana",
"solana-keygen new -o /etc/nixos/solana/validator-keypair.json --no-bip39-passphrase", # generate validator-keypair
"solana config set --keypair /etc/nixos/solana/validator-keypair.json", # set keypair in config
"solana-keygen new -o /etc/nixos/solana/authorized-withdrawer-keypair.json --no-bip39-passphrase", # create authorized withdrawer
"solana-keygen new -o /etc/nixos/solana/vote-account-keypair.json --no-bip39-passphrase", # create vote account
"solana airdrop 1", # airdrop some SOL for vote account
"solana create-vote-account /etc/nixos/solana/vote-account-keypair.json /etc/nixos/solana/validator-keypair.json /etc/nixos/solana/authorized-withdrawer-keypair.json", # create vote account
]
}
provisioner "remote-exec" {
inline = [
"nixos-rebuild switch", # building `nixos` config
]
}
}
output "public_dns" {
value = aws_instance.machine.public_dns
}
output "public_ip" {
value = aws_instance.machine.public_ip
}