Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment: IaC - Terraform #222 #332

Merged
merged 36 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b61c31b
Add initial terraform aws layout
Apr 7, 2022
89210f2
Merge remote-tracking branch 'upstream/master' into uabjabborov/issue222
Apr 7, 2022
cc861b3
Add terraform validator module implementation
Apr 7, 2022
5f9ca11
Move terraform aws regions to variables
Apr 7, 2022
b48e422
Merge remote-tracking branch 'upstream/master' into uabjabborov/issue222
Apr 11, 2022
2b6ae12
Refactor Validator module terraform scripts
Apr 12, 2022
827ea52
Add Private Sentries module implementation
Apr 12, 2022
b275d14
Add private security group to Validator instance
Apr 12, 2022
b02c5d6
Fix typo in Private Sentry security group name
Apr 12, 2022
f7d5046
Fix typos in Private Sentry module security groups
Apr 12, 2022
866cb16
Rename Private Sentry VPC peering module
Apr 12, 2022
a006d8b
Rename terraform aws_instance resource
Apr 12, 2022
0de8c8a
Add elastic IPs to Private Sentry nodes
Apr 12, 2022
d9afa63
Remove metadata_options from Validator node instance
Apr 12, 2022
41c4229
Add name tag to Private Sentry elastic IP
Apr 12, 2022
4645dd6
Create only one elastic IP private-sentries
Apr 12, 2022
830694e
Add terraform public-sentries module implementation
Apr 12, 2022
814c762
Rename validator_vpc to peer_vpc in private-sentries module
Apr 12, 2022
b65361e
Remove unused output from public-sentries terraform module
Apr 12, 2022
a1583c7
Add terraform observers module implementation
Apr 13, 2022
052ea7f
Assign Observers VPC network mask using region_index input variable
Apr 13, 2022
5d768da
Refactor terraform/aws/main.tf
Apr 13, 2022
433f8a0
Enable multi-region public sentries in terraform
Apr 13, 2022
7e13ed0
Merge remote-tracking branch 'upstream/master' into uabjabborov/issue222
Apr 13, 2022
be39ac1
Update default terraform region
Apr 13, 2022
b299621
Add vpc_network_prefix local for validator VPC
Apr 13, 2022
d532799
Add vpc_network_prefix local for private sentries VPC
Apr 13, 2022
5128f9e
Update aws deployment diagram according to terraform
Apr 13, 2022
b5d4042
Enable IPv6 for public sentries and seeds
Apr 14, 2022
9045c44
Disable elastic IP for public sentry nodes
Apr 14, 2022
7467065
Enable TLS for observer load balancers
Apr 15, 2022
eca9da0
Generate ACM certificates for Observer LB listeners
Apr 18, 2022
0cd50b7
Make IPv6 optional for public sentry nodes and seeds
Apr 18, 2022
a933130
Merge remote-tracking branch 'upstream/master' into uabjabborov/issue222
Apr 18, 2022
cb19010
Fix indent
Apr 18, 2022
7384707
Add consensus params to Validator config
Apr 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions deployment/terraform/aws/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
provider "aws" {
alias = "region_1"
region = var.region_1
}

provider "aws" {
alias = "region_2"
region = var.region_2
}

# Validator
module "validator" {
source = "./validator"
providers = {
aws = aws.region_1
}
}

# Private Sentries
module "private_sentries" {
source = "./private-sentries"

providers = {
aws = aws.region_1
aws.peer = aws.region_1
}

peer_vpc = module.validator.vpc
}

# Public Sentries region 1
module "public_sentries_1" {
source = "./public-sentries"
nodes_count = 1

# enable_ipv6 = false

providers = {
aws = aws.region_1
aws.peer = aws.region_1
}

region_index = 1
peer_vpc = module.private_sentries.vpc
}

# Public Sentries region 2
module "public_sentries_2" {
source = "./public-sentries"
nodes_count = 1

# enable_ipv6 = false

providers = {
aws = aws.region_2
aws.peer = aws.region_1
}

region_index = 2
peer_vpc = module.private_sentries.vpc
}

# Observers region 1
module "observers_1" {
source = "./observers"

providers = {
aws = aws.region_1
aws.peer = aws.region_1
}

root_domain_name = var.root_domain_name
enable_tls = var.enable_tls

region_index = 1
peer_vpc = module.private_sentries.vpc
}

# Observers region 2
module "observers_2" {
source = "./observers"

providers = {
aws = aws.region_2
aws.peer = aws.region_1
}

root_domain_name = var.root_domain_name
enable_tls = var.enable_tls

region_index = 2
peer_vpc = module.private_sentries.vpc
}
25 changes: 25 additions & 0 deletions deployment/terraform/aws/observers/acm.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "aws_acm_certificate" "this_acm_cert" {
count = local.enable_tls ? 1 : 0

domain_name = "on.${data.aws_route53_zone.this_zone[0].name}"
validation_method = "DNS"
}

resource "aws_route53_record" "this_acm_val_records" {
count = local.enable_tls ? length(aws_acm_certificate.this_acm_cert[0].domain_validation_options) : 0

name = tolist(aws_acm_certificate.this_acm_cert[0].domain_validation_options)[count.index].resource_record_name
records = [tolist(aws_acm_certificate.this_acm_cert[0].domain_validation_options)[count.index].resource_record_value]
type = tolist(aws_acm_certificate.this_acm_cert[0].domain_validation_options)[count.index].resource_record_type

allow_overwrite = true
ttl = 60
zone_id = data.aws_route53_zone.this_zone[0].zone_id
}

resource "aws_acm_certificate_validation" "this_acm_cert_validation" {
count = local.enable_tls ? 1 : 0

certificate_arn = aws_acm_certificate.this_acm_cert[0].arn
validation_record_fqdns = aws_route53_record.this_acm_val_records[*].fqdn
}
162 changes: 162 additions & 0 deletions deployment/terraform/aws/observers/elb.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
resource "aws_lb" "this_nlb" {
name = "observers-network-lb"
internal = false
load_balancer_type = "network"
subnets = module.this_vpc.public_subnets

enable_cross_zone_load_balancing = true
# enable_deletion_protection = true

tags = {
Name = "Observers NLB"
}
}

locals {
tls_cert_arn = var.enable_tls ? aws_acm_certificate_validation.this_acm_cert_validation[0].certificate_arn : ""
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06" # TLS 1.3 (recommended)
}

resource "aws_lb_listener" "rest" {
count = local.enable_tls ? 0 : 1

load_balancer_arn = aws_lb.this_nlb.arn
port = "80"
protocol = "TCP"

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.rest.arn
}
}

resource "aws_lb_listener" "grpc" {
count = local.enable_tls ? 0 : 1

load_balancer_arn = aws_lb.this_nlb.arn
port = "9090"
protocol = "TCP"

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.grpc.arn
}
}

resource "aws_lb_listener" "rpc" {
count = local.enable_tls ? 0 : 1

load_balancer_arn = aws_lb.this_nlb.arn
port = "8080"
protocol = "TCP"

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.rpc.arn
}
}

resource "aws_lb_listener" "tls_rest" {
count = local.enable_tls ? 1 : 0

load_balancer_arn = aws_lb.this_nlb.arn
port = "443"
protocol = "TLS"
certificate_arn = local.tls_cert_arn
ssl_policy = local.ssl_policy

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.rest.arn
}

depends_on = [
aws_acm_certificate_validation.this_acm_cert_validation[0]
]
}

resource "aws_lb_listener" "tls_grpc" {
count = local.enable_tls ? 1 : 0

load_balancer_arn = aws_lb.this_nlb.arn
port = "8443"
protocol = "TLS"
certificate_arn = local.tls_cert_arn
ssl_policy = local.ssl_policy

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.grpc.arn
}

