-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathmain.tf
572 lines (520 loc) · 16.7 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
terraform {
backend "s3" {
bucket = "aztec-terraform"
region = "eu-west-2"
}
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.74.2"
}
}
}
# Define provider and region
provider "aws" {
region = "eu-west-2"
}
data "terraform_remote_state" "setup_iac" {
backend = "s3"
config = {
bucket = "aztec-terraform"
key = "setup/setup-iac"
region = "eu-west-2"
}
}
data "terraform_remote_state" "aztec2_iac" {
backend = "s3"
config = {
bucket = "aztec-terraform"
key = "aztec2/iac"
region = "eu-west-2"
}
}
data "terraform_remote_state" "aztec-network_iac" {
backend = "s3"
config = {
bucket = "aztec-terraform"
key = "aztec-network/iac"
region = "eu-west-2"
}
}
data "terraform_remote_state" "l1_contracts" {
backend = "s3"
config = {
bucket = "aztec-terraform"
key = "${var.DEPLOY_TAG}/l1-contracts"
region = "eu-west-2"
}
}
# Compute local variables
locals {
sequencer_private_keys = var.SEQUENCER_PRIVATE_KEYS
node_p2p_private_keys = var.NODE_P2P_PRIVATE_KEYS
node_count = length(local.sequencer_private_keys)
data_dir = "/usr/src/yarn-project/aztec"
eth_host = var.ETHEREUM_HOST != "" ? var.ETHEREUM_HOST : "https://${var.DEPLOY_TAG}-mainnet-fork.aztec.network:8545/admin-${var.FORK_ADMIN_API_KEY}"
}
output "node_count" {
value = local.node_count
}
resource "aws_cloudwatch_log_group" "aztec-node-log-group" {
count = local.node_count
name = "/fargate/service/${var.DEPLOY_TAG}/aztec-node-${count.index + 1}"
retention_in_days = 14
}
resource "aws_service_discovery_service" "aztec-node" {
count = local.node_count
name = "${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"
health_check_custom_config {
failure_threshold = 1
}
dns_config {
namespace_id = data.terraform_remote_state.setup_iac.outputs.local_service_discovery_id
dns_records {
ttl = 60
type = "A"
}
dns_records {
ttl = 60
type = "SRV"
}
routing_policy = "MULTIVALUE"
}
# Terraform just fails if this resource changes and you have registered instances.
provisioner "local-exec" {
when = destroy
command = "${path.module}/../servicediscovery-drain.sh ${self.id}"
}
}
# Configure an EFS filesystem.
resource "aws_efs_file_system" "node_data_store" {
creation_token = "${var.DEPLOY_TAG}-node-data"
tags = {
Name = "${var.DEPLOY_TAG}-node-data"
}
lifecycle_policy {
transition_to_ia = "AFTER_14_DAYS"
}
}
resource "aws_efs_mount_target" "public_az1" {
file_system_id = aws_efs_file_system.node_data_store.id
subnet_id = data.terraform_remote_state.setup_iac.outputs.subnet_az1_id
security_groups = [data.terraform_remote_state.setup_iac.outputs.security_group_public_id]
}
resource "aws_efs_mount_target" "public_az2" {
file_system_id = aws_efs_file_system.node_data_store.id
subnet_id = data.terraform_remote_state.setup_iac.outputs.subnet_az2_id
security_groups = [data.terraform_remote_state.setup_iac.outputs.security_group_public_id]
}
data "template_file" "user_data" {
count = local.node_count
template = <<EOF
#!/bin/bash
echo ECS_CLUSTER=${data.terraform_remote_state.setup_iac.outputs.ecs_cluster_name} >> /etc/ecs/ecs.config
echo 'ECS_INSTANCE_ATTRIBUTES={"group": "${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"}' >> /etc/ecs/ecs.config
EOF
}
# Launch template for our prover agents
# 4 cores and 8 GB memory
resource "aws_launch_template" "aztec-node-launch-template" {
count = local.node_count
name = "${var.DEPLOY_TAG}-aztec-node-launch-template-${count.index + 1}"
image_id = "ami-0cd4858f2b923aa6b"
instance_type = "c6a.xlarge"
vpc_security_group_ids = [data.terraform_remote_state.setup_iac.outputs.security_group_private_id]
iam_instance_profile {
name = data.terraform_remote_state.setup_iac.outputs.ecs_instance_profile_name
}
key_name = data.terraform_remote_state.setup_iac.outputs.ecs_instance_key_pair_name
user_data = base64encode(data.template_file.user_data[count.index].rendered)
tag_specifications {
resource_type = "instance"
tags = {
Name = "${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"
prometheus = ""
}
}
}
resource "aws_ec2_fleet" "aztec_node_fleet" {
count = local.node_count
launch_template_config {
launch_template_specification {
launch_template_id = aws_launch_template.aztec-node-launch-template[count.index].id
version = aws_launch_template.aztec-node-launch-template[count.index].latest_version
}
override {
subnet_id = data.terraform_remote_state.setup_iac.outputs.subnet_az1_private_id
availability_zone = "eu-west-2a"
}
override {
subnet_id = data.terraform_remote_state.setup_iac.outputs.subnet_az2_private_id
availability_zone = "eu-west-2b"
}
}
target_capacity_specification {
default_target_capacity_type = "on-demand"
total_target_capacity = 1
spot_target_capacity = 0
on_demand_target_capacity = 1
}
terminate_instances = true
terminate_instances_with_expiration = true
}
# Define task definitions for each node.
resource "aws_ecs_task_definition" "aztec-node" {
count = local.node_count
family = "${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"
requires_compatibilities = ["EC2"]
network_mode = "awsvpc"
execution_role_arn = data.terraform_remote_state.setup_iac.outputs.ecs_task_execution_role_arn
task_role_arn = data.terraform_remote_state.aztec2_iac.outputs.cloudwatch_logging_ecs_role_arn
volume {
name = "efs-data-store"
efs_volume_configuration {
root_directory = "/"
file_system_id = aws_efs_file_system.node_data_store.id
}
}
container_definitions = jsonencode([
{
name = "${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"
image = "${var.DOCKERHUB_ACCOUNT}/aztec:${var.IMAGE_TAG}"
command = ["start", "--node", "--archiver", "--sequencer"]
essential = true
cpu = 4096
memoryReservation = 7790
portMappings = [
{
containerPort = 80
},
{
containerPort = var.NODE_P2P_TCP_PORT + count.index
protocol = "tcp"
},
{
containerPort = var.NODE_P2P_UDP_PORT + count.index
protocol = "udp"
}
]
environment = [
{
name = "NODE_ENV"
value = "production"
},
{
name = "DEPLOY_TAG"
value = var.DEPLOY_TAG
},
{
name = "DEPLOY_AZTEC_CONTRACTS"
value = "false"
},
{
name = "AZTEC_PORT"
value = "80"
},
{
name = "DEBUG"
value = "aztec:*,-json-rpc:json_proxy:*,-aztec:avm_simulator:*"
},
{
name = "ETHEREUM_HOST"
value = "${local.eth_host}"
},
{
name = "DATA_DIRECTORY"
value = "${local.data_dir}/node_${count.index + 1}/data"
},
{
name = "ARCHIVER_POLLING_INTERVAL"
value = "10000"
},
{
name = "ARCHIVER_L1_START_BLOCK",
value = "15918000"
},
{
name = "SEQ_RETRY_INTERVAL"
value = "10000"
},
{
name = "SEQ_MAX_TX_PER_BLOCK"
value = var.SEQ_MAX_TX_PER_BLOCK
},
{
name = "SEQ_MIN_TX_PER_BLOCK"
value = var.SEQ_MIN_TX_PER_BLOCK
},
{
name = "SEQ_MAX_SECONDS_BETWEEN_BLOCKS"
value = var.SEQ_MAX_SECONDS_BETWEEN_BLOCKS
},
{
name = "SEQ_MIN_SECONDS_BETWEEN_BLOCKS"
value = var.SEQ_MIN_SECONDS_BETWEEN_BLOCKS
},
{
name = "SEQ_PUBLISHER_PRIVATE_KEY"
value = local.sequencer_private_keys[count.index]
},
{
name = "VALIDATOR_PRIVATE_KEY"
value = local.sequencer_private_keys[count.index]
},
{
name = "ROLLUP_CONTRACT_ADDRESS"
value = data.terraform_remote_state.l1_contracts.outputs.rollup_contract_address
},
{
name = "INBOX_CONTRACT_ADDRESS"
value = data.terraform_remote_state.l1_contracts.outputs.inbox_contract_address
},
{
name = "OUTBOX_CONTRACT_ADDRESS"
value = data.terraform_remote_state.l1_contracts.outputs.outbox_contract_address
},
{
name = "REGISTRY_CONTRACT_ADDRESS"
value = data.terraform_remote_state.l1_contracts.outputs.registry_contract_address
},
{
name = "FEE_JUICE_CONTRACT_ADDRESS"
value = data.terraform_remote_state.l1_contracts.outputs.fee_juice_contract_address
},
{
name = "FEE_JUICE_PORTAL_CONTRACT_ADDRESS"
value = data.terraform_remote_state.l1_contracts.outputs.FEE_JUICE_PORTAL_CONTRACT_ADDRESS
},
{
name = "API_KEY"
value = var.API_KEY
},
{
name = "API_PREFIX"
value = "/${var.DEPLOY_TAG}/aztec-node-${count.index + 1}/${var.API_KEY}"
},
{
name = "P2P_TCP_LISTEN_ADDR"
value = "0.0.0.0:${var.NODE_P2P_TCP_PORT + count.index}"
},
{
name = "P2P_UDP_LISTEN_ADDR"
value = "0.0.0.0:${var.NODE_P2P_UDP_PORT + count.index}"
},
{
name = "P2P_TCP_ANNOUNCE_ADDR"
value = ":${var.NODE_P2P_TCP_PORT + count.index}"
},
{
name = "P2P_UDP_ANNOUNCE_ADDR"
value = ":${var.NODE_P2P_UDP_PORT + count.index}"
},
{
name = "P2P_QUERY_FOR_IP"
value = "true"
},
{
name = "BOOTSTRAP_NODES"
value = var.BOOTSTRAP_NODES
},
{
name = "P2P_ENABLED"
value = tostring(var.P2P_ENABLED)
},
{
name = "L1_CHAIN_ID"
value = var.L1_CHAIN_ID
},
{
name = "PEER_ID_PRIVATE_KEY"
value = local.node_p2p_private_keys[count.index]
},
{
name = "P2P_MIN_PEERS"
value = var.P2P_MIN_PEERS
},
{
name = "P2P_MAX_PEERS"
value = var.P2P_MAX_PEERS
},
{
name = "P2P_BLOCK_CHECK_INTERVAL_MS"
value = "10000"
},
{
name = "P2P_PEER_CHECK_INTERVAL_MS"
value = "2000"
},
{
name = "P2P_TX_POOL_KEEP_PROVEN_FOR",
value = tostring(var.P2P_TX_POOL_KEEP_PROVEN_FOR)
},
{
name = "PROVER_AGENT_ENABLED"
value = "false"
},
{
name = "PROVER_AGENT_CONCURRENCY",
value = "0"
},
{
name = "PROVER_REAL_PROOFS"
value = tostring(var.PROVING_ENABLED)
},
{
name = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"
value = "http://aztec-otel.local:4318/v1/metrics"
},
{
name = "OTEL_SERVICE_NAME"
value = "${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"
},
{
name = "BB_WORKING_DIRECTORY"
value = "${local.data_dir}/node_${count.index + 1}/temp"
},
{
name = "ACVM_WORKING_DIRECTORY"
value = "${local.data_dir}/node_${count.index + 1}/temp"
},
{
name = "LOG_LEVEL"
value = "info"
},
{
name = "LOG_JSON",
value = "1"
},
{
name = "NETWORK_NAME",
value = "${var.DEPLOY_TAG}"
},
{
name = "VALIDATOR_DISABLED",
value = "1"
},
]
mountPoints = [
{
containerPath = "${local.data_dir}/node_${count.index + 1}"
sourceVolume = "efs-data-store"
}
]
dependsOn = [
]
logConfiguration = {
logDriver = "awslogs"
options = {
"awslogs-group" = "/fargate/service/${var.DEPLOY_TAG}/aztec-node-${count.index + 1}"
"awslogs-region" = "eu-west-2"
"awslogs-stream-prefix" = "ecs"
}
}
}])
}
resource "aws_ecs_service" "aztec-node" {
count = local.node_count
name = "${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"
cluster = data.terraform_remote_state.setup_iac.outputs.ecs_cluster_id
launch_type = "EC2"
desired_count = 1
deployment_maximum_percent = 100
deployment_minimum_healthy_percent = 0
#platform_version = "1.4.0"
force_new_deployment = true
enable_execute_command = true
network_configuration {
#assign_public_ip = true
subnets = [
data.terraform_remote_state.setup_iac.outputs.subnet_az1_private_id,
data.terraform_remote_state.setup_iac.outputs.subnet_az2_private_id
]
security_groups = [data.terraform_remote_state.aztec-network_iac.outputs.p2p_security_group_id, data.terraform_remote_state.setup_iac.outputs.security_group_private_id]
}
load_balancer {
target_group_arn = aws_alb_target_group.aztec-node-http[count.index].arn
container_name = "${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"
container_port = 80
}
service_registries {
registry_arn = aws_service_discovery_service.aztec-node[count.index].arn
container_name = "${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"
container_port = 80
}
placement_constraints {
type = "memberOf"
expression = "attribute:group == ${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"
}
task_definition = aws_ecs_task_definition.aztec-node[count.index].family
}
# Configure ALB to route /aztec-node to server.
resource "aws_alb_target_group" "aztec-node-http" {
count = local.node_count
name = "${var.DEPLOY_TAG}-node-${count.index + 1}-http-target"
port = 80
protocol = "HTTP"
target_type = "ip"
vpc_id = data.terraform_remote_state.setup_iac.outputs.vpc_id
deregistration_delay = 5
health_check {
path = "/${var.DEPLOY_TAG}/aztec-node-${count.index + 1}/${var.API_KEY}/status"
matcher = "200"
interval = 10
healthy_threshold = 2
unhealthy_threshold = 5
timeout = 5
}
tags = {
name = "${var.DEPLOY_TAG}-aztec-node-${count.index + 1}"
}
}
resource "aws_lb_listener_rule" "api" {
count = local.node_count
listener_arn = data.terraform_remote_state.aztec2_iac.outputs.alb_listener_arn
priority = var.NODE_LB_RULE_PRIORITY + count.index
action {
type = "forward"
target_group_arn = aws_alb_target_group.aztec-node-http[count.index].arn
}
condition {
path_pattern {
values = ["/${var.DEPLOY_TAG}/aztec-node-${count.index + 1}/${var.API_KEY}*"]
}
}
}
resource "aws_security_group_rule" "allow-node-tcp-in" {
count = local.node_count
type = "ingress"
from_port = var.NODE_P2P_TCP_PORT + count.index
to_port = var.NODE_P2P_TCP_PORT + count.index
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = data.terraform_remote_state.aztec-network_iac.outputs.p2p_security_group_id
}
resource "aws_security_group_rule" "allow-node-tcp-out" {
count = local.node_count
type = "egress"
from_port = var.NODE_P2P_TCP_PORT + count.index
to_port = var.NODE_P2P_TCP_PORT + count.index
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = data.terraform_remote_state.aztec-network_iac.outputs.p2p_security_group_id
}
resource "aws_security_group_rule" "allow-node-udp-in" {
count = local.node_count
type = "ingress"
from_port = var.NODE_P2P_UDP_PORT
to_port = var.NODE_P2P_UDP_PORT + count.index
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = data.terraform_remote_state.aztec-network_iac.outputs.p2p_security_group_id
}
resource "aws_security_group_rule" "allow-node-udp-out" {
count = local.node_count
type = "egress"
from_port = var.NODE_P2P_UDP_PORT
to_port = var.NODE_P2P_UDP_PORT + count.index
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = data.terraform_remote_state.aztec-network_iac.outputs.p2p_security_group_id
}