Class: Terrafying::Components::Instance

Inherits:
Terrafying::Context
  • Object
show all
Includes:
Usable
Defined in:
lib/terrafying/components/instance.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

#initializeInstance

Returns a new instance of Instance.



22
23
24
# File 'lib/terrafying/components/instance.rb', line 22

def initialize()
  super
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/terrafying/components/instance.rb', line 10

def id
  @id
end

#ip_addressObject (readonly)

Returns the value of attribute ip_address.



10
11
12
# File 'lib/terrafying/components/instance.rb', line 10

def ip_address
  @ip_address
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/terrafying/components/instance.rb', line 10

def name
  @name
end

#subnetObject (readonly)

Returns the value of attribute subnet.



10
11
12
# File 'lib/terrafying/components/instance.rb', line 10

def subnet
  @subnet
end

Class Method Details

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



14
15
16
# File 'lib/terrafying/components/instance.rb', line 14

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

.find_in(vpc, name) ⇒ Object



18
19
20
# File 'lib/terrafying/components/instance.rb', line 18

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

Instance Method Details

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



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

def create_in(vpc, name, options={})
  options = {
    public: false,
    instance_type: "t2.micro",
    instance_profile: nil,
    ports: [],
    tags: {},
    security_groups: [],
    depends_on: [],
  }.merge(options)

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

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

  @security_group = resource :aws_security_group, ident, {
                               name: "instance-#{ident}",
                               description: "Describe the ingress and egress of the instance #{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!

  if options.has_key? :ip_address
    lifecycle = {
      lifecycle: { create_before_destroy: false },
    }
  else
    lifecycle = {
      lifecycle: { create_before_destroy: true },
    }
  end

  if options.has_key? :subnet
    @subnet = options[:subnet]
  else
    subnets = options.fetch(:subnets, vpc.subnets[:private])
    # pick something consistent but not just the first subnet
    subnet_index = XXhash.xxh32(ident) % subnets.count
    @subnet = subnets[subnet_index]
  end

  @id = resource :aws_instance, ident, {
                   ami: options[:ami],
                   instance_type: options[:instance_type],
                   iam_instance_profile: options[:instance_profile] && options[:instance_profile].id,
                   subnet_id: @subnet.id,
                   associate_public_ip_address: options[:public],
                   root_block_device: {
                     volume_type: 'gp2',
                     volume_size: 32,
                   },
                   tags: {
                     'Name' => ident,
                   }.merge(options[:tags]),
                   vpc_security_group_ids: [
                     vpc.internal_ssh_security_group,
                   ].push(*options[:security_groups]),
                   user_data: options[:user_data],
                   lifecycle: {
                     create_before_destroy: true,
                   },
                   depends_on: options[:depends_on],
                 }.merge(options[:ip_address] ? { private_ip: options[:ip_address] } : {}).merge(lifecycle)

  @ip_address = output_of(:aws_instance, ident, options[:public] ? :public_ip : :private_ip)

  self
end

#find_in(vpc, name) ⇒ Object



26
27
28
# File 'lib/terrafying/components/instance.rb', line 26

def find_in(vpc, name)
  raise 'unimplemented'
end