Class: Terrafying::Components::Instance
- Inherits:
-
Terrafying::Context
- Object
- Terrafying::Context
- Terrafying::Components::Instance
- Includes:
- Usable
- Defined in:
- lib/terrafying/components/instance.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#ip_address ⇒ Object
readonly
Returns the value of attribute ip_address.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#subnet ⇒ Object
readonly
Returns the value of attribute subnet.
Class Method Summary collapse
Instance Method Summary collapse
- #create_in(vpc, name, options = {}) ⇒ Object
- #find_in(_vpc, _name) ⇒ Object
-
#initialize ⇒ Instance
constructor
A new instance of Instance.
- #profile_from(profile) ⇒ Object
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
#initialize ⇒ Instance
Returns a new instance of Instance.
24 25 26 |
# File 'lib/terrafying/components/instance.rb', line 24 def initialize super end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
12 13 14 |
# File 'lib/terrafying/components/instance.rb', line 12 def id @id end |
#ip_address ⇒ Object (readonly)
Returns the value of attribute ip_address.
12 13 14 |
# File 'lib/terrafying/components/instance.rb', line 12 def ip_address @ip_address end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/terrafying/components/instance.rb', line 12 def name @name end |
#subnet ⇒ Object (readonly)
Returns the value of attribute subnet.
12 13 14 |
# File 'lib/terrafying/components/instance.rb', line 12 def subnet @subnet end |
Class Method Details
Instance Method Details
#create_in(vpc, name, options = {}) ⇒ Object
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/terrafying/components/instance.rb', line 32 def create_in(vpc, name, = {}) = { public: false, eip: false, instance_type: 't3a.micro', instance_profile: nil, ports: [], tags: {}, metadata_options: nil, security_groups: [], depends_on: [] }.merge() ident = "#{tf_safe(vpc.name)}-#{name}" @name = name @ports = enrich_ports([:ports]) @security_group = resource :aws_security_group, ident, name: "instance-#{ident}", description: "Describe the ingress and egress of the instance #{ident}", tags: [: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 .key? :ip_address { lifecycle: { create_before_destroy: false } } else { lifecycle: { create_before_destroy: true } } end if .key? :subnet @subnet = [:subnet] else subnets = .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 = [:eip] || [:public] @id = resource :aws_instance, ident, { ami: [:ami], instance_type: [:instance_type], iam_instance_profile: profile_from([: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([:tags]), vpc_security_group_ids: [ vpc.internal_ssh_security_group ].push(*[:security_groups]), user_data: [:user_data], metadata_options: [:metadata_options], lifecycle: { create_before_destroy: true }, depends_on: [:depends_on] }.merge([:ip_address] ? { private_ip: [:ip_address] } : {}).merge(lifecycle) @ip_address = @id[[:public] ? :public_ip : :private_ip] if [:eip] @eip = resource :aws_eip, ident, { instance: @id, vpc: true } @ip_address = @eip[:public_ip] end self end |
#find_in(_vpc, _name) ⇒ Object
28 29 30 |
# File 'lib/terrafying/components/instance.rb', line 28 def find_in(_vpc, _name) raise 'unimplemented' end |
#profile_from(profile) ⇒ Object
129 130 131 |
# File 'lib/terrafying/components/instance.rb', line 129 def profile_from(profile) profile.respond_to?(:id) ? profile.id : profile end |