This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathalb.tf
160 lines (137 loc) · 4.59 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
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
# #########################################
# ALB API
# #########################################
module "alb_api_sg" {
source = "./modules/security-group"
open_egress = true
name = "${module.labels.id}-alb-api"
environment = var.environment
vpc_id = module.vpc.vpc_id
tags = module.labels.tags
}
resource "aws_security_group_rule" "alb_api_http_ingress" {
description = "Allows connection on port 80 from anywhere"
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = module.alb_api_sg.id
}
resource "aws_lb" "api" {
name = "${module.labels.id}-api"
internal = false
subnets = module.vpc.public_subnets
security_groups = ["${module.alb_api_sg.id}"]
enable_cross_zone_load_balancing = true
enable_http2 = true
ip_address_type = "dualstack"
tags = module.labels.tags
}
resource "aws_lb_target_group" "api" {
name = "${module.labels.id}-api"
port = var.api_listening_port
protocol = var.api_listening_protocol
vpc_id = module.vpc.vpc_id
deregistration_delay = "10"
target_type = "ip"
health_check {
path = var.health_check_path
matcher = var.health_check_matcher
interval = var.health_check_interval
timeout = var.health_check_timeout
healthy_threshold = var.health_check_healthy_threshold
unhealthy_threshold = var.health_check_unhealthy_threshold
}
tags = module.labels.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_lb_listener" "api_http" {
load_balancer_arn = aws_lb.api.id
port = 80
protocol = "HTTP"
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "Forbidden"
status_code = "403"
}
}
}
resource "aws_lb_listener_rule" "header_check" {
listener_arn = aws_lb_listener.api_http.arn
action {
type = "forward"
target_group_arn = aws_lb_target_group.api.arn
}
condition {
http_header {
http_header_name = "X-Routing-Secret"
values = [jsondecode(data.aws_secretsmanager_secret_version.api_gateway_header.secret_string)["header-secret"]]
}
}
}
# #########################################
# ALB Push
# #########################################
module "alb_push_sg" {
source = "./modules/security-group"
open_egress = true
name = "${module.labels.id}-alb-push"
environment = var.environment
vpc_id = module.vpc.vpc_id
tags = module.labels.tags
}
resource "aws_security_group_rule" "alb_push_https_ingress_all" {
description = "Allows connection on port 443 from anywhere"
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.push_allowed_ips
security_group_id = module.alb_push_sg.id
}
resource "aws_lb" "push" {
name = "${module.labels.id}-push"
internal = false
subnets = module.vpc.public_subnets
security_groups = ["${module.alb_push_sg.id}"]
enable_cross_zone_load_balancing = true
enable_http2 = true
ip_address_type = "dualstack"
tags = module.labels.tags
}
resource "aws_lb_target_group" "push" {
name = "${module.labels.id}-push"
port = var.push_listening_port
protocol = var.push_listening_protocol
vpc_id = module.vpc.vpc_id
target_type = "ip"
deregistration_delay = "10"
health_check {
path = var.health_check_path
matcher = var.health_check_matcher
interval = var.health_check_interval
timeout = var.health_check_timeout
healthy_threshold = var.health_check_healthy_threshold
unhealthy_threshold = var.health_check_unhealthy_threshold
}
tags = module.labels.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_lb_listener" "push_https" {
load_balancer_arn = aws_lb.push.id
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-FS-2018-06"
certificate_arn = local.alb_push_certificate_arn
default_action {
target_group_arn = aws_lb_target_group.push.id
type = "forward"
}
}