-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
233 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
26 changes: 26 additions & 0 deletions
26
tf/modules/nginx_reverseproxy/templates/setup-reverse-proxy.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
sudo apt update | ||
sudo apt install -y nginx | ||
|
||
tmpfile=$(mktemp /tmp/nginx-config.XXXXXX) | ||
cat > $tmpfile <<EOF | ||
server { | ||
listen 80; | ||
server_name _; | ||
location / { | ||
proxy_pass ${proxy_pass_url}; | ||
proxy_http_version 1.1; | ||
proxy_set_header Host \$host; | ||
${extra_config} | ||
} | ||
} | ||
EOF | ||
sudo mv $tmpfile /etc/nginx/sites-available/default | ||
|
||
sudo nginx -t | ||
sudo systemctl reload nginx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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 "tags" { | ||
description = "tags to apply to the resources" | ||
default = {} | ||
type = map(string) | ||
} | ||
|
||
variable "key_name" { | ||
description = "Name of AWS key pair" | ||
} | ||
|
||
variable "name" { | ||
description = "Name of the resources" | ||
default = "ooni-backendproxy" | ||
} | ||
|
||
variable "instance_type" { | ||
default = "t2.micro" | ||
} | ||
|
||
|
||
variable "proxy_pass_url" { | ||
description = "URL to pass to the proxy_pass directive" | ||
} | ||
|
||
variable "nginx_extra_config" { | ||
description = "extra configuration to pass to nginx" | ||
default = "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters