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.



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

def initialize()
  super
end

Instance Attribute Details

#alias_configObject (readonly)

Returns the value of attribute alias_config.



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

def alias_config
  @alias_config
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#portsObject (readonly)

Returns the value of attribute ports.



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

def ports
  @ports
end

#security_groupObject (readonly)

Returns the value of attribute security_group.



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

def security_group
  @security_group
end

#target_groupsObject (readonly)

Returns the value of attribute target_groups.



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

def target_groups
  @target_groups
end

#typeObject (readonly)

Returns the value of attribute type.



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

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, options={})
  LoadBalancer.new.create_in vpc, name, options
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

#attach(set) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/terrafying/components/loadbalancer.rb', line 147

def attach(set)
  if set.respond_to?(:attach_load_balancer)
    set.attach_load_balancer(self)
  else
    raise "Dont' know how to attach object to LB"
  end
end

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



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

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

  @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 = "#{type}-#{tf_safe(vpc.name)}-#{name}"
  @name = ident

  if @type == "application"
    @security_group = resource :aws_security_group, ident, {
                                 name: "loadbalancer-#{ident}",
                                 description: "Describe the ingress and egress of the load balancer #{ident}",
                                 tags: options[: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: !options[:public],
                   subnets: options[:subnets].map(&:id),
                   tags: options[:tags],
                 }.merge(@type == "application" ? { security_groups: [@security_group] } : {})

  @target_groups = []

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

    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.has_key?(:health_check) ? { health_check: port[:health_check] }: {})

    ssl_options = {}
    if port.has_key?(:ssl_certificate)
      ssl_options = {
        ssl_policy: "ELBSecurityPolicy-2015-05",
        certificate_arn: port[:ssl_certificate],
      }
    end

    resource :aws_lb_listener, port_ident, {
               load_balancer_arn: @id,
               port: port[:upstream_port],
               protocol: port[:type].upcase,
               default_action: {
                 target_group_arn: target_group,
                 type: "forward",
               },
             }.merge(ssl_options)

    @target_groups << target_group
  }

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

def find_in(vpc, name)
  ident = "network-#{tf_safe(vpc.name)}-#{name}"
  @type = "network"

  begin
    lb = aws.lb_by_name(ident)
  rescue
    ident = "application-#{tf_safe(vpc.name)}-#{name}"
    @type = "application"

    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)

  @target_groups = target_groups.map(&:target_group_arn)
  @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