-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
287 lines (236 loc) · 7.87 KB
/
app.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
285
286
287
## Import libraries which complete execution
import boto3
import botocore
from ipaddress import ip_network
from itertools import cycle
import json
import os
import csv
from pprint import pprint
from tempfile import NamedTemporaryFile
unique_bucket = os.environ['unique_bucket']
## Invoke the boto3 ec2 clinet for AWS related calls
ec2 = boto3.client("ec2")
s3 = boto3.client("s3")
## Define the lambda handler using the standard default name
def lambda_handler(event, context):
print("The Whole message event")
print(event)
### variable to be sourced from event
messages = event['Records']
print("The Records dictionary from the whole message")
print(messages)
for message in messages :
messageData = {
'body': message['body']
}
print("The Element we have focused on for a variable input")
print(messageData)
vpc_size = messageData['body']
azs = ["eu-west-2a", "eu-west-2b", "eu-west-2c"]
if vpc_size == "small":
subnet_size = 27
number_of_subnets_to_use = 6
subnet_csv = 'example-small.csv'
elif vpc_size == "medium":
subnet_size = 26
number_of_subnets_to_use = 9
subnet_csv = 'example-medium.csv'
elif vpc_size == "large":
subnet_size = 25
number_of_subnets_to_use = 9
subnet_csv = 'example-large.csv'
else:
print("Incorrect VPC size defined")
exit(1)
obj = s3.get_object(Bucket=unique_bucket, Key=subnet_csv)
print(obj)
content = obj['Body'].read().decode('utf-8')
print(content)
s3.download_file(unique_bucket, subnet_csv, '/tmp/tmp-example.csv')
list_dir = os.listdir('/tmp')
print("List of tmp directory")
print(list_dir)
cidr_space =[]
with open('/tmp/tmp-example.csv', newline='', encoding='utf-8') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
i = 0
for row in csv_reader:
if i == 0:
index, network_supernet, allocated, b, c, d, e = row
print("Index 0 row cidr supernet with conditional loop to cidr_space")
print(str(network_supernet))
cidr_space.append(network_supernet)
i +=1
print("selected cidr_space extracted and stripped for consumption")
stripped_cidr = str(cidr_space).strip("[']")
print(stripped_cidr)
with open('/tmp/tmp-example.csv', newline='', encoding='utf-8') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader)
with open('/tmp/tmp-update.csv', 'w') as new_file:
csv_writer = csv.writer(new_file, delimiter=',')
for line in csv_reader:
csv_writer.writerow(line)
print("Load in CSV omit first 1 rows then recommit to S3")
print(csv_writer)
s3.upload_file('/tmp/tmp-update.csv', unique_bucket, subnet_csv )
### variable to be set by call to IPAM solution
supernet = stripped_cidr
all_subnets_in_supernet = list(ip_network(supernet).subnets(new_prefix=subnet_size))
allocated_subnets = list(
zip(all_subnets_in_supernet[:number_of_subnets_to_use], cycle(azs))
)
for subnet, region in allocated_subnets:
print(f"cidr: {subnet} for region: {region}")
cidr = str(subnet)
print(type(cidr))
print(cidr)
print(
f"\nremaining unallocated subnets from {supernet}: {all_subnets_in_supernet[number_of_subnets_to_use:]}"
)
### Creation of VPC Data
response = ec2.create_vpc(CidrBlock=supernet)
print(response)
vpcid = response["Vpc"]["VpcId"]
waiter = ec2.get_waiter('vpc_available')
waiter.wait(
Filters=[
{
'Name': 'state',
'Values': [
'available',
]
},
],
VpcIds=[vpcid
],
WaiterConfig={
'Delay' : 1,
'MaxAttempts': 20
}
)
print(waiter.wait)
### Creation of Subnets and building a subnetidlist with Subnet id references
subnetidlist=[]
for subnet, region in allocated_subnets:
print(f"cidr: {subnet} for region: {region}")
cidr = str(subnet)
net = ec2.create_subnet(
AvailabilityZone=region,
CidrBlock=cidr,
VpcId=vpcid,
)
print(cidr)
subid = net["Subnet"]["SubnetId"]
subnetidlist.append(subid)
print(subnetidlist)
print(
f"\nremaining unallocated subnets from {supernet}: {all_subnets_in_supernet[number_of_subnets_to_use:]}"
)
### Create NACL and moving subnets into the particular NACL entry
networkACL = ec2.create_network_acl( VpcId = vpcid )
print(networkACL)
networkACL_id = networkACL["NetworkAcl"]["NetworkAclId"]
print (networkACL_id)
### Create NACL rule entries for the above invoked networkACL id
networkACL_entry = ec2.create_network_acl_entry(
CidrBlock='10.0.0.0/8',
Egress=False,
NetworkAclId=networkACL_id,
PortRange={
'From': 53,
'To': 53
},
Protocol='17',
RuleAction='allow',
RuleNumber=100
)
networkACL_entry = ec2.create_network_acl_entry(
CidrBlock='10.0.0.0/8',
Egress=False,
NetworkAclId=networkACL_id,
PortRange={
'From': 22,
'To': 22
},
Protocol='6',
RuleAction='allow',
RuleNumber=110
)
networkACL_entry = ec2.create_network_acl_entry(
CidrBlock='10.0.0.0/8',
Egress=False,
NetworkAclId=networkACL_id,
PortRange={
'From': 443,
'To': 443
},
Protocol='6',
RuleAction='allow',
RuleNumber=120
)
networkACL_entry = ec2.create_network_acl_entry(
CidrBlock='10.0.0.0/8',
Egress=True,
NetworkAclId=networkACL_id,
PortRange={
'From': 1024,
'To': 65525
},
Protocol='6',
RuleAction='allow',
RuleNumber=120
)
### Get default ACL from Account
acl_response = ec2.describe_network_acls( NetworkAclIds=[], Filters=[] )
### List the associations by NetworkAclId
for acl in acl_response['NetworkAcls']:
for association in acl['Associations']:
print(association['NetworkAclId'])
id = (association['NetworkAclId'])
### Get association IDs for ofermentioned NetworkAcls, build a list with criteria
myAssociations = []
for acl in acl_response['NetworkAcls']:
if( acl["VpcId"] == vpcid and len( acl['Associations'] ) > 0 ):
myAssociations = acl['Associations']
print(myAssociations)
break
### Replace associations from default nacl based on the criteria to the new ACL
for a in myAssociations:
ec2.replace_network_acl_association(
AssociationId = a['NetworkAclAssociationId'],
NetworkAclId = networkACL['NetworkAcl']['NetworkAclId']
)
### Attach subnets to a non-default routing table
privaterttable = ec2.create_route_table(
DryRun=False,
VpcId=vpcid
)
privaterttableid = privaterttable['RouteTable']['RouteTableId']
### Creating a standard route table
j = 0
for id in range(len(subnetidlist)):
route_table_association = ec2.associate_route_table(
SubnetId=subnetidlist[j],
RouteTableId=privaterttableid)
j += 1
### Looping through subnetidlist and associating subnets to routing table
security_group = ec2.create_security_group(
Description='default-sg',
GroupName='default-sg',
VpcId=vpcid
)
print(security_group)
### Adding a default Security Group
# s3_gate_endpoint = ec2.create_vpc_endpoint(
# VpcEndpointType='Gateway',
# VpcId=vpcid,
# ServiceName='com.amazonaws.eu-west-2.s3',
# RouteTableIds=[
# privaterttableid,
# PrivateDnsEnabled=False,
# ],
# )
# print(s3_gate_endpoint)
### Adding Local S3 endpoint### Adding Local S3 endpoint