Class: Terrafying::Components::LoadBalancer
- Inherits:
-
Terrafying::Context
- Object
- Terrafying::Context
- Terrafying::Components::LoadBalancer
- Includes:
- Usable
- Defined in:
- lib/terrafying/components/loadbalancer.rb
Instance Attribute Summary collapse
-
#alias_config ⇒ Object
readonly
Returns the value of attribute alias_config.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ports ⇒ Object
readonly
Returns the value of attribute ports.
-
#security_group ⇒ Object
readonly
Returns the value of attribute security_group.
-
#targets ⇒ Object
readonly
Returns the value of attribute targets.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #alb_cert(cert_arn, port_ident) ⇒ Object
- #alb_certs(port, port_ident) ⇒ Object
- #application? ⇒ Boolean
- #attach(set) ⇒ Object
- #autoscale(set, target_value:, disable_scale_in:) ⇒ Object
- #create_in(vpc, name, options = {}) ⇒ Object
- #find_in(vpc, name) ⇒ Object
- #forward_to_tg(port, port_ident, vpc) ⇒ Object
-
#initialize ⇒ LoadBalancer
constructor
A new instance of LoadBalancer.
- #make_identifier(type, vpc_name, name) ⇒ Object
- #network? ⇒ Boolean
- #register_target(target_group, listener) ⇒ Object
- #subnets_for(subnets) ⇒ Object
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
#initialize ⇒ LoadBalancer
Returns a new instance of LoadBalancer.
25 26 27 |
# File 'lib/terrafying/components/loadbalancer.rb', line 25 def initialize() super end |
Instance Attribute Details
#alias_config ⇒ Object (readonly)
Returns the value of attribute alias_config.
11 12 13 |
# File 'lib/terrafying/components/loadbalancer.rb', line 11 def alias_config @alias_config end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/terrafying/components/loadbalancer.rb', line 11 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/terrafying/components/loadbalancer.rb', line 11 def name @name end |
#ports ⇒ Object (readonly)
Returns the value of attribute ports.
11 12 13 |
# File 'lib/terrafying/components/loadbalancer.rb', line 11 def ports @ports end |
#security_group ⇒ Object (readonly)
Returns the value of attribute security_group.
11 12 13 |
# File 'lib/terrafying/components/loadbalancer.rb', line 11 def security_group @security_group end |
#targets ⇒ Object (readonly)
Returns the value of attribute targets.
11 12 13 |
# File 'lib/terrafying/components/loadbalancer.rb', line 11 def targets @targets end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
11 12 13 |
# File 'lib/terrafying/components/loadbalancer.rb', line 11 def type @type end |
Class Method Details
.create_in(vpc, name, options = {}) ⇒ Object
17 18 19 |
# File 'lib/terrafying/components/loadbalancer.rb', line 17 def self.create_in(vpc, name, ={}) LoadBalancer.new.create_in vpc, name, end |
.find_in(vpc, name) ⇒ Object
21 22 23 |
# File 'lib/terrafying/components/loadbalancer.rb', line 21 def self.find_in(vpc, name) LoadBalancer.new.find_in vpc, name end |
Instance Method Details
#alb_cert(cert_arn, port_ident) ⇒ Object
180 181 182 183 184 185 186 187 |
# File 'lib/terrafying/components/loadbalancer.rb', line 180 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
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/terrafying/components/loadbalancer.rb', line 167 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-2016-08', certificate_arn: default_cert } end |
#application? ⇒ Boolean
189 190 191 |
# File 'lib/terrafying/components/loadbalancer.rb', line 189 def application? @type == 'application' end |
#attach(set) ⇒ Object
201 202 203 204 205 |
# File 'lib/terrafying/components/loadbalancer.rb', line 201 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
207 208 209 210 |
# File 'lib/terrafying/components/loadbalancer.rb', line 207 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
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 |
# File 'lib/terrafying/components/loadbalancer.rb', line 67 def create_in(vpc, name, ={}) = { ports: [], public: false, subnets: vpc.subnets.fetch(:private, []), hex_ident: false, idle_timeout: nil, tags: {} }.merge() = { Name: name }.merge([:tags]) @hex_ident = [:hex_ident] @ports = enrich_ports([: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: .merge( { loadbalancer_name: ident, } ), vpc_id: vpc.id, } path_mtu_setup! end @id = resource :aws_lb, ident, { name: ident, load_balancer_type: type, internal: ![:public], tags: , }.merge(subnets_for([:subnets])) .merge(application? ? { security_groups: [@security_group], idle_timeout: [:idle_timeout], access_logs: [:access_logs] } : {}) .compact @targets = [] @ports.each { |port| port_ident = "#{ident}-#{port[:downstream_port]}" default_action = port.key?(:action) ? port[:action] : forward_to_tg(port, port_ident, vpc) = 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() register_target(default_action[:target_group_arn], listener) if default_action[:type] == 'forward' } @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
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 |
# File 'lib/terrafying/components/loadbalancer.rb', line 29 def find_in(vpc, name) @type = "network" ident = make_identifier(@type, vpc.name, name) begin lb = aws.lb_by_name(ident) rescue @type = "application" ident = make_identifier(@type, vpc.name, name) lb = aws.lb_by_name(ident) @security_group = aws.({ loadbalancer_name: ident }) end @id = lb.load_balancer_arn @name = ident target_groups = aws.target_groups_by_lb(@id) @targets = target_groups.map { |tg| Struct::Target.new( target_group: tg.target_group_arn, listener: nil ) } @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
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/terrafying/components/loadbalancer.rb', line 146 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
212 213 214 215 216 |
# File 'lib/terrafying/components/loadbalancer.rb', line 212 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
197 198 199 |
# File 'lib/terrafying/components/loadbalancer.rb', line 197 def network? @type == 'network' end |
#register_target(target_group, listener) ⇒ Object
160 161 162 163 164 165 |
# File 'lib/terrafying/components/loadbalancer.rb', line 160 def register_target(target_group, listener) @targets << Struct::Target.new( target_group: target_group, listener: listener ) end |
#subnets_for(subnets) ⇒ Object
193 194 195 |
# File 'lib/terrafying/components/loadbalancer.rb', line 193 def subnets_for(subnets) return { subnets: subnets.map(&:id) } end |