class Terrafying::Components::Instance

Attributes

id[R]
ip_address[R]
name[R]
subnet[R]

Public Class Methods

create_in(vpc, name, options = {}) click to toggle source
# File lib/terrafying/components/instance.rb, line 15
def self.create_in(vpc, name, options = {})
  Instance.new.create_in vpc, name, options
end
find_in(vpc, name) click to toggle source
# File lib/terrafying/components/instance.rb, line 19
def self.find_in(vpc, name)
  Instance.new.find_in vpc, name
end
new() click to toggle source
Calls superclass method
# File lib/terrafying/components/instance.rb, line 23
def initialize
  super
end

Public Instance Methods

create_in(vpc, name, options = {}) click to toggle source
# File lib/terrafying/components/instance.rb, line 31
def create_in(vpc, name, options = {})
  options = {
    public: false,
    eip: false,
    instance_type: 't3a.micro',
    instance_profile: nil,
    ports: [],
    tags: {},
    metadata_options: nil,
    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'],
                                 ipv6_cidr_blocks: nil,
                                 prefix_list_ids: nil,
                                 security_groups: nil,
                                 self: nil,
                                 description: nil
                               }
                             ]

  path_mtu_setup!

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

  if options.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

  associate_public_ip_address = options[:eip] || options[:public]

  @id = resource :aws_instance, ident, {
    ami: options[:ami],
    instance_type: options[:instance_type],
    iam_instance_profile: profile_from(options[:instance_profile]),
    subnet_id: @subnet.id,
    associate_public_ip_address: associate_public_ip_address,
    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],
    metadata_options: options[:metadata_options],
    lifecycle: {
      create_before_destroy: true
    },
    depends_on: options[:depends_on]
  }.merge(options[:ip_address] ? { private_ip: options[:ip_address] } : {}).merge(lifecycle)

  @ip_address = @id[options[:public] ? :public_ip : :private_ip]

  if options[:eip]
    @eip = resource :aws_eip, ident, {
      instance: @id,
      vpc: true
    }
    @ip_address = @eip[:public_ip]
  end

  self
end
find_in(_vpc, _name) click to toggle source
# File lib/terrafying/components/instance.rb, line 27
def find_in(_vpc, _name)
  raise 'unimplemented'
end
profile_from(profile) click to toggle source
# File lib/terrafying/components/instance.rb, line 128
def profile_from(profile)
  profile.respond_to?(:id) ? profile.id : profile
end