-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
162 lines (114 loc) · 4.38 KB
/
lambda_function.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
import json
import boto3
import xlrd
from xlrd.book import open_workbook_xls
import xlwt
import sys
from VPC_Creation import create_vpc
from DHCP_Creation import create_dhcp
from Subnets_Creation import create_subnets
from RouteTable_Creation import create_routetable
from InternetGateway_Creation import create_internetgateway
def lambda_handler(event, context):
# TODO implement
ec2Resource = boto3.resource('ec2')
ec2Client = boto3.client('ec2')
bucketName = 's3_bucket_name' # Update with your bucket name
filePath = 'vpc_detail.xls' # Update with your detials file name
workbook = read_xls(bucketName,filePath)
#Creating VPC
data = read_vpc_detail(workbook)
print('data: ' , data)
# DHCP Creation
try:
DhcpOptionsId = create_dhcp(ec2Client,data['DHCP'],'null')
print('Dhcp Options ID : ' , DhcpOptionsId)
except:
print('DHCP Creation Exception')
print(sys.exc_info())
# VPC Creaetion
try:
vpc_id = create_vpc(ec2Resource,ec2Client,data['CidrBlock'],data['vpc_name'],DhcpOptionsId)
print('VPC_ID: ', vpc_id)
except:
print('VPC Creation Exception')
print(sys.exc_info())
# Internet Gateway Creation
try :
InternetGatewayId = create_internetgateway(ec2Client,data['IG'],vpc_id)
print('Internet Gateway ID: ', InternetGatewayId)
except:
print('Internet Gateway Creation Exception')
# Public Route Table Creation
try:
RoutetableId = create_routetable(ec2Client,vpc_id,data['PRT'],InternetGatewayId,'Y')
print('Public Route table ID:' , RoutetableId )
except:
print('Public Route Table Creation Exception')
print(sys.exc_info())
# private Route Table Creation
try:
RoutetableId = create_routetable(ec2Client,vpc_id,data['PRRT'],InternetGatewayId,'N')
print('Private Route table ID:' , RoutetableId )
except:
print('Route Table Creation Exception')
print(sys.exc_info())
# Creating Subnets
subnets = subnet_creation_from_xls(workbook,ec2Client,vpc_id)
print(subnets)
return {
'body': 'Lambda Works!!!'
}
def read_xls(bucketName,filePath):
# Read File From S3 Bucket
s3Client = boto3.client('s3')
data = s3Client.get_object(Bucket=bucketName, Key=filePath)
content = data['Body'].read()
# Read xls File for API-Gateway Detail
workbook = open_workbook_xls(file_contents=content)
return workbook
def read_vpc_detail(workbook):
data = {}
sheet1 = workbook.sheet_by_index(0)
data['vpc_name'] = sheet1.cell_value(0,1)
data['CidrBlock'] = sheet1.cell_value(1,1)
data['IG'] = sheet1.cell_value(2,1)
data['PRT'] = sheet1.cell_value(3,1)
data['PRRT'] = sheet1.cell_value(4,1)
data['DHCP'] = sheet1.cell_value(5,1)
return data
def subnet_creation_from_xls(workbook,client,vpc_id):
sheet2 = workbook.sheet_by_index(1)
subnets = {}
#data = []
#row = []
index = 1
while(True):
if (sheet2.nrows == index):
break
#row.append(sheet2.cell_value(index,1))
#row.append(sheet2.cell_value(index,2))
# First Availability Zone
CidrBlock = sheet2.cell_value(index,1)
tag_name = sheet2.cell_value(index,2)
availability_zone = sheet2.cell_value(index,3)
IsPublic = sheet2.cell_value(index,7)
try :
subnet_id = create_subnets(client,CidrBlock,availability_zone,vpc_id,tag_name,IsPublic)
subnets[tag_name] = subnet_id
except :
print('Exception in subnet creation: ', tag_name)
print(sys.exc_info())
# Second Availability Zone
CidrBlock = sheet2.cell_value(index,4)
tag_name = sheet2.cell_value(index,5)
availability_zone = sheet2.cell_value(index,6)
IsPublic = sheet2.cell_value(index,7)
try :
subnet_id = create_subnets(client,CidrBlock,availability_zone,vpc_id,tag_name,IsPublic)
subnets[tag_name] = subnet_id
except:
print('Exception in subnet creation: ', tag_name)
print(sys.exc_info())
index = index + 1
return subnets