/
vpc_peering.rb
112 lines (97 loc) · 3.84 KB
/
vpc_peering.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
module Enscalator
module Templates
# VPC Peering connection between two VPCs
class VPCPeering < Enscalator::RichTemplateDSL
include Enscalator::Plugins::VPCPeeringConnection
# Retrieve local VPC configuration from provisioned stack
# @return [Aws::CloudFormation::Stack]
def local_vpc_stack
@local_vpc_stack ||= cfn_resource(cfn_client(region)).stack(vpc_stack_name)
end
# Interface for VPC
# @param [String] id logical id of VPC
# @return [Aws::EC2::Vpc]
def vpc(id: get_resource(local_vpc_stack, 'VPC'))
Aws::EC2::Vpc.new(id: id, region: region)
end
# 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
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 # tpl
end # class VPCPeering
end # module Plugins
end # module Enscalator