-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.tf
More file actions
110 lines (89 loc) · 2.32 KB
/
main.tf
File metadata and controls
110 lines (89 loc) · 2.32 KB
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
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_path_config = var.nginx_extra_path_config,
extra_nginx_config = var.nginx_extra_nginx_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
}