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

feat: add clickhouse proxy instance #100

Merged
merged 5 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 7 additions & 1 deletion tf/environments/dev/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,19 @@ moved {
module "ooni_backendproxy" {
source = "../../modules/ooni_backendproxy"

stage = local.environment

vpc_id = module.network.vpc_id
subnet_ids = module.network.vpc_subnet_public[*].id
subnet_id = module.network.vpc_subnet_public[0].id
private_subnet_cidr = module.network.vpc_subnet_private[*].cidr_block
dns_zone_ooni_io = local.dns_zone_ooni_io

key_name = module.adm_iam_roles.oonidevops_key_name
instance_type = "t2.micro"

backend_url = "https://backend-hel.ooni.org/"
clickhouse_url = "backend-fsn.ooni.org"
clickhouse_port = "9000"

tags = merge(
local.tags,
Expand Down
45 changes: 27 additions & 18 deletions tf/modules/ooni_backendproxy/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ resource "aws_security_group" "nginx_sg" {
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
protocol = "tcp"
from_port = 9000
to_port = 9000
cidr_blocks = var.private_subnet_cidr
}

ingress {
protocol = "tcp"
from_port = 22
Expand Down Expand Up @@ -55,7 +62,9 @@ resource "aws_launch_template" "ooni_backendproxy" {
key_name = var.key_name

user_data = base64encode(templatefile("${path.module}/templates/setup-backend-proxy.sh", {
backend_url = var.backend_url
backend_url = var.backend_url,
clickhouse_url = var.clickhouse_url,
clickhouse_port = var.clickhouse_port
}))

lifecycle {
Expand All @@ -65,6 +74,7 @@ resource "aws_launch_template" "ooni_backendproxy" {
network_interfaces {
delete_on_termination = true
associate_public_ip_address = true
subnet_id = var.subnet_id
security_groups = [
aws_security_group.nginx_sg.id,
]
Expand All @@ -76,7 +86,7 @@ resource "aws_launch_template" "ooni_backendproxy" {
}
}

resource "aws_autoscaling_group" "oonibackend_proxy" {
resource "aws_instance" "oonibackend_proxy" {
launch_template {
id = aws_launch_template.ooni_backendproxy.id
version = "$Latest"
Expand All @@ -86,19 +96,7 @@ resource "aws_autoscaling_group" "oonibackend_proxy" {
create_before_destroy = true
}

name_prefix = "${var.name}-asg-"

min_size = 1
max_size = 2
desired_capacity = 1
vpc_zone_identifier = var.subnet_ids

instance_refresh {
strategy = "Rolling"
preferences {
min_healthy_percentage = 50
}
}
tags = var.tags
}

resource "aws_alb_target_group" "oonibackend_proxy" {
Expand All @@ -114,7 +112,18 @@ resource "aws_alb_target_group" "oonibackend_proxy" {
tags = var.tags
}

resource "aws_autoscaling_attachment" "oonibackend_proxy" {
autoscaling_group_name = aws_autoscaling_group.oonibackend_proxy.id
lb_target_group_arn = aws_alb_target_group.oonibackend_proxy.arn
resource "aws_lb_target_group_attachment" "oonibackend_proxy" {
target_id = aws_instance.oonibackend_proxy.id
target_group_arn = aws_alb_target_group.oonibackend_proxy.arn
}

resource "aws_route53_record" "clickhouse_proxy_alias" {
zone_id = var.dns_zone_ooni_io
name = "clickhouse.${var.stage}.ooni.io"
type = "CNAME"
ttl = 300

records = [
aws_instance.oonibackend_proxy.public_dns
]
}
5 changes: 3 additions & 2 deletions tf/modules/ooni_backendproxy/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
output "autoscaling_group_id" {
value = aws_autoscaling_group.oonibackend_proxy.id
output "aws_instance_id" {
value = aws_instance.oonibackend_proxy.id
}

output "alb_target_group_id" {
value = aws_alb_target_group.oonibackend_proxy.id
}
19 changes: 19 additions & 0 deletions tf/modules/ooni_backendproxy/templates/setup-backend-proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,24 @@ server {
EOF
sudo mv $tmpfile /etc/nginx/sites-available/default


tmpfile_stream=$(mktemp /tmp/nginx-stream-config.XXXXXX)
cat > $tmpfile_stream <<EOF
stream {
upstream clickhouse_backend {
server ${clickhouse_url}:${clickhouse_port};
}

server {
listen 9000;

proxy_pass clickhouse_backend;
}

error_log /var/log/nginx/error.log;
}
EOF
sudo mv $tmpfile_stream /etc/nginx/modules-enabled/stream.conf

sudo nginx -t
sudo systemctl reload nginx
27 changes: 24 additions & 3 deletions tf/modules/ooni_backendproxy/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ variable "vpc_id" {
description = "the id of the VPC to deploy the instance into"
}

variable "subnet_ids" {
description = "the ids of the subnet of the subnets to deploy the instance into"
variable "subnet_id" {
description = "the ids of the subnet to deploy the instance into"
}

variable "private_subnet_cidr" {
description = "the cidr block of the private subnet to allow traffic from for the clickhouse proxy"
}

variable "tags" {
Expand All @@ -28,4 +32,21 @@ variable "instance_type" {
variable "backend_url" {
type = string
default = "https://backend-fsn.ooni.org/"
}
}

variable "stage" {
default = "one of dev, stage, test, prod"
}

variable "dns_zone_ooni_io" {
description = "id of the DNS zone for ooni_io"
}

variable "clickhouse_url" {
description = "clickhouse url to proxy requests to"
default = "backend-fsn.ooni.org"
}

variable "clickhouse_port" {
description = "clickhouse port for the backend"
}
Loading