Class: Terrafying::Components::VPC
- Inherits:
-
Terrafying::Context
- Object
- Terrafying::Context
- Terrafying::Components::VPC
- Defined in:
- lib/terrafying/components/vpc.rb
Instance Attribute Summary collapse
-
#azs ⇒ Object
readonly
Returns the value of attribute azs.
-
#cidr ⇒ Object
readonly
Returns the value of attribute cidr.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#internal_ssh_security_group ⇒ Object
readonly
Returns the value of attribute internal_ssh_security_group.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ssh_group ⇒ Object
readonly
Returns the value of attribute ssh_group.
-
#subnets ⇒ Object
readonly
Returns the value of attribute subnets.
-
#zone ⇒ Object
readonly
Returns the value of attribute zone.
Class Method Summary collapse
Instance Method Summary collapse
- #allocate_subnets!(name, options = {}) ⇒ Object
- #allocate_subnets_in!(ctx, name, options = {}) ⇒ Object
- #create(name, raw_cidr, options = {}) ⇒ Object
- #drop_subnet!(raw_cidr) ⇒ Object
- #extract_subnet!(bit_size) ⇒ Object
- #find(name) ⇒ Object
-
#initialize ⇒ VPC
constructor
A new instance of VPC.
- #peer_with(other_vpc, options = {}) ⇒ Object
- #peer_with_external(account_id, vpc_id, cidrs, options = {}) ⇒ Object
- #peer_with_vpn(ip_address, cidrs, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ VPC
Returns a new instance of VPC.
26 27 28 |
# File 'lib/terrafying/components/vpc.rb', line 26 def initialize super end |
Instance Attribute Details
#azs ⇒ Object (readonly)
Returns the value of attribute azs.
16 17 18 |
# File 'lib/terrafying/components/vpc.rb', line 16 def azs @azs end |
#cidr ⇒ Object (readonly)
Returns the value of attribute cidr.
16 17 18 |
# File 'lib/terrafying/components/vpc.rb', line 16 def cidr @cidr end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
16 17 18 |
# File 'lib/terrafying/components/vpc.rb', line 16 def id @id end |
#internal_ssh_security_group ⇒ Object (readonly)
Returns the value of attribute internal_ssh_security_group.
16 17 18 |
# File 'lib/terrafying/components/vpc.rb', line 16 def internal_ssh_security_group @internal_ssh_security_group end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/terrafying/components/vpc.rb', line 16 def name @name end |
#ssh_group ⇒ Object (readonly)
Returns the value of attribute ssh_group.
16 17 18 |
# File 'lib/terrafying/components/vpc.rb', line 16 def ssh_group @ssh_group end |
#subnets ⇒ Object (readonly)
Returns the value of attribute subnets.
16 17 18 |
# File 'lib/terrafying/components/vpc.rb', line 16 def subnets @subnets end |
#zone ⇒ Object (readonly)
Returns the value of attribute zone.
16 17 18 |
# File 'lib/terrafying/components/vpc.rb', line 16 def zone @zone end |
Class Method Details
Instance Method Details
#allocate_subnets!(name, options = {}) ⇒ Object
396 397 398 |
# File 'lib/terrafying/components/vpc.rb', line 396 def allocate_subnets!(name, = {}) allocate_subnets_in!(self, name, ) end |
#allocate_subnets_in!(ctx, name, options = {}) ⇒ Object
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/terrafying/components/vpc.rb', line 400 def allocate_subnets_in!(ctx, name, = {}) = { public: false, bit_size: @subnet_size, internet: true, tags: {} }.merge() gateways = if [:public] [@internet_gateway] * @azs.count elsif [:internet] && !@nat_gateways.nil? @nat_gateways else [nil] * @azs.count end if @subnets.has_key?(name.to_sym) @subnets[name.to_sym].zip(gateways).map do |subnet, gateway| = { tags: { subnet_name: name }.merge([:tags]).merge(@tags) } unless gateway.nil? if [:public] [:gateway] = gateway elsif [:internet] [:nat_gateway] = gateway end end ctx.add! Terrafying::Components::Subnet.create_in( self, name, subnet.az, subnet.cidr, ) end else @subnets[name] = @azs.zip(gateways).map do |az, gateway| = { tags: { subnet_name: name }.merge([:tags]).merge(@tags) } unless gateway.nil? if [:public] [:gateway] = gateway elsif [:internet] [:nat_gateway] = gateway end end ctx.add! Terrafying::Components::Subnet.create_in( self, name, az, extract_subnet!([:bit_size]), ) end end end |
#create(name, raw_cidr, options = {}) ⇒ Object
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/terrafying/components/vpc.rb', line 86 def create(name, raw_cidr, = {}) = { subnet_size: 24, internet_access: true, nat_eips: [], azs: aws.availability_zones, tags: {}, ssh_group: DEFAULT_SSH_GROUP }.merge() if [:parent_zone].nil? [:parent_zone] = Zone.find(DEFAULT_ZONE) end if [:subnets].nil? [:subnets] = if [:internet_access] { public: { public: true }, private: { internet: true } } else { private: {} } end end @name = name @cidr = raw_cidr @azs = [:azs] @tags = [:tags] @ssh_group = [:ssh_group] cidr = NetAddr::CIDR.create(raw_cidr) @remaining_ip_space = NetAddr::Tree.new @remaining_ip_space.add! cidr @subnet_size = [:subnet_size] @subnets = {} per_az_subnet_size = [:subnets].values.reduce(0) do |memo, s| memo + (1 << (32 - s.fetch(:bit_size, @subnet_size))) end total_subnet_size = per_az_subnet_size * @azs.count if total_subnet_size > cidr.size raise 'Not enough space for subnets in CIDR' end @id = resource :aws_vpc, name, cidr_block: cidr.to_s, enable_dns_hostnames: true, tags: { Name: name, ssh_group: @ssh_group }.merge(@tags) @zone = add! Terrafying::Components::Zone.create("#{name}.#{[:parent_zone].fqdn}", parent_zone: [:parent_zone], tags: { vpc: @id }.merge(@tags)) dhcp = resource :aws_vpc_dhcp_options, name, domain_name: @zone.fqdn, domain_name_servers: ['AmazonProvidedDNS'], tags: { Name: name }.merge(@tags) resource :aws_vpc_dhcp_options_association, name, vpc_id: @id, dhcp_options_id: dhcp if [:internet_access] if [:nat_eips].empty? [:nat_eips] = @azs.map { |az| resource :aws_eip, "#{name}-nat-gateway-#{az}", vpc: true } elsif [:nat_eips].size != @azs.count raise 'The nubmer of nat eips has to match the number of AZs' end @internet_gateway = resource :aws_internet_gateway, name, vpc_id: @id, tags: { Name: name }.merge(@tags) allocate_subnets!(:nat_gateway, bit_size: 28, public: true) @nat_gateways = @azs.zip(@subnets[:nat_gateway], [:nat_eips]).map do |az, subnet, eip| resource :aws_nat_gateway, "#{name}-#{az}", allocation_id: eip, subnet_id: subnet.id end end [:subnets].each { |key, config| allocate_subnets! key, config } @internal_ssh_security_group = resource :aws_security_group, "#{name}-internal-ssh", name: "#{name}-internal-ssh", description: 'Allows SSH between machines inside the VPC CIDR', tags: @tags, vpc_id: @id, ingress: [ { from_port: 22, to_port: 22, protocol: 'tcp', cidr_blocks: [@cidr], description: nil, ipv6_cidr_blocks: nil, prefix_list_ids: nil, security_groups: nil, self: nil } ], egress: [ { from_port: 22, to_port: 22, protocol: 'tcp', cidr_blocks: [@cidr], description: nil, ipv6_cidr_blocks: nil, prefix_list_ids: nil, security_groups: nil, self: nil } ] self end |
#drop_subnet!(raw_cidr) ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/terrafying/components/vpc.rb', line 381 def drop_subnet!(raw_cidr) cidr = NetAddr::CIDR.create(raw_cidr) bit_size = cidr.bits target = @remaining_ip_space.longest_match(cidr) @remaining_ip_space.delete!(target) if target.bits != bit_size target.remainder(cidr).each do |rem| @remaining_ip_space.add!(rem) end end end |
#extract_subnet!(bit_size) ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/terrafying/components/vpc.rb', line 356 def extract_subnet!(bit_size) bit_size = 28 if bit_size > 28 # aws can't have smaller targets = @remaining_ip_space.find_space(Subnet: bit_size) if targets.count == 0 raise "Run out of ip space to allocate a /#{bit_size}" end target = targets[0] @remaining_ip_space.delete!(target) if target.bits == bit_size new_subnet = target else new_subnet = target.subnet(Bits: bit_size, Objectify: true)[0] target.remainder(new_subnet).each do |rem| @remaining_ip_space.add!(rem) end end new_subnet.to_s end |
#find(name) ⇒ 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 |
# File 'lib/terrafying/components/vpc.rb', line 30 def find(name) vpc = aws.vpc(name) @name = name @id = vpc.vpc_id @cidr = vpc.cidr_block @azs = aws.availability_zones @remaining_ip_space = NetAddr::Tree.new @remaining_ip_space.add! cidr @tags = {} @zone = Terrafying::Components::Zone.find_by_tag(vpc: @id) raise 'Failed to find zone' if @zone.nil? @subnets = aws.subnets_for_vpc(vpc.vpc_id).each_with_object({}) do |subnet, subnets| subnet_inst = Subnet.find(subnet.subnet_id) subnet_name_tag = subnet..detect { |tag| tag.key == 'subnet_name' } key = if subnet_name_tag subnet_name_tag.value.to_sym else subnet_inst.public ? :public : :private end if subnets.key?(key) subnets[key] << subnet_inst else subnets[key] = [subnet_inst] end end # need to sort subnets so they are in az order @subnets.each { |_, s| s.sort! { |a, b| a.az <=> b.az } } @subnets.each do |_,subnet| subnet.each do |s| drop_subnet!(s.cidr) end end @nat_gateways = [] aws.nat_gateways_for_vpc(vpc.vpc_id).each do |nat_gateway| @nat_gateways << nat_gateway.nat_gateway_id end = vpc..select { |tag| tag.key == 'ssh_group' } @ssh_group = if .count > 0 [0].value else DEFAULT_SSH_GROUP end @internal_ssh_security_group = aws.security_group("#{tf_safe(name)}-internal-ssh") self end |
#peer_with(other_vpc, options = {}) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/terrafying/components/vpc.rb', line 304 def peer_with(other_vpc, = {}) = { complete: false, peerings: [ { from: @subnets.values.flatten, to: other_vpc.subnets.values.flatten }, { from: other_vpc.subnets.values.flatten, to: @subnets.values.flatten } ] }.merge() other_vpc_ident = tf_safe(other_vpc.name) our_cidr = NetAddr::CIDR.create(@cidr) other_cidr = NetAddr::CIDR.create(other_vpc.cidr) if our_cidr.contains?(other_cidr[0]) || our_cidr.contains?(other_cidr.last) raise 'VPCs to be peered have overlapping CIDRs' end peering_connection = resource :aws_vpc_peering_connection, "#{@name}-to-#{other_vpc_ident}", peer_vpc_id: other_vpc.id, vpc_id: @id, auto_accept: true, tags: { Name: "#{@name} to #{other_vpc.name}" }.merge(@tags) if [:complete] our_route_tables = @subnets.values.flatten.map(&:route_table).sort.uniq their_route_tables = other_vpc.subnets.values.flatten.map(&:route_table).sort.uniq (our_route_tables.product([other_vpc.cidr]) + their_route_tables.product([@cidr])).each do |route_table, cidr| hash = Digest::SHA2.hexdigest "#{route_table}-#{tf_safe(cidr)}" resource :aws_route, "#{@name}-#{other_vpc_ident}-peer-#{hash}", route_table_id: route_table, destination_cidr_block: cidr, vpc_peering_connection_id: peering_connection end else [:peerings].each.with_index do |peering, _i| route_tables = peering[:from].map(&:route_table).sort.uniq cidrs = peering[:to].map(&:cidr).sort.uniq route_tables.product(cidrs).each do |route_table, cidr| hash = Digest::SHA2.hexdigest "#{route_table}-#{tf_safe(cidr)}" resource :aws_route, "#{@name}-#{other_vpc_ident}-peer-#{hash}", route_table_id: route_table, destination_cidr_block: cidr, vpc_peering_connection_id: peering_connection end end end end |
#peer_with_external(account_id, vpc_id, cidrs, options = {}) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/terrafying/components/vpc.rb', line 212 def peer_with_external(account_id, vpc_id, cidrs, = {}) = { region: 'eu-west-1', subnets: @subnets.values.flatten }.merge() peering_connection = resource :aws_vpc_peering_connection, "#{@name}-to-#{account_id}-#{vpc_id}", peer_owner_id: account_id, peer_vpc_id: vpc_id, peer_region: [:region], vpc_id: @id, auto_accept: false, tags: { Name: "#{@name} to #{account_id}.#{vpc_id}" }.merge(@tags) our_route_tables = [:subnets].map(&:route_table).sort.uniq our_route_tables.product(cidrs).each do |route_table, cidr| hash = Digest::SHA2.hexdigest "#{route_table}-#{tf_safe(cidr)}" resource :aws_route, "#{@name}-to-#{account_id}-#{vpc_id}-peer-#{hash}", route_table_id: route_table, destination_cidr_block: cidr, vpc_peering_connection_id: peering_connection end end |
#peer_with_vpn(ip_address, cidrs, options = {}) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/terrafying/components/vpc.rb', line 238 def peer_with_vpn(ip_address, cidrs, = {}) = { bgp_asn: 65_000, type: 'ipsec.1', tunnels: [], static_routes_only: true, subnets: @subnets.values.flatten }.merge() ident = tf_safe(Digest::SHA256.bubblebabble(ip_address)[0..11]+ip_address) if [:tunnels].count > 2 raise 'You can only define a max of two tunnels' end customer_gateway = resource :aws_customer_gateway, ident, bgp_asn: [:bgp_asn], ip_address: ip_address, type: [:type], tags: { Name: "Connection to #{ip_address}" }.merge(@tags) vpn_gateway = resource :aws_vpn_gateway, ident, vpc_id: @id, tags: { Name: "Connection to #{ip_address}" }.merge(@tags) connection_config = { vpn_gateway_id: vpn_gateway, customer_gateway_id: customer_gateway, type: [:type], static_routes_only: [:static_routes_only], tags: { Name: "Connection to #{ip_address}" }.merge(@tags) } [:tunnels].each.with_index do |tunnel, i| connection_config["tunnel#{i + 1}_inside_cidr"] = tunnel[:cidr] if tunnel.key?(:key) connection_config["tunnel#{i + 1}_preshared_key"] = tunnel[:key] end end connection = resource :aws_vpn_connection, ident, connection_config cidrs.each do |cidr| resource :aws_vpn_connection_route, "#{ident}-#{tf_safe(cidr)}", destination_cidr_block: cidr, vpn_connection_id: connection end route_tables = [:subnets].map(&:route_table).sort.uniq route_tables.product(cidrs).each do |route_table, cidr| hash = Digest::SHA2.hexdigest "#{route_table}-#{tf_safe(cidr)}" resource :aws_route, "#{@name}-to-#{tf_safe(ip_address)}-peer-#{hash}", route_table_id: route_table, destination_cidr_block: cidr, gateway_id: vpn_gateway end end |