Class: Terrafying::Components::LoadBalancer

Inherits:
Terrafying::Context
  • Object
show all
Includes:
Usable
Defined in:
lib/terrafying/components/loadbalancer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Usable

#egress_security_group, #ingress_security_group, #path_mtu_setup!, #pingable_by, #pingable_by_cidr, #used_by, #used_by_cidr

Constructor Details

#initializeLoadBalancer

Returns a new instance of LoadBalancer.



26
27
28
# File 'lib/terrafying/components/loadbalancer.rb', line 26

def initialize
  super
end

Instance Attribute Details

#alias_configObject (readonly)

Returns the value of attribute alias_config.



12
13
14
# File 'lib/terrafying/components/loadbalancer.rb', line 12

def alias_config
  @alias_config
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/terrafying/components/loadbalancer.rb', line 12

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/terrafying/components/loadbalancer.rb', line 12

def name
  @name
end

#portsObject (readonly)

Returns the value of attribute ports.



12
13
14
# File 'lib/terrafying/components/loadbalancer.rb', line 12

def ports
  @ports
end

#security_groupObject (readonly)

Returns the value of attribute security_group.



12
13
14
# File 'lib/terrafying/components/loadbalancer.rb', line 12

def security_group
  @security_group
end

#targetsObject (readonly)

Returns the value of attribute targets.



12
13
14
# File 'lib/terrafying/components/loadbalancer.rb', line 12

def targets
  @targets
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/terrafying/components/loadbalancer.rb', line 12

def type
  @type
end

Class Method Details

.create_in(vpc, name, options = {}) ⇒ Object



18
19
20
# File 'lib/terrafying/components/loadbalancer.rb', line 18

def self.create_in(vpc, name, options = {})
  LoadBalancer.new.create_in vpc, name, options
end

.find_in(vpc, name) ⇒ Object



22
23
24
# File 'lib/terrafying/components/loadbalancer.rb', line 22

def self.find_in(vpc, name)
  LoadBalancer.new.find_in vpc, name
end

Instance Method Details

#alb_cert(cert_arn, port_ident) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/terrafying/components/loadbalancer.rb', line 185

def alb_cert(cert_arn, port_ident)
  cert_ident = "#{port_ident}-#{Digest::SHA2.hexdigest(cert_arn)[0..8]}"

  resource :aws_lb_listener_certificate, cert_ident,
           listener_arn: "${aws_lb_listener.#{port_ident}.arn}",
           certificate_arn: cert_arn
end

#alb_certs(port, port_ident) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/terrafying/components/loadbalancer.rb', line 172

def alb_certs(port, port_ident)
  return {} unless port.key? :ssl_certificate

  certs = Array(port[:ssl_certificate])
  default_cert = certs.shift
  certs.map { |cert| alb_cert(cert, port_ident) }

  {
    ssl_policy: 'ELBSecurityPolicy-TLS-1-2-2017-01',
    certificate_arn: default_cert
  }
end

#application?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/terrafying/components/loadbalancer.rb', line 193

def application?
  @type == 'application'
end

#attach(set) ⇒ Object



205
206
207
208
209
210
# File 'lib/terrafying/components/loadbalancer.rb', line 205

def attach(set)
  raise "Dont' know how to attach object to LB" unless set.respond_to?(:attach_load_balancer)

  set.attach_load_balancer(self)
  @security_group = set.ingress_security_group if network?
end

#autoscale(set, target_value:, disable_scale_in:) ⇒ Object



212
213
214
215
216
# File 'lib/terrafying/components/loadbalancer.rb', line 212

def autoscale(set, target_value:, disable_scale_in:)
  raise "Dont' know how to attach object to LB" unless set.respond_to?(:autoscale_on_load_balancer)

  set.autoscale_on_load_balancer(self, target_value: target_value, disable_scale_in: disable_scale_in)
end

#create_in(vpc, name, options = {}) ⇒ Object



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
# File 'lib/terrafying/components/loadbalancer.rb', line 68

