-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
245 lines (206 loc) · 5.6 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = var.region
}
resource "aws_security_group" "ord_server_ssh_sg" {
name = "ord_server_ssh_sg"
ingress { # ssh
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress { # websocket
from_port = 8765
to_port = 8765
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"]
}
tags = {
Name = var.resource_tag_name
}
}
resource "tls_private_key" "pk" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "kp" {
key_name = "ord_server_key" # Create "ord_server_key" in AWS
public_key = tls_private_key.pk.public_key_openssh
provisioner "local-exec" { # Create "ord_server.pem" locally
command = <<-EOT
echo '${tls_private_key.pk.private_key_pem}' > ~/.ssh/ord_server_${tls_private_key.pk.id}.pem
chmod 400 ~/.ssh/ord_server_${tls_private_key.pk.id}.pem
EOT
}
tags = {
Name = var.resource_tag_name
}
}
resource "random_password" "password" {
length = 16
special = false
}
data "cloudinit_config" "post_deploy" {
part {
content_type = "text/x-shellscript"
content = templatefile("init.tpl", {
# environment = var.env
})
}
}
resource "aws_dynamodb_table" "ord_server_table" {
name = "OrdControlTable"
billing_mode = "PROVISIONED"
read_capacity = 20
write_capacity = 20
hash_key = "Id"
range_key = "DateAdded"
attribute {
name = "Id"
type = "S"
}
attribute {
name = "DateAdded"
type = "S"
}
attribute {
name = "Name"
type = "S"
}
ttl {
attribute_name = "TimeToExist"
enabled = false
}
global_secondary_index {
name = "NameIndex"
hash_key = "Name"
range_key = "DateAdded"
write_capacity = 10
read_capacity = 10
projection_type = "KEYS_ONLY"
/* non_key_attributes = ["UserId"] */
}
tags = {
Name = var.resource_tag_name
}
}
# FUN WITH PERMISSIONS
# ec2-role -> policy file -> instance profile -> {instance-profile in ec2 resource}
resource "aws_iam_policy" "ordserver_ec2_policy" {
name = "ordserver_ec2_policy"
description = "ordserver_ec2_policy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"dynamodb:*"
]
Resource = [
"${aws_dynamodb_table.ord_server_table.arn}"
]
}
]
})
tags = {
Name = var.resource_tag_name
}
}
resource "aws_iam_role" "ordserver_ec2_role" {
name = "ordserver_ec2_role"
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
tags = {
Name = var.resource_tag_name
}
}
resource "aws_iam_policy_attachment" "ordserver_ec2_policy_attachment" {
name = "ordserver_ec2_policy_attachment"
policy_arn = aws_iam_policy.ordserver_ec2_policy.arn
roles = [aws_iam_role.ordserver_ec2_role.name]
}
resource "aws_iam_instance_profile" "ordserver_ec2_instance_profile" {
name = "ordserver_ec2_instance_profile"
role = aws_iam_role.ordserver_ec2_role.name
tags = {
Name = var.resource_tag_name
}
}
resource "aws_instance" "ord_server" {
ami = "ami-095413544ce52437d"
instance_type = var.instance_type
availability_zone = var.availability_zone
user_data = data.cloudinit_config.post_deploy.rendered
key_name = aws_key_pair.kp.key_name
iam_instance_profile = aws_iam_instance_profile.ordserver_ec2_instance_profile.name
security_groups = [aws_security_group.ord_server_ssh_sg.name]
tags = {
Name = var.resource_tag_name
}
provisioner "file" {
source = "server"
destination = "/home/ubuntu/OrdControl"
connection {
type = "ssh"
host = aws_instance.ord_server.public_ip
user = "ubuntu"
private_key = file("~/.ssh/ord_server_${tls_private_key.pk.id}.pem")
insecure = true
}
}
provisioner "local-exec" {
command = <<-EOT
echo 'window.OrdControl = window.OrdControl || {};' > client/js/env.js
echo 'window.OrdControl.password="${random_password.password.result}";' >> client/js/env.js
echo 'window.OrdControl.wsurl="${aws_instance.ord_server.public_dns}";' >> client/js/env.js
echo `window.OrdControl.connectionString = "ssh -o 'StrictHostKeyChecking no' -i ~/.ssh/ord_server_${tls_private_key.pk.id}.pem ubuntu@${aws_instance.ord_server.public_dns}";` >> client/js/env.js
cp client/js/env.js server/client-env.js.txt
EOT
}
}
resource "aws_ebs_volume" "bitcoin_ord_data" {
# This snapshot is from February 27 2023, & contains fully synced bitcoind & ord data dirs
snapshot_id = var.snapshot_id
availability_zone = var.availability_zone
type = "gp3"
size = 3123
iops = 4000
tags = {
Name = var.resource_tag_name
}
}
resource "aws_volume_attachment" "bitcoin_ord_data_att" {
# note that this device_name is not respected by the instance types that use nvme
device_name = "/dev/xvdh"
volume_id = aws_ebs_volume.bitcoin_ord_data.id
instance_id = aws_instance.ord_server.id
}