-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalb.tf
63 lines (53 loc) · 1.59 KB
/
alb.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
resource "aws_lb" "alb" {
count = var.lb_type == "ALB" ? 1 : 0
name = "alb-${var.name}"
internal = false
load_balancer_type = "application"
subnets = var.public_subnet_ids
security_groups = [
aws_security_group.alb[0].id,
]
idle_timeout = 400
tags = {
Name = "${var.name}"
}
}
resource "aws_lb_target_group" "alb_tg" {
count = var.lb_type == "ALB" ? 1 : 0
name = "tg-${var.name}"
port = 80
protocol = "HTTP"
vpc_id = var.vpc_id
}
resource "aws_lb_listener" "alb_http_listener" {
count = var.lb_type == "ALB" ? 1 : 0
load_balancer_arn = aws_lb.alb[0].arn
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
resource "aws_lb_listener" "alb_https_listener" {
count = var.lb_type == "ALB" ? 1 : 0
load_balancer_arn = aws_lb.alb[0].arn
port = "443"
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = var.certificate_arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alb_tg[0].arn
}
}
resource "aws_autoscaling_attachment" "alb_asg_attachment" {
count = "${var.lb_type == "ALB" ? 1 : 0}" * var.instance_count
autoscaling_group_name = aws_autoscaling_group.asg[count.index].name
alb_target_group_arn = aws_lb_target_group.alb_tg[0].arn
depends_on = [aws_lb_target_group.alb_tg]
}