-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
186 lines (150 loc) · 3.72 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
/**
* The ALB module creates an ALB, target_groups, listener rules,
* and the route53 records needed.
*/
variable "name" {}
variable "subnet_ids" {
type = "list"
description = "List of subnets where LB live, tipically one per AZ"
}
variable "environment" {
description = "Environment tag, e.g prod"
}
variable "security_groups" {
type = "list"
description = "List of security group to associate with the LB"
}
variable "backend_proto" {
default = "HTTP"
}
variable "healthcheck" {
default = {
healthy_threshold = 2
unhealthy_threshold = 5
timeout = 5
path = "/"
interval = 30
}
}
variable "hosts" {
type = "list"
description = "List of ALB's Content-Based Routing host to match"
default = []
}
variable "services" {
type = "list"
description = "List of services where traffic of the matching hosts will be forwarded"
default = []
}
variable "ports" {
type = "list"
description = "List of backend_ports associated with each service"
default = []
}
variable "zone_id" {
description = "Route53 zone ID to use for dns_name"
}
variable "log_bucket" {
description = "S3 bucket name to write ALB logs into"
}
variable "vpc_id" {}
variable "ssl_arn" {}
variable "ssl_policy" {}
/**
* Resources.
*/
resource "aws_alb" "main" {
name = "${var.name}-${var.environment}"
internal = "false"
subnets = [ "${var.subnet_ids}" ]
security_groups = [ "${var.security_groups}" ]
access_logs {
bucket = "${var.log_bucket}"
}
tags {
Name = "${var.name}"
Environment = "${var.environment}"
}
}
resource "aws_alb_target_group" "main" {
count = "${length(var.services)}"
name = "${var.services[count.index]}-${var.environment}"
port = "${var.ports[count.index]}"
protocol = "${var.backend_proto}"
vpc_id = "${var.vpc_id}"
health_check = [ "${var.healthcheck}" ]
}
resource "aws_alb_listener" "main" {
load_balancer_arn = "${aws_alb.main.id}"
port = "443"
protocol = "HTTPS"
certificate_arn = "${var.ssl_arn}"
ssl_policy = "${var.ssl_policy}"
default_action {
target_group_arn = "${aws_alb_target_group.main.0.arn}"
type = "forward"
}
}
resource "aws_alb_listener_rule" "main" {
count = "${length(var.hosts)}"
listener_arn = "${aws_alb_listener.main.arn}"
priority = "${count.index + 100 }"
action {
type = "forward"
target_group_arn = "${element(aws_alb_target_group.main.*.arn, count.index)}"
}
condition {
field = "host-header"
values = ["${var.hosts[count.index]}.*"]
}
lifecycle {
ignore_changes = ["priority"]
}
}
# Add ALB record on DNS
resource "aws_route53_record" "main" {
count = "${length(var.hosts)}"
zone_id = "${var.zone_id}"
name = "${var.hosts[count.index]}"
type = "A"
alias {
name = "${aws_alb.main.dns_name}"
zone_id = "${aws_alb.main.zone_id}"
evaluate_target_health = false
}
}
/**
* Outputs.
*/
// The ALB name.
output "name" {
value = "${aws_alb.main.name}"
}
// The ALB ID.
output "id" {
value = "${aws_alb.main.id}"
}
// The ALB ARN
output "arn" {
value = "${aws_alb.main.arn}"
}
// The ALB listener ARN
output "listener_arn" {
value = "${aws_alb_listener.main.arn}"
}
// The ALB dns_name.
output "dns" {
value = "${aws_alb.main.dns_name}"
}
// FQDN built using the zone domain and name
output "fqdn" {
value = "${aws_route53_record.main.fqdn}"
}
// The zone id of the ALB
output "zone_id" {
value = "${aws_alb.main.zone_id}"
}
// The target-group id of the ALB
output "target_groups" {
value = [ "${aws_alb_target_group.main.*.id}" ]
}