forked from reireias/rails-on-ecs-terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalb.tf
70 lines (58 loc) · 1.55 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
resource "aws_lb" "app" {
name = "${local.name}-app"
# NOTE: allow from internet
# tfsec:ignore:AWS005
internal = false
subnets = values(aws_subnet.public)[*].id
security_groups = [aws_security_group.alb.id]
enable_deletion_protection = false
access_logs {
bucket = aws_s3_bucket.logs.bucket
enabled = true
}
}
resource "aws_lb_listener" "app" {
load_balancer_arn = aws_lb.app.arn
port = "443"
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-TLS-1-2-2017-01"
certificate_arn = aws_acm_certificate.main.arn
# NOTE: Return 404 to all not CloudFront requests.
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
status_code = "404"
}
}
}
resource "aws_lb_listener_rule" "app_from_cloudfront" {
listener_arn = aws_lb_listener.app.arn
priority = 100
action {
type = "forward"
target_group_arn = aws_lb_target_group.app["blue"].arn
}
condition {
http_header {
http_header_name = "x-pre-shared-key"
values = [data.aws_kms_secrets.secrets.plaintext["cloudfront_shared_key"]]
}
}
# NOTE: Ignore target group switch
lifecycle {
ignore_changes = [action]
}
}
resource "aws_lb_target_group" "app" {
for_each = toset(["blue", "green"])
name = "${local.name}-app-${each.key}"
port = 3000
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_vpc.main.id
health_check {
path = "/health_checks"
matcher = "200-299"
}
}