forked from caktus/aws-web-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_balancer.py
152 lines (138 loc) · 4.16 KB
/
load_balancer.py
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
from troposphere import elasticloadbalancing as elb
from troposphere import GetAtt, If, Join, Output, Ref
from . import USE_ECS, USE_GOVCLOUD
from .security_groups import load_balancer_security_group
from .template import template
from .utils import ParameterWithDefaults as Parameter
from .vpc import public_subnet_a, public_subnet_b
# Web worker
if USE_ECS:
web_worker_port = Ref(template.add_parameter(
Parameter(
"WebWorkerPort",
Description="Web worker container exposed port",
Type="Number",
Default="8000",
),
group="Load Balancer",
label="Web Worker Port",
))
else:
# default to port 80 for EC2 and Elastic Beanstalk options
web_worker_port = Ref(template.add_parameter(
Parameter(
"WebWorkerPort",
Description="Default web worker exposed port (non-HTTPS)",
Type="Number",
Default="80",
),
group="Load Balancer",
label="Web Worker Port",
))
web_worker_protocol = Ref(template.add_parameter(
Parameter(
"WebWorkerProtocol",
Description="Web worker instance protocol",
Type="String",
Default="HTTP",
AllowedValues=["HTTP", "HTTPS"],
),
group="Load Balancer",
label="Web Worker Protocol",
))
# Web worker health check
web_worker_health_check_protocol = Ref(template.add_parameter(
Parameter(
"WebWorkerHealthCheckProtocol",
Description="Web worker health check protocol",
Type="String",
Default="TCP",
AllowedValues=["TCP", "HTTP", "HTTPS"],
),
group="Load Balancer",
label="Health Check: Protocol",
))
web_worker_health_check_port = Ref(template.add_parameter(
Parameter(
"WebWorkerHealthCheckPort",
Description="Web worker health check port",
Type="Number",
Default="80",
),
group="Load Balancer",
label="Health Check: Port",
))
web_worker_health_check = Ref(template.add_parameter(
Parameter(
"WebWorkerHealthCheck",
Description="Web worker health check URL path, e.g., \"/health-check\"; "
"required unless WebWorkerHealthCheckProtocol is TCP",
Type="String",
Default="",
),
group="Load Balancer",
label="Health Check: URL",
))
# Web load balancer
listeners = [
elb.Listener(
LoadBalancerPort=80,
InstanceProtocol=web_worker_protocol,
InstancePort=web_worker_port,
Protocol='HTTP',
)
]
if USE_GOVCLOUD:
# configure the default HTTPS listener to pass TCP traffic directly,
# since GovCloud doesn't support the Certificate Manager (this can be
# modified to enable SSL termination at the load balancer via the AWS
# console, if needed)
listeners.append(elb.Listener(
LoadBalancerPort=443,
InstanceProtocol='TCP',
InstancePort=443,
Protocol='TCP',
))
else:
from .certificates import application as application_certificate
from .certificates import cert_condition
listeners.append(If(cert_condition, elb.Listener(
LoadBalancerPort=443,
InstanceProtocol=web_worker_protocol,
InstancePort=web_worker_port,
Protocol='HTTPS',
SSLCertificateId=application_certificate,
), Ref("AWS::NoValue")))
load_balancer = elb.LoadBalancer(
'LoadBalancer',
template=template,
Subnets=[
Ref(public_subnet_a),
Ref(public_subnet_b),
],
SecurityGroups=[Ref(load_balancer_security_group)],
Listeners=listeners,
HealthCheck=elb.HealthCheck(
Target=Join("", [
web_worker_health_check_protocol,
":",
web_worker_health_check_port,
web_worker_health_check,
]),
HealthyThreshold="2",
UnhealthyThreshold="2",
Interval="100",
Timeout="10",
),
CrossZone=True,
)
template.add_output(Output(
"LoadBalancerDNSName",
Description="Loadbalancer DNS",
Value=GetAtt(load_balancer, "DNSName")
))
template.add_output(Output(
"LoadBalancerHostedZoneID",
Description="Loadbalancer hosted zone",
Value=GetAtt(load_balancer, "CanonicalHostedZoneNameID")
))