-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubnets_Creation.py
More file actions
52 lines (35 loc) · 1.44 KB
/
Subnets_Creation.py
File metadata and controls
52 lines (35 loc) · 1.44 KB
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
from Taging import attach_tag
def create_subnets(client,CidrBlock,availability_zone,VpcId,subnet_name,IsPublic):
# check whether sunet is already created or not
SubnetId = find_subnet(client,VpcId,CidrBlock,availability_zone,subnet_name)
if SubnetId is None:
response = client.create_subnet(
AvailabilityZone=availability_zone,
CidrBlock=CidrBlock,
VpcId=VpcId
)
SubnetId = response['Subnet']['SubnetId']
if IsPublic == 'Y' :
response = client.modify_subnet_attribute(
MapPublicIpOnLaunch={
'Value': True
},
SubnetId=SubnetId
)
attach_tag(client,SubnetId,subnet_name)
print('Subnet is created with ID: ',SubnetId)
return SubnetId
else:
return SubnetId
def find_subnet(client,VpcId,CidrBlock,availability_zone,subnet_name):
filters = [
{'Name': 'tag:Name', 'Values': [subnet_name]},
{'Name' : 'vpc-id' , 'Values' : [VpcId]},
{'Name': 'cidr-block' , 'Values' : [CidrBlock]},
{'Name': 'availability-zone' , 'Values' : [availability_zone]}
]
response = client.describe_subnets(Filters=filters)
if len(response['Subnets']) > 0:
return response['Subnets'][0]['SubnetId']
else:
return None