-
Notifications
You must be signed in to change notification settings - Fork 41
/
alb.tf
79 lines (65 loc) · 1.68 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
locals {
target_groups = ["primary", "secondary"]
hosts_name = ["*.yourdomain.com"] #example : fill your information
}
resource "aws_security_group" "alb" {
name = "${var.service_name}-allow-http"
vpc_id = "${aws_vpc.this.id}"
ingress {
from_port = 80
protocol = "tcp"
to_port = 80
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
protocol = "-1"
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.service_name}-allow-http"
}
}
resource "aws_lb" "this" {
name = "${var.service_name}-service-alb"
internal = false
load_balancer_type = "application"
security_groups = ["${aws_security_group.alb.id}"]
subnets = "${aws_subnet.public.*.id}"
tags = {
Name = "${var.service_name}-service-alb"
}
}
resource "aws_lb_target_group" "this" {
count = "${length(local.target_groups)}"
name = "${var.service_name}-tg-${element(local.target_groups, count.index)}"
port = 80
protocol = "HTTP"
vpc_id = "${aws_vpc.this.id}"
target_type = "instance"
health_check {
path = "/"
}
}
resource "aws_lb_listener" "this" {
load_balancer_arn = "${aws_lb.this.arn}"
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = "${aws_lb_target_group.this.0.arn}"
}
}
resource "aws_lb_listener_rule" "this" {
count = 2
listener_arn = "${aws_lb_listener.this.arn}"
action {
type = "forward"
target_group_arn = "${aws_lb_target_group.this.*.arn[count.index]}"
}
condition {
field = "host-header"
values = "${local.hosts_name}"
}
}