-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
245 lines (204 loc) · 6.93 KB
/
main.tf
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# --- ALB Security Group ------------------------------------------------------
resource "aws_security_group" "alb_sg" {
name = "${local.snake_case_name}_alb_sg"
description = "${var.name} ALB Security Group"
vpc_id = var.vpc_id
}
resource "aws_security_group_rule" "alb_http_ingress" {
type = "ingress"
description = "Allow HTTP ingress from all"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
security_group_id = aws_security_group.alb_sg.id
}
resource "aws_security_group_rule" "alb_https_ingress" {
type = "ingress"
description = "Allow HTTPS ingress from all"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
security_group_id = aws_security_group.alb_sg.id
}
resource "aws_security_group_rule" "alb_egress" {
type = "egress"
description = "Allow all egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
security_group_id = aws_security_group.alb_sg.id
}
# --- ASG Security Group ------------------------------------------------------
resource "aws_security_group" "asg_sg" {
name = "${local.snake_case_name}_asg_sg"
description = "${var.name} ASG Security Group"
vpc_id = var.vpc_id
}
resource "aws_security_group_rule" "asg_http_ingress" {
type = "ingress"
description = "Allow HTTP ingress from ALB"
from_port = 80
to_port = 80
protocol = "tcp"
source_security_group_id = aws_security_group.alb_sg.id
security_group_id = aws_security_group.asg_sg.id
}
resource "aws_security_group_rule" "asg_ssh_ingress" {
type = "ingress"
description = "Allow SSH ingress from bastion"
from_port = 22
to_port = 22
protocol = "tcp"
source_security_group_id = var.bastion_sg_id
security_group_id = aws_security_group.asg_sg.id
}
resource "aws_security_group_rule" "asg_egress" {
type = "egress"
description = "Allow all egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
security_group_id = aws_security_group.asg_sg.id
}
# --- RDS Security Group ------------------------------------------------------
resource "aws_security_group" "db_sg" {
name = "${local.snake_case_name}_db_sg"
description = "${var.name} database Security Group"
vpc_id = var.vpc_id
}
resource "aws_security_group_rule" "db_ingress" {
type = "ingress"
description = "Allow database ingress from ASG"
from_port = var.rds_port
to_port = var.rds_port
protocol = "tcp"
source_security_group_id = aws_security_group.asg_sg.id
security_group_id = aws_security_group.db_sg.id
}
resource "aws_security_group_rule" "db_egress" {
type = "egress"
description = "Allow all egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
security_group_id = aws_security_group.db_sg.id
}
# --- ASG ---------------------------------------------------------------------
module "asg" {
source = "terraform-aws-modules/autoscaling/aws"
name = "${local.kebab_case_name}-asg"
min_size = var.asg_min_size
max_size = var.asg_max_size
desired_capacity = var.asg_desired_capacity
health_check_type = "EC2"
vpc_zone_identifier = var.private_subnet_ids
image_id = var.ami_id
instance_type = var.asg_instance_type
enable_monitoring = false
launch_template_name = local.kebab_case_name
launch_template_description = "${var.name} launch template"
update_default_version = true
target_group_arns = module.alb.target_group_arns
security_groups = [aws_security_group.asg_sg.id]
iam_instance_profile_name = var.asg_instance_profile_name
user_data = base64encode(templatefile(var.asg_user_data_template, {}))
instance_refresh = {
strategy = "Rolling"
preferences = {
min_healthy_percentage = 0
}
triggers = ["launch_configuration"]
}
tags = {
Type = local.snake_case_name
}
}
# --- ALB ---------------------------------------------------------------------
module "cert" {
source = "terraform-aws-modules/acm/aws"
domain_name = var.fqdn
zone_id = var.route53_zone_id
subject_alternative_names = var.subject_alternative_names
wait_for_validation = true
}
module "alb" {
source = "terraform-aws-modules/alb/aws"
name = "${local.kebab_case_name}-alb"
load_balancer_type = "application"
vpc_id = var.vpc_id
subnets = var.public_subnet_ids
create_security_group = false
security_groups = [aws_security_group.alb_sg.id]
target_groups = [
{
name = local.kebab_case_name
backend_protocol = "HTTP"
backend_port = 80
target_type = "instance"
}
]
http_tcp_listeners = [
{
port = 80
protocol = "HTTP"
action_type = "redirect"
redirect = {
port = 443
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
]
https_listeners = [
{
port = 443
protocol = "HTTPS"
certificate_arn = module.cert.acm_certificate_arn
target_group_index = 0
}
]
}
resource "aws_route53_record" "a" {
for_each = toset(local.domain_aliases)
zone_id = var.route53_zone_id
name = each.key
type = "A"
alias {
name = module.alb.lb_dns_name
zone_id = module.alb.lb_zone_id
evaluate_target_health = false
}
}
# --- RDS ---------------------------------------------------------------------
module "rds" {
source = "terraform-aws-modules/rds/aws"
identifier = local.kebab_case_name
engine = var.rds_engine
engine_version = var.rds_engine_version
family = var.rds_family
major_engine_version = var.rds_major_engine_version
instance_class = var.rds_instance_class
allocated_storage = var.rds_allocated_storage
storage_encrypted = true
create_random_password = false
db_name = var.rds_db_name
username = var.rds_username
password = var.rds_password
port = var.rds_port
maintenance_window = var.rds_maintenance_window
backup_window = var.rds_backup_window
backup_retention_period = var.rds_backup_retention_period
subnet_ids = var.database_subnet_ids
create_db_subnet_group = true
vpc_security_group_ids = [aws_security_group.db_sg.id]
}