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.



27
28
29
# File 'lib/terrafying/components/staticset.rb', line 27

def initialize()
  super
end

Instance Attribute Details

#instancesObject (readonly)

Returns the value of attribute instances.



15
16
17
# File 'lib/terrafying/components/staticset.rb', line 15

def instances
  @instances
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/terrafying/components/staticset.rb', line 15

def name
  @name
end

Class Method Details

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



19
20
21
# File 'lib/terrafying/components/staticset.rb', line 19

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

.find_in(vpc, name) ⇒ Object



23
24
25
# File 'lib/terrafying/components/staticset.rb', line 23

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

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

  self.used_by(load_balancer) if load_balancer.type == "application"
end

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



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

def create_in(vpc, name, options={})
  options = {
    public: false,
    ami: aws.ami("base-image-59a5b709", owners=["136393635417"]),
    instance_type: "t2.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,
                               egress: [
                                 {
                                   from_port: 0,
                                   to_port: 0,
                                   protocol: -1,
                                   cidr_blocks: ["0.0.0.0/0"],
                                 }
                               ],
                             }

  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 { |volume, vol_i|
      volume_for("#{instance_ident}-#{vol_i}", instance, volume, options[:tags])
    }

    instance
  end

  @ports.each { |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,
             }
  }

  self
end

#find_in(vpc, name) ⇒ Object



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

def find_in(vpc, name)
  @name = name

  raise 'unimplemented'

  self
end

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



116
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 116

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