Class: Enscalator::Templates::VPCPeering

Inherits:
RichTemplateDSL show all
Includes:
Plugins::VPCPeeringConnection
Defined in:
lib/enscalator/templates/vpc_peering.rb

Overview

VPC Peering connection between two VPCs

Constant Summary

Constants inherited from RichTemplateDSL

RichTemplateDSL::TEMPLATE_BODY_LIMIT

Constants included from Plugins::Route53

Plugins::Route53::HEALTH_CHECK_TYPE, Plugins::Route53::RECORD_TYPE

Instance Method Summary collapse

Methods included from Plugins::VPCPeeringConnection

#parameter_vpc_id, #vpc_peering_init

Methods inherited from RichTemplateDSL

#availability_zones, #creating?, #deploy, #deployment_env, #description, #enqueue, #exec!, #handle_trailing_dot, #has_multiple_envs, #hosted_zone, #initialize, #network_interface, #parameter, #parse_params, #post_run, #pre_run, #private_hosted_zone, #public_hosted_zone, #read_availability_zones, #region, #resource, #tags_to_properties, #vpc_stack_name

Methods included from Plugins::Route53

#create_healthcheck, #create_multiple_dns_records, #create_private_hosted_zone, #create_public_hosted_zone, #create_single_dns_record

Methods included from Helpers

#cfn_call_script, #create_ssh_key, #find_ami, #flatten_hash, #gen_ssh_key_name, #init_assets_dir, #init_aws_config, #read_user_data, #run_cmd

Methods included from Helpers::Dns

#get_dns_records, #upsert_dns_record

Methods included from Helpers::Stack

#cfn_create_stack, #generate_parameters, #get_resource, #get_resources, #wait_stack

Methods included from Helpers::Wrappers

#cfn_client, #cfn_resource, #ec2_client, #route53_client

Methods included from Core::CfResources

#iam_instance_profile_with_full_access, #instance_vpc, #instance_with_network, #security_group, #security_group_vpc, #subnet

Methods included from Core::CfParameters

#parameter_allocated_storage, #parameter_ami, #parameter_ec2_instance_type, #parameter_instance_type, #parameter_key_name, #parameter_name, #parameter_password, #parameter_rds_instance_type, #parameter_username

Constructor Details

This class inherits a constructor from Enscalator::RichTemplateDSL

Instance Method Details

#local_vpc_stackAws::CloudFormation::Stack

Retrieve local VPC configuration from provisioned stack

Returns:

  • (Aws::CloudFormation::Stack)
[View source]

9
10
11
# File 'lib/enscalator/templates/vpc_peering.rb', line 9

def local_vpc_stack
  @local_vpc_stack ||= cfn_resource(cfn_client(region)).stack(vpc_stack_name)
end

#read_vpc_route_tables(vpc) ⇒ Object

[View source]

70
71
72
73
74
75
76
# File 'lib/enscalator/templates/vpc_peering.rb', line 70

def read_vpc_route_tables(vpc)
  routes = []
  vpc.route_tables.each do |rt|
    routes << rt
  end
  routes
end

#tplObject

VPC Peering connection can be created only when

  • both VPCs are in the same region

  • connected VPCs has distinct CIDR blocks

Route tables should be created in the following way:

VPC Local’s route table 172.16.0.0/16 -> Local 10.0.0.0/16 -> pcx-11112222

VPC Remote’s route table 10.0.0.0/16 Local 172.16.0.0/16 pcx-11112222

[View source]

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
# File 'lib/enscalator/templates/vpc_peering.rb', line 34

def tpl
  connection_name = 'PrivateConnection'
  local_vpc_id_ref, remote_vpc_id_ref = %W(#{connection_name}VpcId #{connection_name}PeerVpcId)

  def validate_params(*params)
    params.each do |param|
      fail "Unable to find required parameter #{param}" unless @parameters.key?(param)
    end
  rescue RuntimeError => e
    puts e
    exit 1
  end

  validate_params(*[remote_vpc_id_ref])

  local_vpc, remote_vpc = [vpc, vpc(id: @parameters[remote_vpc_id_ref])]

  description 'Stack to create peering connection between two VPCs'

  parameter_vpc_id(local_vpc_id_ref,
                   'VpcId from where connection gets created',
                   local_vpc.id)

  parameter_vpc_id(remote_vpc_id_ref,
                   'VpcId where peering connection should go',
                   remote_vpc.id)

  # Initialize Peering connection
  vpc_peering_init(connection_name,
                   tags: [
                     {
                       Key: 'Name',
                       Value: connection_name
                     }
                   ])

  def read_vpc_route_tables(vpc)
    routes = []
    vpc.route_tables.each do |rt|
      routes << rt
    end
    routes
  end

  # Add rules to local VPC routing table
  read_vpc_route_tables(local_vpc).map(&:id).each_with_index do |rt_id, i|
    local_vpc_route_rule = "LocalVPCPeeringRoute#{i + 1}"
    resource local_vpc_route_rule,
             Type: 'AWS::EC2::Route',
             Properties: {
               RouteTableId: rt_id,
               DestinationCidrBlock: remote_vpc.cidr_block,
               VpcPeeringConnectionId: ref(connection_name)
             }

    output local_vpc_route_rule,
           Description: "Local VPC Peering connection for #{rt_id}",
           Value: ref(local_vpc_route_rule)
  end

  # Add rules to remote VPC routing table
  read_vpc_route_tables(remote_vpc).map(&:id).each_with_index do |rt_id, i|
    remote_vpc_route_rule = "RemoteVPCPeeringRoute#{i + 1}"
    resource remote_vpc_route_rule,
             Type: 'AWS::EC2::Route',
             Properties: {
               RouteTableId: rt_id,
               DestinationCidrBlock: local_vpc.cidr_block,
               VpcPeeringConnectionId: ref(connection_name)
             }

    output remote_vpc_route_rule,
           Description: "Remote VPC Peering connection for #{rt_id}",
           Value: ref(remote_vpc_route_rule)
  end
end

#validate_params(*params) ⇒ Object

[View source]

38
39
40
41
42
43
44
45
# File 'lib/enscalator/templates/vpc_peering.rb', line 38

def validate_params(*params)
  params.each do |param|
    fail "Unable to find required parameter #{param}" unless @parameters.key?(param)
  end
rescue RuntimeError => e
  puts e
  exit 1
end

#vpc(id: get_resource(local_vpc_stack, 'VPC')) ⇒ Aws::EC2::Vpc

Interface for VPC

Parameters:

  • id (String) (defaults to: get_resource(local_vpc_stack, 'VPC'))

    logical id of VPC

Returns:

  • (Aws::EC2::Vpc)
[View source]

16
17
18
# File 'lib/enscalator/templates/vpc_peering.rb', line 16

def vpc(id: get_resource(local_vpc_stack, 'VPC'))
  Aws::EC2::Vpc.new(id: id, region: region)
end