From 415a8a6da6282a6f8f5402da09ff2ffb568b101a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Fri, 19 Apr 2024 19:15:02 +0200 Subject: [PATCH 1/3] Implement nginx based caching layer --- tf/modules/nginx_reverseproxy/main.tf | 109 ++++++++++++++++++ tf/modules/nginx_reverseproxy/outputs.tf | 6 + .../templates/setup-reverse-proxy.sh | 26 +++++ tf/modules/nginx_reverseproxy/variables.tf | 36 ++++++ tf/modules/oonith_service/main.tf | 63 ++++++++-- 5 files changed, 233 insertions(+), 7 deletions(-) create mode 100644 tf/modules/nginx_reverseproxy/main.tf create mode 100644 tf/modules/nginx_reverseproxy/outputs.tf create mode 100644 tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh create mode 100644 tf/modules/nginx_reverseproxy/variables.tf diff --git a/tf/modules/nginx_reverseproxy/main.tf b/tf/modules/nginx_reverseproxy/main.tf new file mode 100644 index 00000000..82357c4e --- /dev/null +++ b/tf/modules/nginx_reverseproxy/main.tf @@ -0,0 +1,109 @@ +data "aws_ssm_parameter" "ubuntu_22_ami" { + name = "/aws/service/canonical/ubuntu/server/22.04/stable/current/amd64/hvm/ebs-gp2/ami-id" +} + +# Important note about security groups: +# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group#recreating-a-security-group +resource "aws_security_group" "nginx" { + description = "security group for nginx" + + vpc_id = var.vpc_id + + ingress { + protocol = "tcp" + from_port = 80 + to_port = 80 + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + protocol = "tcp" + from_port = 22 + to_port = 22 + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + + cidr_blocks = [ + "0.0.0.0/0", + ] + } + + tags = var.tags +} + +resource "aws_launch_template" "nginx" { + name_prefix = "${var.name}-nginx-tmpl-" + image_id = data.aws_ssm_parameter.ubuntu_22_ami.value + instance_type = var.instance_type + key_name = var.key_name + + user_data = base64encode(templatefile("${path.module}/templates/setup-reverse-proxy.sh", { + proxy_pass_url = var.proxy_pass_url, + extra_config = var.nginx_extra_config, + })) + + lifecycle { + create_before_destroy = true + } + + network_interfaces { + delete_on_termination = true + associate_public_ip_address = true + security_groups = [ + aws_security_group.nginx.id, + ] + } + + tag_specifications { + resource_type = "instance" + tags = var.tags + } +} + +resource "aws_autoscaling_group" "nginx" { + launch_template { + id = aws_launch_template.nginx.id + version = "$Latest" + } + + lifecycle { + 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 + } + } +} + +resource "aws_alb_target_group" "nginx" { + name = var.name + port = 80 + protocol = "HTTP" + vpc_id = var.vpc_id + + lifecycle { + create_before_destroy = true + } + + tags = var.tags +} + +resource "aws_autoscaling_attachment" "nginx" { + autoscaling_group_name = aws_autoscaling_group.nginx.id + lb_target_group_arn = aws_alb_target_group.nginx.arn +} diff --git a/tf/modules/nginx_reverseproxy/outputs.tf b/tf/modules/nginx_reverseproxy/outputs.tf new file mode 100644 index 00000000..aac0d3e1 --- /dev/null +++ b/tf/modules/nginx_reverseproxy/outputs.tf @@ -0,0 +1,6 @@ +output "autoscaling_group_id" { + value = aws_autoscaling_group.nginx.id +} +output "alb_target_group_id" { + value = aws_alb_target_group.nginx.id +} diff --git a/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh b/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh new file mode 100644 index 00000000..2a762a40 --- /dev/null +++ b/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh @@ -0,0 +1,26 @@ +#!/bin/bash +set -e + +sudo apt update +sudo apt install -y nginx + +tmpfile=$(mktemp /tmp/nginx-config.XXXXXX) +cat > $tmpfile < Date: Fri, 19 Apr 2024 20:08:36 +0200 Subject: [PATCH 2/3] Set caching config --- tf/modules/nginx_reverseproxy/main.tf | 5 +- .../templates/setup-reverse-proxy.sh | 46 ++++++++++++++++++- tf/modules/nginx_reverseproxy/variables.tf | 6 ++- tf/modules/oonith_service/main.tf | 11 +++-- 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/tf/modules/nginx_reverseproxy/main.tf b/tf/modules/nginx_reverseproxy/main.tf index 82357c4e..2dcdfeeb 100644 --- a/tf/modules/nginx_reverseproxy/main.tf +++ b/tf/modules/nginx_reverseproxy/main.tf @@ -43,8 +43,9 @@ resource "aws_launch_template" "nginx" { key_name = var.key_name user_data = base64encode(templatefile("${path.module}/templates/setup-reverse-proxy.sh", { - proxy_pass_url = var.proxy_pass_url, - extra_config = var.nginx_extra_config, + proxy_pass_url = var.proxy_pass_url, + extra_path_config = var.nginx_extra_path_config, + extra_nginx_config = var.nginx_extra_nginx_config, })) lifecycle { diff --git a/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh b/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh index 2a762a40..448a3ef6 100644 --- a/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh +++ b/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh @@ -16,11 +16,55 @@ server { proxy_http_version 1.1; proxy_set_header Host \$host; - ${extra_config} + ${extra_path_config} } } EOF sudo mv $tmpfile /etc/nginx/sites-available/default +tmpfile=$(mktemp /tmp/nginx-config.XXXXXX) +cat > $tmpfile <\\d+\\.\\d+\.\\d+)\. \$ip.0; + ~(?P[^:]+:[^:]+): \$ip::; + default 0.0.0.0; + } + + # log anonymized ipaddr and caching status + log_format ooni_nginx_fmt '\$remote_addr_anon \$upstream_cache_status [\$time_local] ' + '"\$request" \$status \$body_bytes_sent "\$http_referer" "\$http_user_agent"'; + + ${extra_nginx_config} + + access_log syslog:server=unix:/dev/log ooni_nginx_fmt; + error_log syslog:server=unix:/dev/log; + + gzip on; + + include /etc/nginx/conf.d/*.conf; + include /etc/nginx/sites-enabled/*; +} +EOF +sudo mv $tmpfile /etc/nginx/sites-available/default + +sudo mkdir -p /var/cache/nginx +sudo chown -R www-data /var/cache/nginx + sudo nginx -t sudo systemctl reload nginx diff --git a/tf/modules/nginx_reverseproxy/variables.tf b/tf/modules/nginx_reverseproxy/variables.tf index 5f2cc653..8fbf6f62 100644 --- a/tf/modules/nginx_reverseproxy/variables.tf +++ b/tf/modules/nginx_reverseproxy/variables.tf @@ -30,7 +30,11 @@ variable "proxy_pass_url" { description = "URL to pass to the proxy_pass directive" } -variable "nginx_extra_config" { +variable "nginx_extra_nginx_config" { + description = "extra configuration to pass to nginx" + default = "" +} +variable "nginx_extra_path_config" { description = "extra configuration to pass to nginx" default = "" } diff --git a/tf/modules/oonith_service/main.tf b/tf/modules/oonith_service/main.tf index f7b9512f..497e1ce6 100644 --- a/tf/modules/oonith_service/main.tf +++ b/tf/modules/oonith_service/main.tf @@ -218,9 +218,9 @@ module "oonith_nginx_cache" { instance_type = "t2.micro" tags = var.tags - name = "oonith-nginx-cache" - proxy_pass_url = "http://${aws_alb.oonith_service.dns_name}/" - nginx_extra_config = < Date: Fri, 19 Apr 2024 20:21:56 +0200 Subject: [PATCH 3/3] Fix template move --- tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh b/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh index 448a3ef6..18e10d15 100644 --- a/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh +++ b/tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh @@ -61,7 +61,7 @@ http { include /etc/nginx/sites-enabled/*; } EOF -sudo mv $tmpfile /etc/nginx/sites-available/default +sudo mv $tmpfile /etc/nginx/nginx.conf sudo mkdir -p /var/cache/nginx sudo chown -R www-data /var/cache/nginx