def create_in(vpc, name, options = {})
  options = {
    ports: [],
    public: false,
    subnets: vpc.subnets.fetch(:private, []),
    cross_zone_load_balancing: false,
    hex_ident: false,
    idle_timeout: nil,
    tags: {},
    security_groups: []
  }.merge(options)

  @tags = {
    Name: name
  }.merge(options[:tags])

  @hex_ident = options[:hex_ident]
  @ports = enrich_ports(options[:ports])

  l4_ports = @ports.select { |p| is_l4_port(p) }

  if l4_ports.count > 0 && l4_ports.count < @ports.count
    raise 'Ports have to either be all layer 4 or 7'
  end

  @type = l4_ports.count == 0 ? 'application' : 'network'

  ident = make_identifier(@type, vpc.name, name)
  @name = ident

  if application?
    @security_group = resource :aws_security_group, ident,
                               name: "loadbalancer-#{ident}",
                               description: "Describe the ingress and egress of the load balancer #{ident}",
                               tags: @tags.merge(
                                 loadbalancer_name: ident
                               ),
                               vpc_id: vpc.id

    path_mtu_setup!
  end

  if network? && options[:security_groups].count > 0
    warn 'You cannot set security groups on a network loadbalancer, set them on the instances behind it.'
  end

  @id = resource :aws_lb, ident, {
    name: ident,
    load_balancer_type: type,
    internal: !options[:public],
    tags: @tags
  }.merge(subnets_for(options[:subnets]))
        .merge(application? ? { security_groups: [@security_group] + options[:security_groups], idle_timeout: options[:idle_timeout], access_logs: options[:access_logs] } : {})
        .merge(network? ? { enable_cross_zone_load_balancing: options[:cross_zone_load_balancing] } : {})
        .compact

  @targets = []

  @ports.each do |port|
    port_ident = "#{ident}-#{port[:downstream_port]}"

    default_action = port.key?(:action) ? port[:action] : forward_to_tg(port, port_ident, vpc)

    ssl_options = alb_certs(port, port_ident)

    listener = resource :aws_lb_listener, port_ident, {
      load_balancer_arn: @id,
      port: port[:upstream_port],
      protocol: port[:type].upcase,
      default_action: default_action
    }.merge(ssl_options)

    register_target(default_action[:target_group_arn], listener) if default_action[:type] == 'forward'
  end

  @alias_config = {
    name: output_of(:aws_lb, ident, :dns_name),
    zone_id: output_of(:aws_lb, ident, :zone_id),
    evaluate_target_health: true
  }
  self
end

#find_in(vpc, name) ⇒ Object



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
# File 'lib/terrafying/components/loadbalancer.rb', line 30

def find_in(vpc, name)
  @type = 'network'
  ident = make_identifier(@type, vpc.name, name)

  begin
    lb = aws.lb_by_name(ident)
  rescue StandardError
    @type = 'application'
    ident = make_identifier(@type, vpc.name, name)

    lb = aws.lb_by_name(ident)

    @security_group = aws.security_group_by_tags(loadbalancer_name: ident)
  end

  @id = lb.load_balancer_arn
  @name = ident

  target_groups = aws.target_groups_by_lb(@id)

  @targets = target_groups.map do |tg|
    Struct::Target.new(
      target_group: tg.target_group_arn,
      listener: nil
    )
  end

  @ports = enrich_ports(target_groups.map(&:port).sort.uniq)

  @alias_config = {
    name: lb.dns_name,
    zone_id: lb.canonical_hosted_zone_id,
    evaluate_target_health: true
  }

  self
end

#forward_to_tg(port, port_ident, vpc) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/terrafying/components/loadbalancer.rb', line 151

def forward_to_tg(port, port_ident, vpc)
  target_group = resource :aws_lb_target_group, port_ident, {
    name: port_ident,
    port: port[:downstream_port],
    protocol: port[:type].upcase,
    vpc_id: vpc.id
  }.merge(port.key?(:health_check) ? { health_check: port[:health_check] } : {})

  {
    type: 'forward',
    target_group_arn: target_group
  }
end

#make_identifier(type, vpc_name, name) ⇒ Object



218
219
220
221
222
223
# File 'lib/terrafying/components/loadbalancer.rb', line 218

def make_identifier(type, vpc_name, name)
  gen_id = "#{type}-#{tf_safe(vpc_name)}-#{name}"
  return Digest::SHA2.hexdigest(gen_id)[0..24] if @hex_ident || gen_id.size > 26

  gen_id[0..31]
end

#network?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/terrafying/components/loadbalancer.rb', line 201

def network?
  @type == 'network'
end

#register_target(target_group, listener) ⇒ Object



165
166
167
168
169
170
# File 'lib/terrafying/components/loadbalancer.rb', line 165

def register_target(target_group, listener)
  @targets << Struct::Target.new(
    target_group: target_group,
    listener: listener
  )
end

#subnets_for(subnets) ⇒ Object



197
198
199
# File 'lib/terrafying/components/loadbalancer.rb', line 197

def subnets_for(subnets)
  { subnets: subnets.map(&:id) }
end