Class: Terrafying::Components::StaticSet
- Inherits:
-
Terrafying::Context
- Object
- Terrafying::Context
- Terrafying::Components::StaticSet
- Includes:
- Usable
- Defined in:
- lib/terrafying/components/staticset.rb
Instance Attribute Summary collapse
-
#instances ⇒ Object
readonly
Returns the value of attribute instances.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #attach_load_balancer(load_balancer) ⇒ Object
- #create_in(vpc, name, options = {}) ⇒ Object
- #default_egress_rule(ident, security_group) ⇒ Object
- #find_in(vpc, name) ⇒ Object
-
#initialize ⇒ StaticSet
constructor
A new instance of StaticSet.
- #volume_for(name, instance, volume, tags) ⇒ 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 ⇒ StaticSet
Returns a new instance of StaticSet.
27 28 29 |
# File 'lib/terrafying/components/staticset.rb', line 27 def initialize() super end |
Instance Attribute Details
#instances ⇒ Object (readonly)
Returns the value of attribute instances.
15 16 17 |
# File 'lib/terrafying/components/staticset.rb', line 15 def instances @instances end |
#name ⇒ Object (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
Instance Method Details
#attach_load_balancer(load_balancer) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/terrafying/components/staticset.rb', line 143 def attach_load_balancer(load_balancer) @instances.product(load_balancer.targets).each.with_index { |(instance, target), i| resource :aws_lb_target_group_attachment, "#{load_balancer.name}-#{@name}-#{i}", { target_group_arn: target.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 |
# File 'lib/terrafying/components/staticset.rb', line 39 def create_in(vpc, name, ={}) = { 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() ident = "#{tf_safe(vpc.name)}-#{name}" @name = ident @ports = enrich_ports([:ports]) @security_group = resource :aws_security_group, ident, { name: "staticset-#{ident}", description: "Describe the ingress and egress of the static set #{ident}", tags: [:tags], vpc_id: vpc.id, } default_egress_rule(ident, @security_group) path_mtu_setup! @instances = [:instances].map.with_index do |config, i| instance_ident = "#{name}-#{i}" instance = add! Instance.create_in( vpc, instance_ident, .merge( { subnets: [:subnets], security_groups: [@security_group] + [:security_groups], depends_on: [:depends_on], instance_profile: [:instance_profile], tags: { staticset_name: ident, }.merge([:tags]) }.merge(config) ) ) [:volumes].each.with_index { |volume, vol_i| volume_for("#{instance_ident}-#{vol_i}", instance, volume, [: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 |
#default_egress_rule(ident, security_group) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/terrafying/components/staticset.rb', line 110 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
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
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/terrafying/components/staticset.rb', line 121 def volume_for(name, instance, volume, ) 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() }.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 |