-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpc.py
284 lines (228 loc) · 7.36 KB
/
vpc.py
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
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
211
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
237
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
'''
AWS CloudFormation Template generator
This template describes a VPC and associated objects neccessary to run a
drift based stack.
To use:
python vpc.py > vpc.json
create stack:
aws cloudformation create-stack --stack-name roxxor-dev --template-body file://./vpc.json --parameters ParameterKey=VPCBaseNet,ParameterValue=10.22
or
cfn -c vpc.json -t -p VPCBaseNet=10.87 --region=eu-west-1 roxxor-dev
delete stack:
aws cloudformation delete-stack --stack-name roxxor-dev
info:
aws cloudformation describe-stack-events --stack-name roxxor-dev
'''
from troposphere import Join, Output, Parameter, Ref, Tags, Template, GetAZs, Select, GetAtt, Export, Sub
from troposphere import ec2, rds, elasticache, s3, elasticloadbalancing
def _(*args):
"""Join elements together to make a string. I.e. wrap all items in 'args'
into a Fn::Join template function.
"""
return Join("", list(args))
def get_resource_name(resource_name):
"""Returns a templatizable name with tier name and 'postfix' added together."""
return _(get_tier(), "-", resource_name)
def get_tier():
"""Returns a templatizable name of the tier."""
return Ref("AWS::StackName") # Tier name is currently the same as stack name.
class TierTags(Tags):
"""Enforces 'Name' to be set to 'tier_name' + resource_name,
and adds tier='tier_name' to tag set.
"""
def __init__(self, resource_name, **kwargs):
kwargs["Name"] = get_resource_name(resource_name)
kwargs["tier"] = get_tier()
return super(TierTags, self).__init__(**kwargs)
t = Template()
t.add_version("2010-09-09")
t.add_description("""\
AWS VPC with VPN access.
This template describes a VPC and associated objects neccessary to run a
drift based stack.
Objects:
* VPC
* Subnets
* Route Tables
* Internet Gateways
* NAT Gateways
* Security Groups
* Network ACLs
Servers:
* Redis
* Postgres
""")
# The VPC base net is e.g. 10.85.x.x
vpc_base_net = t.add_parameter(Parameter(
"VPCBaseNet",
ConstraintDescription=(
"must be a valid first two IP numbers of the form x.x"),
Description="The first two IP numbers for the VPC CIDR.",
Default="10.1",
MinLength="4",
AllowedPattern="(\d{1,3})\.(\d{1,3})",
MaxLength="18",
Type="String",
))
# The VPC object
vpc = t.add_resource(ec2.VPC(
"VPC",
CidrBlock=_(Ref(vpc_base_net), ".0.0/16"),
EnableDnsSupport="true",
EnableDnsHostnames="true",
Tags=TierTags("vpc", created_by=Ref("AWS::AccountId"))
))
# Each VPC has two public subnet, two private subnets and two RDS subnets, covering
# two availability zones.
# x.x.0.x and x.x.10.x are public subnets
# x.x.1.x and x.x.2.x are private subnets
# x.x.91.x and x.x.92.x are RDS subnets
def add_subnet(tag_name, ip_part, route_table, az, realm):
"""Add a subnet named 'tag_name' with 'ip_part as the /24 domain and
associated with 'route_table'. 'az' is the index into list of availability
zones, i.e. "0", "1" or "2".
Export the subnet ID.
"""
template_name = tag_name.title().replace('-', '')
subnet = ec2.Subnet(
template_name,
VpcId=Ref("VPC"),
CidrBlock=_(Ref(vpc_base_net), ".{}.0/24".format(ip_part)),
AvailabilityZone=Select(az, GetAZs()),
Tags=TierTags(tag_name, realm=realm)
)
subnet = t.add_resource(subnet)
t.add_output([
Output(
template_name,
Description="The ID of this subnet.",
Value=Ref(subnet),
Export=Export(_(Sub("${AWS::StackName}-"), tag_name)),
)
])
t.add_resource(ec2.SubnetRouteTableAssociation(
"{}RouteTableAssociation".format(template_name),
SubnetId=Ref(subnet),
RouteTableId=Ref(route_table)
))
return subnet
# One public route table and one private route table
public_rtbl = t.add_resource(ec2.RouteTable(
"PublicRouteTable",
VpcId=Ref("VPC"),
Tags=TierTags("rtbl-internet")
))
private_rtbl = t.add_resource(ec2.RouteTable(
"PrivateRouteTable",
VpcId=Ref("VPC"),
Tags=TierTags("rtbl-private")
))
# The public and private subnets
public_subnet_1 = add_subnet("public-subnet-1", "21", public_rtbl, "0", 'public')
public_subnet_2 = add_subnet("public-subnet-2", "22", public_rtbl, "1", 'public')
private_subnet_1 = add_subnet("private-subnet-1", "1", private_rtbl, "0", 'private')
private_subnet_2 = add_subnet("private-subnet-2", "2", private_rtbl, "1", 'private')
# Managed DB's get their own subnets, referenced through a special DB subnet group
db_subnet_1 = add_subnet("db-subnet-1", "91", private_rtbl, "0", 'db')
db_subnet_2 = add_subnet("db-subnet-2", "92", private_rtbl, "1", 'db')
db_subnet_group = t.add_resource(rds.DBSubnetGroup(
"DBSubnetGroup",
DBSubnetGroupDescription=get_resource_name("db-subnetgroup-desc"),
SubnetIds=[Ref(db_subnet_1), Ref(db_subnet_2)],
Tags=TierTags("db-subnetgroup")
))
# The public route table need an Internet gateway
t.add_resource(ec2.InternetGateway(
"InternetGateway",
Tags=TierTags("internet-gateway")
))
t.add_resource(ec2.VPCGatewayAttachment(
"InternetGatewayAttachment",
InternetGatewayId=Ref("InternetGateway"),
VpcId=Ref("VPC"),
DependsOn="InternetGateway"
))
t.add_resource(ec2.Route(
"IGWRoute",
GatewayId=Ref("InternetGateway"),
DestinationCidrBlock="0.0.0.0/0",
RouteTableId=Ref("PublicRouteTable"),
))
# NAT Gateway for private subnets
nat_eip = t.add_resource(ec2.EIP(
'NatEip',
Domain="vpc",
))
nat = t.add_resource(ec2.NatGateway(
'NatGateway',
AllocationId=GetAtt(nat_eip, 'AllocationId'),
SubnetId=Ref(public_subnet_1),
))
t.add_resource(ec2.Route(
'NatRoute',
RouteTableId=Ref(private_rtbl),
DestinationCidrBlock='0.0.0.0/0',
NatGatewayId=Ref(nat),
))
# Set up general security groups for the VPC and some specific ones for the
# following servers:
# - VPN server.
# - RabbitMQ server.
# - Redis server.
# - Postgres server.
# A catch-all group allowing all traffic from 10.x.x.x private network.
private_sg = t.add_resource(ec2.SecurityGroup(
"PrivateSecurityGroup",
VpcId=Ref("VPC"),
Tags=TierTags("private-sg"),
GroupDescription="Allow all traffic on 10.x.x.x",
SecurityGroupIngress=[
ec2.SecurityGroupRule(
IpProtocol="-1",
FromPort="-1",
ToPort="-1",
CidrIp="10.0.0.0/8",
),
],
))
# Allow incoming HTTPS traffic from any remote address.
t.add_resource(ec2.SecurityGroup(
"HTTPSSecurityGroup",
VpcId=Ref("VPC"),
Tags=TierTags("https-sg"),
GroupDescription="Allow incoming HTTPS traffic from any remote address.",
SecurityGroupIngress=[
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort="443",
ToPort="443",
CidrIp="0.0.0.0/0",
),
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort="80",
ToPort="80",
CidrIp="0.0.0.0/0",
),
],
))
t.add_output([
Output(
"VPCId",
Description="VPCId of the newly created VPC.",
Value=Ref(vpc),
),
Output(
"VPCBaseNet",
Description="VPCId of the newly created VPC.",
Value=Ref("VPCBaseNet"),
),
Output(
"StackVPC",
Description="The ID of the VPC.",
Value=Ref(vpc),
Export=Export(Sub("${AWS::StackName}-VPCID")),
),
])
print t.to_json()
#print t.to_yaml()