Skip to content

Commit a5b020b

Browse files
authored
Merge pull request #1 from HackCapital/slajax/alb-service
ALB Service
2 parents c5a3e52 + 81f12c8 commit a5b020b

File tree

2 files changed

+412
-0
lines changed

2 files changed

+412
-0
lines changed

alb-service/alb/main.tf

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/**
2+
* The ELB module creates an ELB, security group
3+
* a route53 record and a service healthcheck.
4+
* It is used by the service module.
5+
*/
6+
7+
variable "name" {
8+
description = "ELB name, e.g cdn"
9+
}
10+
11+
variable "subnet_ids" {
12+
description = "Comma separated list of subnet IDs"
13+
}
14+
15+
variable "environment" {
16+
description = "Environment tag, e.g prod"
17+
}
18+
19+
variable "port" {
20+
description = "Instance port"
21+
}
22+
23+
variable "security_groups" {
24+
description = "Comma separated list of security group IDs"
25+
}
26+
27+
variable "healthcheck" {
28+
description = "Healthcheck path"
29+
}
30+
31+
variable "log_bucket" {
32+
description = "S3 bucket name to write ELB logs into"
33+
}
34+
35+
variable "external_dns_name" {
36+
description = "The subdomain under which the ELB is exposed externally, defaults to the task name"
37+
}
38+
39+
variable "internal_dns_name" {
40+
description = "The subdomain under which the ELB is exposed internally, defaults to the task name"
41+
}
42+
43+
variable "external_zone_id" {
44+
description = "The zone ID to create the record in"
45+
}
46+
47+
variable "internal_zone_id" {
48+
description = "The zone ID to create the record in"
49+
}
50+
51+
variable "ssl_certificate_id" {}
52+
53+
variable "vpc_id" {
54+
description = "The id of the VPC."
55+
}
56+
57+
/**
58+
* Resources.
59+
*/
60+
61+
# Create a new load balancer
62+
resource "aws_alb" "main" {
63+
name = "${var.name}"
64+
internal = false
65+
subnets = ["${split(",",var.subnet_ids)}"]
66+
security_groups = ["${split(",",var.security_groups)}"]
67+
68+
access_logs {
69+
bucket = "${var.log_bucket}"
70+
}
71+
}
72+
73+
resource "aws_alb_target_group" "main" {
74+
name = "alb-target-${var.name}"
75+
port = "${var.port}"
76+
protocol = "HTTP"
77+
vpc_id = "${var.vpc_id}"
78+
depends_on = ["aws_alb.main"]
79+
80+
stickiness {
81+
type = "lb_cookie"
82+
enabled = true
83+
}
84+
85+
health_check {
86+
healthy_threshold = 2
87+
unhealthy_threshold = 2
88+
timeout = 5
89+
protocol = "HTTP"
90+
path = "${var.healthcheck}"
91+
interval = 30
92+
}
93+
}
94+
95+
resource "aws_alb_listener" "service_https" {
96+
load_balancer_arn = "${aws_alb.main.arn}"
97+
port = "443"
98+
protocol = "HTTPS"
99+
ssl_policy = "ELBSecurityPolicy-2015-05"
100+
certificate_arn = "${var.ssl_certificate_id}"
101+
102+
default_action {
103+
target_group_arn = "${aws_alb_target_group.main.arn}"
104+
type = "forward"
105+
}
106+
}
107+
108+
resource "aws_alb_listener" "service_http" {
109+
load_balancer_arn = "${aws_alb.main.arn}"
110+
port = "80"
111+
protocol = "HTTP"
112+
113+
default_action {
114+
target_group_arn = "${aws_alb_target_group.main.arn}"
115+
type = "forward"
116+
}
117+
}
118+
119+
resource "aws_route53_record" "external" {
120+
zone_id = "${var.external_zone_id}"
121+
name = "${var.external_dns_name}"
122+
type = "A"
123+
124+
alias {
125+
zone_id = "${aws_alb.main.zone_id}"
126+
name = "${aws_alb.main.dns_name}"
127+
evaluate_target_health = false
128+
}
129+
}
130+
131+
resource "aws_route53_record" "internal" {
132+
zone_id = "${var.internal_zone_id}"
133+
name = "${var.internal_dns_name}"
134+
type = "A"
135+
136+
alias {
137+
zone_id = "${aws_alb.main.zone_id}"
138+
name = "${aws_alb.main.dns_name}"
139+
evaluate_target_health = false
140+
}
141+
}
142+
143+
/**
144+
* Outputs.
145+
*/
146+
147+
// The ELB name.
148+
output "name" {
149+
value = "${aws_alb.main.name}"
150+
}
151+
152+
// The ELB ID.
153+
output "id" {
154+
value = "${aws_alb.main.id}"
155+
}
156+
157+
// The ELB dns_name.
158+
output "dns" {
159+
value = "${aws_alb.main.dns_name}"
160+
}
161+
162+
// FQDN built using the zone domain and name (external)
163+
output "external_fqdn" {
164+
value = "${aws_route53_record.external.fqdn}"
165+
}
166+
167+
// FQDN built using the zone domain and name (internal)
168+
output "internal_fqdn" {
169+
value = "${aws_route53_record.internal.fqdn}"
170+
}
171+
172+
// The zone id of the ELB
173+
output "zone_id" {
174+
value = "${aws_alb.main.zone_id}"
175+
}
176+
177+
output "target_group" {
178+
value = "${aws_alb_target_group.main.arn}"
179+
}

0 commit comments

Comments
 (0)