-
Notifications
You must be signed in to change notification settings - Fork 10
/
loadbalancer.cfndsl.rb
216 lines (178 loc) · 7.98 KB
/
loadbalancer.cfndsl.rb
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
CloudFormation do
loadbalancer_scheme = external_parameters[:loadbalancer_scheme]
maximum_availability_zones = external_parameters[:maximum_availability_zones]
loadbalancer_type = external_parameters[:loadbalancer_type]
static_ips = external_parameters[:static_ips]
private = false
if loadbalancer_scheme == 'internal'
private = true
end
az_conditions_resources('SubnetPublic', maximum_availability_zones) unless private
az_conditions_resources('SubnetCompute', maximum_availability_zones) if private
nlb_eip_conditions(maximum_availability_zones) if (loadbalancer_type == 'network') && !(private) && (static_ips)
securityGroups = external_parameters[:securityGroups]
ip_blocks = external_parameters[:ip_blocks]
EC2_SecurityGroup('SecurityGroupLoadBalancer') do
GroupDescription FnJoin(' ', [Ref('EnvironmentName'), external_parameters[:component_name]])
VpcId Ref('VPCId')
SecurityGroupIngress sg_create_rules(securityGroups['loadbalancer'], ip_blocks)
end
atributes = []
loadbalancer_attributes = external_parameters.fetch(:loadbalancer_attributes, {})
loadbalancer_attributes.each do |key, value|
atributes << { Key: key, Value: value } unless value.nil?
end
tags = []
tags << { Key: "Environment", Value: Ref("EnvironmentName") }
tags << { Key: "EnvironmentType", Value: Ref("EnvironmentType") }
loadbalancer_tags = external_parameters[:loadbalancer_tags]
loadbalancer_tags.each do |key, value|
tags << { Key: key, Value: value }
end
ElasticLoadBalancingV2_LoadBalancer('LoadBalancer') do
if private
Subnets az_conditional_resources('SubnetCompute', maximum_availability_zones)
Scheme 'internal'
elsif (loadbalancer_type == 'network') && !(private) && (static_ips)
SubnetMappings nlb_subnet_mappings('SubnetPublic', maximum_availability_zones)
else
Subnets az_conditional_resources('SubnetPublic', maximum_availability_zones)
end
if loadbalancer_type == 'network'
Type loadbalancer_type
else
SecurityGroups [Ref('SecurityGroupLoadBalancer')]
end
Tags tags if tags.any?
LoadBalancerAttributes atributes if atributes.any?
end
targetgroups = external_parameters[:targetgroups]
targetgroups.each do |tg_name, tg|
atributes = []
tg['atributes'].each do |key, value|
atributes << { Key: key, Value: value }
end if tg.has_key?('atributes')
tags = []
tags << { Key: "Environment", Value: Ref("EnvironmentName") }
tags << { Key: "EnvironmentType", Value: Ref("EnvironmentType") }
tg['tags'].each do |key, value|
tags << { Key: key, Value: value }
end if tg.has_key?('tags')
ElasticLoadBalancingV2_TargetGroup("#{tg_name}TargetGroup") do
## Required
Port tg['port']
Protocol tg['protocol'].upcase
VpcId Ref('VPCId')
## Optional
if tg.has_key?('healthcheck')
HealthCheckPort tg['healthcheck']['port'] if tg['healthcheck'].has_key?('port')
HealthCheckProtocol tg['healthcheck']['protocol'] if tg['healthcheck'].has_key?('protocol')
HealthCheckIntervalSeconds tg['healthcheck']['interval'] if tg['healthcheck'].has_key?('interval')
HealthCheckTimeoutSeconds tg['healthcheck']['timeout'] if tg['healthcheck'].has_key?('timeout')
HealthyThresholdCount tg['healthcheck']['heathy_count'] if tg['healthcheck'].has_key?('heathy_count')
UnhealthyThresholdCount tg['healthcheck']['unheathy_count'] if tg['healthcheck'].has_key?('unheathy_count')
HealthCheckPath tg['healthcheck']['path'] if tg['healthcheck'].has_key?('path')
Matcher ({ HttpCode: tg['healthcheck']['code'] }) if tg['healthcheck'].has_key?('code')
end
TargetType tg['type'] if tg.has_key?('type')
TargetGroupAttributes atributes if atributes.any?
Tags tags if tags.any?
if tg.has_key?('type') and tg['type'] == 'ip' and tg.has_key? 'target_ips'
Targets (tg['target_ips'].map {|ip| { 'Id' => ip['ip'], 'Port' => ip['port'] }})
end
end
Output("#{tg_name}TargetGroup") {
Value(Ref("#{tg_name}TargetGroup"))
Export FnSub("${EnvironmentName}-#{external_parameters[:component_name]}-#{tg_name}TargetGroup")
}
end
listeners = external_parameters.fetch(:listeners, {})
listeners.each do |listener_name, listener|
next if listener.nil?
ElasticLoadBalancingV2_Listener("#{listener_name}Listener") do
Protocol listener['protocol'].upcase
Certificates [{ CertificateArn: Ref('SslCertId') }] if listener['protocol'] == 'https'
SslPolicy listener['ssl_policy'] if listener.has_key?('ssl_policy')
Port listener['port']
DefaultActions ([
TargetGroupArn: Ref("#{listener['default_targetgroup']}TargetGroup"),
Type: "forward"
])
LoadBalancerArn Ref('LoadBalancer')
end
if (listener.has_key?('certificates')) && (listener['protocol'] == 'https')
ElasticLoadBalancingV2_ListenerCertificate("#{listener_name}ListenerCertificate") {
Certificates listener['certificates'].map { |cert| { CertificateArn: Ref("#{cert}CertificateArn") } }
ListenerArn Ref("#{listener_name}Listener")
}
end
listener['rules'].each do |rule|
listener_conditions = []
actions = []
if rule.key?("path")
listener_conditions << { Field: "path-pattern", Values: [ rule["path"] ] }
end
if rule.key?("host")
hosts = []
if rule["host"].kind_of?(String) && !rule["host"].include?('.')
hosts << FnJoin("", [ rule["host"], ".", Ref("EnvironmentName"), ".", Ref('DnsDomain') ])
else
hosts << rule["host"]
end
listener_conditions << { Field: "host-header", Values: hosts }
end
if rule.key?("targetgroup")
actions << { Type: "forward", TargetGroupArn: Ref("#{rule['targetgroup']}TargetGroup") }
end
if rule.key?("redirect")
actions << { Type: "redirect", RedirectConfig: rule['redirect'] }
end
ElasticLoadBalancingV2_ListenerRule("#{listener_name}Rule#{rule['priority']}") do
Actions actions
Conditions listener_conditions
ListenerArn Ref("#{listener_name}Listener")
Priority rule['priority'].to_i
end
end if listener.has_key?('rules')
Output("#{listener_name}Listener") {
Value(Ref("#{listener_name}Listener"))
Export FnSub("${EnvironmentName}-#{external_parameters[:component_name]}-#{listener_name}Listener")
}
end
records = external_parameters.fetch(:records, [])
records.each do |record|
Route53_RecordSet("#{record.gsub('*','Wildcard').gsub('.','Dot')}LoadBalancerRecord") do
HostedZoneName FnJoin("", [ Ref("EnvironmentName"), ".", Ref('DnsDomain'), "."])
if record == 'apex' || record == ''
Name FnJoin("", [ Ref("EnvironmentName"), ".", Ref('DnsDomain'), "."])
else
Name FnJoin("", [ "#{record}.", Ref("EnvironmentName"), ".", Ref('DnsDomain'), "."])
end
Type 'A'
AliasTarget ({
DNSName: FnGetAtt("LoadBalancer", "DNSName"),
HostedZoneId: FnGetAtt("LoadBalancer", "CanonicalHostedZoneID")
})
end
end
Output("LoadBalancer") {
Value(Ref("LoadBalancer"))
Export FnSub("${EnvironmentName}-#{external_parameters[:component_name]}-LoadBalancer")
}
Output("SecurityGroupLoadBalancer") {
Value(Ref("SecurityGroupLoadBalancer"))
Export FnSub("${EnvironmentName}-#{external_parameters[:component_name]}-SecurityGroupLoadBalancer")
}
Output("LoadBalancerDNSName") {
Value(FnGetAtt("LoadBalancer", "DNSName"))
Export FnSub("${EnvironmentName}-#{external_parameters[:component_name]}-DNSName")
}
Output("LoadBalancerCanonicalHostedZoneID") {
Value(FnGetAtt("LoadBalancer", "CanonicalHostedZoneID"))
Export FnSub("${EnvironmentName}-#{external_parameters[:component_name]}-CanonicalHostedZoneID")
}
Output("LoadBalancerFullName") {
Value(FnGetAtt("LoadBalancer", "LoadBalancerFullName"))
Export FnSub("${EnvironmentName}-#{external_parameters[:component_name]}-LoadBalancerFullName")
}
end