-
Notifications
You must be signed in to change notification settings - Fork 0
/
alb.tf
113 lines (93 loc) · 2.98 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
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
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 9.9"
for_each = var.create_endpoint ? toset(["this"]) : toset([])
name = local.prefix_short
enable_deletion_protection = !var.force_delete
load_balancer_type = "application"
security_groups = [module.endpoint_security_group.security_group_id]
subnets = var.public ? var.public_subnets : var.private_subnets
vpc_id = var.vpc_id
internal = !var.public
# TODO: Support IPv6 and/or dualstack.
ip_address_type = "ipv4"
listeners = {
http = {
port = 80
protocol = "HTTP"
redirect = {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
https = {
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-TLS-1-2-2017-01"
certificate_arn = aws_acm_certificate.endpoint["this"].arn
forward = {
target_group_key = "endpoint"
}
}
}
target_groups = {
endpoint = {
name = "${local.prefix_short}-app"
protocol = "HTTP"
target_type = "ip"
port = var.container_port
# Theres nothing to attach here in this definition. Instead, ECS will
# attach the IPs of the tasks to this target group.
create_attachment = false
health_check = {
path = "/health"
healthy_threshold = 5
unhealthy_threshold = 2
}
}
}
tags = var.tags
}
resource "aws_acm_certificate" "endpoint" {
for_each = var.create_endpoint ? toset(["this"]) : toset([])
domain_name = local.fqdn
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
tags = var.tags
}
resource "aws_route53_record" "endpoint" {
for_each = var.create_endpoint ? toset(["this"]) : toset([])
name = local.fqdn
type = "A"
zone_id = data.aws_route53_zone.domain["this"].zone_id
alias {
name = module.alb["this"].dns_name
zone_id = module.alb["this"].zone_id
evaluate_target_health = true
}
}
resource "aws_route53_record" "endpoint_validation" {
for_each = var.create_endpoint ? {
for dvo in aws_acm_certificate.endpoint["this"].domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
} : {}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = data.aws_route53_zone.domain["this"].zone_id
}
resource "aws_acm_certificate_validation" "endpoint" {
for_each = var.create_endpoint ? toset(["this"]) : toset([])
certificate_arn = aws_acm_certificate.endpoint["this"].arn
validation_record_fqdns = [
for record in aws_route53_record.endpoint_validation : record.fqdn
]
}