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



162
163
164
165
166
167
168
169
170
# File 'lib/terrafying/components/staticset.rb', line 162

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}".gsub(%r{^(\d)}, '_\1'),
             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
106
107
108
109
110
# File 'lib/terrafying/components/staticset.rb', line 37

def create_in(vpc, name, options = {})
  options = {
    public: false,
    eip: false,
    ami: aws.ami('base-image-fc-75aa2aef', owners = ['477284023816']),
    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: [],
    vpc_endpoints_egress: []
  }.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

  vpc_endpoints_egress = options[:vpc_endpoints_egress]
  if vpc_endpoints_egress.empty?
    default_egress_rule(ident, @security_group)
  else
    vpc_endpoint_egress_rules(ident, @security_group, vpc, vpc_endpoints_egress)
  end
  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] == 'udp' ? 'udp' : 'tcp',
             self: true
  end

  self
end

#default_egress_rule(ident, security_group) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/terrafying/components/staticset.rb', line 112

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/terrafying/components/staticset.rb', line 141

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

#vpc_endpoint_egress_rules(ident, security_group, vpc, vpc_endpoints) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/terrafying/components/staticset.rb', line 123

def vpc_endpoint_egress_rules(ident, security_group, vpc, vpc_endpoints)
  prefix_ids = vpc_endpoints.map do | e |
    vpc_endpoint = data :aws_vpc_endpoint, "#{ident}-#{tf_safe(e)}", {
      vpc_id: vpc.id,
      service_name: e,
    }
    vpc_endpoint[:prefix_list_id]
  end

  resource :aws_security_group_rule, "#{ident}-vpc-endpoint-egress",
           security_group_id: security_group,
           type: 'egress',
           from_port: 0,
           to_port: 0,
           protocol: -1,
           prefix_list_ids: prefix_ids
end