Class: Terrafying::Components::StaticSet

Inherits:
Terrafying::Context
  • Object
show all
Includes:
Usable
Defined in:
lib/terrafying/components/staticset.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, #security_group, #used_by, #used_by_cidr

Constructor Details

#initializeStaticSet

Returns a new instance of StaticSet.



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

def initialize
  super
end

Instance Attribute Details

#instancesObject (readonly)

Returns the value of attribute instances.



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

def instances
  @instances
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

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



17
18
19
# File 'lib/terrafying/components/staticset.rb', line 17

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

.find_in(vpc, name) ⇒ Object



21
22
23
# File 'lib/terrafying/components/staticset.rb', line 21

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

Instance Method Details

#attach_load_balancer(load_balancer) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/terrafying/components/staticset.rb', line 138

def attach_load_balancer(load_balancer)
  @instances.product(load_balancer.targets).each.with_index do |(instance, target), i|
    resource :aws_lb_target_group_attachment, "#{load_balancer.name}-#{@name}-#{i}",
             target_group_arn: target.target_group,
             target_id: instance.id
  end

  used_by(load_balancer) if load_balancer.type == 'application'
end

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



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

def create_in(vpc, name, options = {})
  options = {
    public: false,
    eip: false,
    ami: aws.ami('base-image-f074c65a', owners = ['136393635417']),
    instance_type: 't3a.micro',
    subnets: vpc.subnets.fetch(:private, []),
    ports: [],
    instances: [{}],
    instance_profile: nil,
    security_groups: [],
    user_data: '',
    tags: {},
    ssh_group: vpc.ssh_group,
    depends_on: [],
    volumes: []
  }.merge(options)

  ident = "#{tf_safe(vpc.name)}-#{name}"

  @name = ident
  @ports = enrich_ports(options[:ports])

  @security_group = resource :aws_security_group, ident,
                             name: "staticset-#{ident}",
                             description: "Describe the ingress and egress of the static set #{ident}",
                             tags: options[:tags],
                             vpc_id: vpc.id

  default_egress_rule(ident, @security_group)

  path_mtu_setup!

  @instances = options[:instances].map.with_index do |config, i|
    instance_ident = "#{name}-#{i}"

    instance = add! Instance.create_in(
      vpc, instance_ident, options.merge(
                             {
                               subnets: options[:subnets],
                               security_groups: [@security_group] + options[:security_groups],
                               depends_on: options[:depends_on],
                               instance_profile: options[:instance_profile],
                               tags: {
                                 staticset_name: ident
                               }.merge(options[:tags])
                             }.merge(config)
                           )
    )

    options[:volumes].each.with_index do |volume, vol_i|
      volume_for("#{instance_ident}-#{vol_i}", instance, volume, options[:tags])
    end

    instance
  end

  @ports.each do |port|
    resource :aws_security_group_rule, "#{@name}-to-self-#{port[:name]}",
             security_group_id: @security_group,
             type: 'ingress',
             from_port: from_port(port[:upstream_port]),
             to_port: to_port(port[:upstream_port]),
             protocol: port[:type],
             self: true
  end

  self
end

#default_egress_rule(ident, security_group) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/terrafying/components/staticset.rb', line 107

def default_egress_rule(ident, security_group)
  resource :aws_security_group_rule, "#{ident}-default-egress",
           security_group_id: security_group,
           type: 'egress',
           from_port: 0,
           to_port: 0,
           protocol: -1,
           cidr_blocks: ['0.0.0.0/0']
end

#find_in(_vpc, name) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/terrafying/components/staticset.rb', line 29

def find_in(_vpc, name)
  @name = name

  raise 'unimplemented'

  self
end

#volume_for(name, instance, volume, tags) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/terrafying/components/staticset.rb', line 117

def volume_for(name, instance, volume, tags)
  vol_opts = {
    availability_zone: instance.subnet.az,
    size: volume[:size],
    type: volume[:type] || 'gp2',
    encrypted: volume[:encrypted] || false,
    kms_key_id: volume[:kms_key_id],
    tags: {
      Name: name
    }.merge(tags)
  }.reject { |_, v| v.nil? }

  volume_id = resource :aws_ebs_volume, name, vol_opts

  resource :aws_volume_attachment, name,
           device_name: volume[:device],
           volume_id: volume_id,
           instance_id: instance.id,
           force_detach: true
end