forked from AztecProtocol/Setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
646 lines (542 loc) · 15.8 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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
terraform {
backend "s3" {
bucket = "aztec-terraform"
key = "setup/setup-iac"
region = "eu-west-2"
}
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.74.2"
}
}
}
provider "aws" {
profile = "default"
region = "eu-west-2"
}
# Create a vpc.
resource "aws_vpc" "setup" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "setup"
}
}
# We create an addional CIDR block to expand beyond initial /16 limits.
resource "aws_vpc_ipv4_cidr_block_association" "cidr1" {
vpc_id = aws_vpc.setup.id
cidr_block = "10.1.0.0/16"
}
### PUBLIC NETWORK
# Public subnets in each availability zone.
resource "aws_subnet" "public_az1" {
vpc_id = aws_vpc.setup.id
cidr_block = "10.0.0.0/17"
availability_zone = "eu-west-2a"
tags = {
Name = "setup-az1"
}
}
resource "aws_subnet" "public_az2" {
vpc_id = aws_vpc.setup.id
cidr_block = "10.0.128.0/17"
availability_zone = "eu-west-2b"
tags = {
Name = "setup-az2"
}
}
# Internet gateway.
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.setup.id
tags = {
Name = "setup"
}
}
# NAT gateway.
resource "aws_eip" "nat_eip_az1" {
vpc = true
depends_on = [aws_internet_gateway.gw]
}
resource "aws_eip" "nat_eip_az2" {
vpc = true
depends_on = [aws_internet_gateway.gw]
}
resource "aws_nat_gateway" "gw_az1" {
allocation_id = aws_eip.nat_eip_az1.id
subnet_id = aws_subnet.public_az1.id
depends_on = [aws_internet_gateway.gw]
}
resource "aws_nat_gateway" "gw_az2" {
allocation_id = aws_eip.nat_eip_az2.id
subnet_id = aws_subnet.public_az2.id
depends_on = [aws_internet_gateway.gw]
}
# Route main routing table default traffic through gateway.
resource "aws_route" "internet_access" {
route_table_id = aws_vpc.setup.main_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
# Public security group and rules.
resource "aws_security_group" "public" {
name = "setup-public"
vpc_id = aws_vpc.setup.id
tags = {
Name = "setup-public"
}
}
resource "aws_security_group_rule" "public_allow_ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.public.id
}
resource "aws_security_group_rule" "setup_public_allow_http" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.public.id
}
resource "aws_security_group_rule" "setup_public_allow_https" {
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.public.id
}
resource "aws_security_group_rule" "setup_public_allow_all_vpc" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["10.0.0.0/8"]
security_group_id = aws_security_group.public.id
}
resource "aws_security_group_rule" "setup_public_allow_all_outgoing" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.public.id
}
### PRIVATE NETWORK
# Private subnets in each availability zone.
resource "aws_subnet" "private_az1" {
vpc_id = aws_vpc.setup.id
cidr_block = "10.1.0.0/17"
availability_zone = "eu-west-2a"
tags = {
Name = "setup-az1-private"
}
}
resource "aws_subnet" "private_az2" {
vpc_id = aws_vpc.setup.id
cidr_block = "10.1.128.0/17"
availability_zone = "eu-west-2b"
tags = {
Name = "setup-az2-private"
}
}
# Private network routing tables, rules to NAT gateway, and subnet associations.
resource "aws_route_table" "private_az1" {
vpc_id = aws_vpc.setup.id
tags = {
Name = "setup-private-az1"
}
}
resource "aws_route_table" "private_az2" {
vpc_id = aws_vpc.setup.id
tags = {
Name = "setup-private-az2"
}
}
resource "aws_route" "private_az1" {
route_table_id = aws_route_table.private_az1.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.gw_az1.id
}
resource "aws_route" "private_az2" {
route_table_id = aws_route_table.private_az2.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.gw_az2.id
}
resource "aws_route_table_association" "subnet_association_az1" {
subnet_id = aws_subnet.private_az1.id
route_table_id = aws_route_table.private_az1.id
}
resource "aws_route_table_association" "subnet_association_az2" {
subnet_id = aws_subnet.private_az2.id
route_table_id = aws_route_table.private_az2.id
}
# Private security group.
resource "aws_security_group" "private" {
name = "setup-private"
vpc_id = aws_vpc.setup.id
ingress {
protocol = "-1"
from_port = 0
to_port = 0
self = true
}
ingress {
protocol = "tcp"
from_port = 80
to_port = 80
cidr_blocks = ["10.0.0.0/8"]
}
ingress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["${aws_instance.bastion.private_ip}/32"]
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "setup-private"
}
}
# Private link endpoint interfaces to get access to AWS services.
resource "aws_vpc_endpoint" "ecs_agent" {
vpc_id = aws_vpc.setup.id
service_name = "com.amazonaws.eu-west-2.ecs-agent"
vpc_endpoint_type = "Interface"
private_dns_enabled = true
security_group_ids = [
aws_security_group.private.id,
]
subnet_ids = [aws_subnet.public_az1.id]
}
resource "aws_vpc_endpoint" "ecs_telemetry" {
vpc_id = aws_vpc.setup.id
service_name = "com.amazonaws.eu-west-2.ecs-telemetry"
vpc_endpoint_type = "Interface"
private_dns_enabled = true
security_group_ids = [
aws_security_group.private.id,
]
subnet_ids = [aws_subnet.public_az1.id]
}
resource "aws_vpc_endpoint" "ecs" {
vpc_id = aws_vpc.setup.id
service_name = "com.amazonaws.eu-west-2.ecs"
vpc_endpoint_type = "Interface"
private_dns_enabled = true
security_group_ids = [
aws_security_group.private.id,
]
subnet_ids = [aws_subnet.public_az1.id]
}
resource "aws_vpc_endpoint" "logs" {
vpc_id = aws_vpc.setup.id
service_name = "com.amazonaws.eu-west-2.logs"
vpc_endpoint_type = "Interface"
private_dns_enabled = true
security_group_ids = [
aws_security_group.private.id,
]
subnet_ids = [aws_subnet.public_az1.id]
}
resource "aws_vpc_endpoint" "ecr_api" {
vpc_id = aws_vpc.setup.id
service_name = "com.amazonaws.eu-west-2.ecr.api"
vpc_endpoint_type = "Interface"
private_dns_enabled = true
security_group_ids = [
aws_security_group.private.id,
]
subnet_ids = [aws_subnet.public_az1.id]
}
resource "aws_vpc_endpoint" "ecr_dkr" {
vpc_id = aws_vpc.setup.id
service_name = "com.amazonaws.eu-west-2.ecr.dkr"
vpc_endpoint_type = "Interface"
private_dns_enabled = true
security_group_ids = [
aws_security_group.private.id,
]
subnet_ids = [aws_subnet.public_az1.id]
}
resource "aws_vpc_endpoint" "s3" {
vpc_id = aws_vpc.setup.id
service_name = "com.amazonaws.eu-west-2.s3"
vpc_endpoint_type = "Gateway"
route_table_ids = [aws_vpc.setup.main_route_table_id, aws_route_table.private_az1.id, aws_route_table.private_az2.id]
}
# Service discovery.
resource "aws_service_discovery_private_dns_namespace" "local" {
name = "local"
description = "local"
vpc = aws_vpc.setup.id
}
# We require a role to allow the spot fleet manager to terminate spot instances.
data "aws_iam_policy_document" "fleet_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["spotfleet.amazonaws.com", "ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ec2_spot_fleet_role" {
name = "ec2-spot-fleet-role"
assume_role_policy = data.aws_iam_policy_document.fleet_assume_role_policy.json
}
resource "aws_iam_role_policy_attachment" "ec2_spot_fleet_policy" {
role = aws_iam_role.ec2_spot_fleet_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"
}
# We require a task execution role for the ECS container agent to make calls to the ECS API.
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html
data "aws_iam_policy_document" "assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_task_execution_role" {
name = "ecs-task-execution-role"
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}
resource "aws_iam_role_policy_attachment" "ecs_task_execution_policy" {
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
# Create our cluster.
resource "aws_ecs_cluster" "setup" {
name = "setup"
# setting {
# name = "containerInsights"
# value = "enabled"
# }
}
# Create our load balancer.
data "aws_iam_policy_document" "alb_log_policy" {
statement {
actions = ["s3:PutObject"]
resources = ["arn:aws:s3:::aztec-logs/*"]
principals {
type = "AWS"
identifiers = ["652711504416"]
}
}
}
resource "aws_s3_bucket" "logs" {
bucket = "aztec-logs"
acl = "private"
policy = data.aws_iam_policy_document.alb_log_policy.json
}
resource "aws_alb" "setup" {
name = "setup"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.public.id]
subnets = [
aws_subnet.public_az1.id,
aws_subnet.public_az2.id
]
access_logs {
bucket = aws_s3_bucket.logs.bucket
prefix = "setup-alb-logs"
enabled = true
}
tags = {
Name = "setup"
}
}
resource "aws_alb_listener" "http_listener" {
load_balancer_arn = aws_alb.setup.arn
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
resource "aws_alb_listener" "https_listener" {
load_balancer_arn = aws_alb.setup.arn
port = "443"
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = aws_acm_certificate_validation.cert.certificate_arn
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "Not found."
status_code = "404"
}
}
}
# ALB DNS entry.
resource "aws_route53_record" "setup" {
zone_id = "Z1XXO7GDQEVT6B"
name = "setup-staging"
type = "A"
alias {
name = aws_alb.setup.dns_name
zone_id = aws_alb.setup.zone_id
evaluate_target_health = true
}
}
resource "aws_route53_record" "ignition" {
zone_id = "Z1XXO7GDQEVT6B"
name = "ignition"
type = "A"
alias {
name = aws_alb.setup.dns_name
zone_id = aws_alb.setup.zone_id
evaluate_target_health = true
}
}
# QR Code DNS entry and redirection.
resource "aws_s3_bucket" "qr_redirect" {
bucket = "qr.aztecprotocol.com"
acl = "private"
website {
index_document = "index.html"
routing_rules = <<EOF
[{
"Redirect": {
"Protocol": "https",
"HostName": "aztecprotocol.us7.list-manage.com",
"ReplaceKeyWith": "/subscribe?u=0f5fa2f22c3349ec01c3d1fdc&id=1b8d51cab0"
}
}]
EOF
}
}
resource "aws_route53_record" "qr" {
zone_id = "Z1XXO7GDQEVT6B"
name = "qr"
type = "A"
alias {
name = aws_s3_bucket.qr_redirect.website_domain
zone_id = aws_s3_bucket.qr_redirect.hosted_zone_id
evaluate_target_health = true
}
}
# Enterprise Code DNS entry and redirection.
resource "aws_s3_bucket" "enterprise_redirect" {
bucket = "enterprise.aztecprotocol.com"
acl = "private"
website {
index_document = "index.html"
routing_rules = <<EOF
[{
"Redirect": {
"Protocol": "https",
"HostName": "www.aztecprotocol.com",
"ReplaceKeyWith": ""
}
}]
EOF
}
}
resource "aws_route53_record" "enterprise" {
zone_id = "Z1XXO7GDQEVT6B"
name = "enterprise"
type = "A"
alias {
name = aws_s3_bucket.enterprise_redirect.website_domain
zone_id = aws_s3_bucket.enterprise_redirect.hosted_zone_id
evaluate_target_health = true
}
}
# Certificate management.
resource "aws_acm_certificate" "cert" {
domain_name = "aztecprotocol.com"
subject_alternative_names = ["*.aztecprotocol.com"]
validation_method = "DNS"
tags = {
Name = "aztecprotocol.com"
}
}
resource "aws_route53_record" "cert_validation" {
for_each = {
for dvo in aws_acm_certificate.cert.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
zone_id = "Z1XXO7GDQEVT6B"
ttl = 300
allow_overwrite = true
name = each.value.name
records = [each.value.record]
type = each.value.type
}
resource "aws_acm_certificate_validation" "cert" {
certificate_arn = aws_acm_certificate.cert.arn
validation_record_fqdns = [for record in aws_route53_record.cert_validation : record.fqdn]
}
# EC2 instances roles, policies, keys.
data "aws_iam_policy_document" "instance_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "setup_ecs_instance" {
name = "setup-ecs-instance"
assume_role_policy = data.aws_iam_policy_document.instance_assume_role_policy.json
}
resource "aws_iam_instance_profile" "ecs" {
name = "setup-ecs-instance"
role = aws_iam_role.setup_ecs_instance.name
}
resource "aws_iam_policy_attachment" "ecs_instance" {
name = "setup-ecs-instance"
roles = [aws_iam_role.setup_ecs_instance.name]
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
resource "aws_key_pair" "instance_key_pair" {
key_name = "setup-instance-key-pair"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDagCvr/+CA1jmFaJf+e9+Kw6iwfhvaKOpfbGEl5zLgB+rum5L4Kga6Jow1gLQeMnAHfqc2IgpsU4t04c8PYApAt8AWNDL+KxMiFytfjKfJ2DZJA73CYkFnkfnMtU+ki+JG9dAHd6m7ShtCSzE5n6EDO2yWCVWQfqE3dcnpwrymSWkJYrbxzeOixiNZ4f1nD9ddvFvTWGB4l+et5SWgeIaYgJYDqTI2teRt9ytJiDGrCWXs9olHsCZOL6TEJPUQmNekwBkjMAZ4TmbBMjwbUlIxOpW2UxzlONcNn7IlRcGQg0Gdbkpo/zOlCNXsvacvnphDk5vKKaQj+aQiG916LU5P [email protected]"
}
# Bastion
resource "aws_instance" "bastion" {
ami = "ami-0d8e27447ec2c8410"
instance_type = "t3.small"
subnet_id = aws_subnet.public_az1.id
vpc_security_group_ids = [aws_security_group.public.id]
iam_instance_profile = aws_iam_instance_profile.ecs.name
associate_public_ip_address = true
key_name = aws_key_pair.instance_key_pair.key_name
availability_zone = "eu-west-2a"
tags = {
Name = "bastion"
prometheus = ""
}
}
resource "aws_route53_record" "bastion" {
zone_id = "Z1XXO7GDQEVT6B"
name = "bastion"
type = "A"
ttl = 300
records = [aws_instance.bastion.public_ip]
}