depends_on = [
aws_acm_certificate_validation.this_acm_cert_validation[0]
]
}

resource "aws_lb_listener" "tls_rpc" {
count = local.enable_tls ? 1 : 0

load_balancer_arn = aws_lb.this_nlb.arn
port = "26657"
protocol = "TLS"
certificate_arn = local.tls_cert_arn
ssl_policy = local.ssl_policy

default_action {
type = "forward"
target_group_arn = aws_lb_target_group.rpc.arn
}

depends_on = [
aws_acm_certificate_validation.this_acm_cert_validation[0]
]
}

resource "aws_lb_target_group" "rest" {
name = "observers-rest-target-group"
port = 1317
protocol = "TCP"
vpc_id = module.this_vpc.vpc_id
preserve_client_ip = false
}

resource "aws_lb_target_group" "grpc" {
name = "observers-grpc-target-group"
port = 9090
protocol = "TCP"
vpc_id = module.this_vpc.vpc_id
preserve_client_ip = false
}

resource "aws_lb_target_group" "rpc" {
name = "observers-rpc-target-group"
port = 26657
protocol = "TCP"
vpc_id = module.this_vpc.vpc_id
preserve_client_ip = false
}

resource "aws_lb_target_group_attachment" "rest_targets" {
count = length(aws_instance.this_nodes)

target_group_arn = aws_lb_target_group.rest.arn
target_id = aws_instance.this_nodes[count.index].id
port = 80
}

resource "aws_lb_target_group_attachment" "grpc_targets" {
count = length(aws_instance.this_nodes)

target_group_arn = aws_lb_target_group.grpc.arn
target_id = aws_instance.this_nodes[count.index].id
port = 9090
}

resource "aws_lb_target_group_attachment" "rpc_targets" {
count = length(aws_instance.this_nodes)

target_group_arn = aws_lb_target_group.rpc.arn
target_id = aws_instance.this_nodes[count.index].id
port = 26657
}
3 changes: 3 additions & 0 deletions deployment/terraform/aws/observers/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
enable_tls = var.enable_tls && var.root_domain_name != ""
}
43 changes: 43 additions & 0 deletions deployment/terraform/aws/observers/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]

filter {
name = "name"
values = ["ubuntu-minimal/images/hvm-ssd/ubuntu-focal-20.04-amd64-minimal-*"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}
}

resource "aws_key_pair" "key_pair" {
public_key = file(var.ssh_public_key_path)
}

resource "aws_instance" "this_nodes" {
count = var.nodes_count

ami = data.aws_ami.ubuntu.id
instance_type = "t3.medium"

subnet_id = element(module.this_vpc.public_subnets, count.index % length(module.this_vpc.public_subnets))
vpc_security_group_ids = [
module.this_dev_sg.security_group_id,
module.this_private_sg.security_group_id
]

key_name = aws_key_pair.key_pair.id
monitoring = true

tags = {
Name = "Observer Node [${count.index}]"
}

root_block_device {
encrypted = true
volume_size = 30
}
}
Empty file.
9 changes: 9 additions & 0 deletions deployment/terraform/aws/observers/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.72"
configuration_aliases = [aws, aws.peer]
}
}
}
26 changes: 26 additions & 0 deletions deployment/terraform/aws/observers/route53.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
locals {
enable_routing = var.root_domain_name == "" ? 0 : 1
}

data "aws_route53_zone" "this_zone" {
count = local.enable_routing
name = var.root_domain_name
}

data "aws_region" "current" {}

resource "aws_route53_record" "on" {
count = local.enable_routing

zone_id = data.aws_route53_zone.this_zone[0].zone_id
name = "on.${data.aws_route53_zone.this_zone[0].name}"
type = "CNAME"
ttl = "300"

latency_routing_policy {
region = data.aws_region.current.name
}

set_identifier = "Observers NLB [${var.region_index}]"
records = ["${aws_lb.this_nlb.dns_name}"]
}
Loading