How to add more Subnets in existing AWS VPC
This article take us to the next level. Our previous articles shows how to create vpc/igw/subnet/routing table etc.. Article 1 AWS with Python
What if , we have 1000-2000 SVIs/Subnets created in our Core switches and similar setup is now required in AWS VPC. That's the one of goal to migrate your workload in AWS. It's a time consuming as well as boring task to most of us. Clicking every time for each subnet and associate them in their routing table in AWS, Not a very interesting one.
We can make it simple with Python script and error free too. We have taken few subnets here and someone can add more subnet in the csv file as shown below. This is quite simple file with subnet , availability zone and router ID details. routing id was taken from routing table which created earlier. You can see we have two routing id here for two different routing tables. First routing table ending with xxx225d8 and other one is 457b38.
We have divided our subnets in different AZ/router tables so each subnet get created to different location and providing subnet distribution in a easier way.
Here is script for finishing this task with few challenges explanation in the last.
import boto3,csv from botocore.exceptions import ClientError region_name = 'us-east-1' ## Function to Get VPC ID. def getVpc(resource='ec2',region_name='NONE',cidr='None'): print(f"\nGetting VPC from Region {region_name}") client = boto3.client(resource,region_name) resp = client.describe_vpcs() # print(resp['Vpcs'][0]['CidrBlock']) if cidr in resp['Vpcs'][0]['CidrBlock']: vpcID = resp['Vpcs'][0]['VpcId'] return vpcID else: print(f"CIDR Details not found {cidr}") ## Function to Create Subnet and associate with Routing table. This also find if ## same subnet already exist or ## new subnet creation overlapped with exist one. This is handled with Try/except ##ClientError def createsubnet_rt(region_name,az_name,subnetcidr,vpcid, resource='ec2',igwID='igwid',rtID='Default'): client = boto3.client(resource,region_name) subnet_list = [] for i in client.describe_subnets()['Subnets']: subnet_list.append(i['CidrBlock']) if subnetcidr not in subnet_list: try: resp = client.create_subnet(AvailabilityZone=az_name, CidrBlock=subnetcidr,VpcId=vpcid) subnetID = resp['Subnet']['SubnetId'] client.associate_route_table(RouteTableId=rtID,SubnetId=subnetID) print (f"\nCreating Subnets {subnetcidr} in {az_name} and region is { region_name} ") print(f"\nAssociating Subnets {subnetcidr} in Routing Table for the AZ {az_name}, Our Region is {region_name}") print (f"\nSubnet {subnetcidr} created and associated in Routing Table {rtID}") except ClientError as e: print (f"\nCreating Subnets {subnetcidr} in {az_name} and region is {region_name} ") print (f"Provided Subnet {subnetcidr} is overlappend subnet. {e}") else: print (f"\n This Subnet {subnetcidr} already exist, Not creating again.") print("*" * 100) ## Calling both function ... getVpcID = getVpc(region_name=region_name,cidr='172.172.0.0/16') with open('subnetfile.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') csv_reader = list(csv_reader) for row in csv_reader[1:]: subnet_id = row[0] az_name = row[1] rt_id = row[2] createsubnet_rt(subnetcidr=subnet_id, vpcid=getVpcID,region_name = region_name,az_name=az_name, igwID='igwid',rtID = rt_id)
Output after running the scripts.
Here is now our two challenge, 1) If we have Duplicate subnet entered in the table .
for example , 172.172.1.0/24 and 2.0/24 was already created.
Scripts ignore if subnet is already created , message will be provided with subnet already exists and move to the next subnet creation
2nd Issue If new subnet is already part of some other subnet. like here 172.172.50.0/22
covering first two which means it's conflict.
Scripts handle both situation via existing subnet check and invalid subnet conflict with try/except and avoid creating the overlapping subnets. User is now aware and can fix it in the file and run the script again for overlapping subnets.
Output if Overlapping subnet conflict found. Check the last message for 172.172.50.0/22
That' All for now... Thank you for viewing this article and please feel free to share it.
Stay tune for next article...