Created
August 26, 2022 08:14
-
-
Save legovaer/0c08acfe0809e14e2293fcb108003686 to your computer and use it in GitHub Desktop.
Python script that collects data about your AWS VPCs. This can be used to determine if you have VPCs that are no longer used.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import sys | |
import csv | |
import ipaddress | |
def describe_regions(session): | |
try: | |
aws_regions = [] | |
ec2_client = session.client('ec2') | |
response_regions = ec2_client.describe_regions()['Regions'] | |
for region in response_regions: | |
aws_regions.append(region['RegionName']) | |
return aws_regions | |
except Exception: | |
print("Unexpected error:", sys.exc_info()[0]) | |
def describe_vpc(ec2,aws_region,writer): | |
try: | |
response_vpc = ec2.describe_vpcs()['Vpcs'] | |
for vpc in response_vpc: | |
print('=' * 50) | |
count = 0 | |
filters = [ | |
{'Name': 'vpc-id', | |
'Values': [vpc['VpcId']]} | |
] | |
response_subnets = ec2.describe_subnets(Filters=filters)['Subnets'] | |
for subnets in response_subnets: | |
count += 1 | |
total_count = (ipaddress.ip_network(subnets['CidrBlock']).num_addresses) - 5 | |
Used_IP = total_count - subnets['AvailableIpAddressCount'] | |
writer.writerow({"VpcId": vpc['VpcId'], "VpcCidr": vpc['CidrBlock'], "Region": aws_region, | |
"Subnet": subnets['CidrBlock'], "SubnetId": subnets['SubnetId'], "AvailableIPv4": subnets['AvailableIpAddressCount'], "Total_Network_IP": str(total_count), | |
"AvailabilityZone": subnets['AvailabilityZone'],"Used_IP": str(Used_IP)}) | |
print({"VpcId": vpc['VpcId'], "VpcCidr": vpc['CidrBlock'], "Region": aws_region, | |
"Subnet": subnets['CidrBlock'], "SubnetId": subnets['SubnetId'], "AvailableIPv4": subnets['AvailableIpAddressCount'], "Total_Network_IP": str(total_count), | |
"AvailabilityZone": subnets['AvailabilityZone'],"Used_IP": str(Used_IP)}) | |
print('='*50) | |
except Exception: | |
print("Unexpected error:", sys.exc_info()[0]) | |
def main(): | |
try: | |
session = boto3.session.Session() | |
file_name = "results" | |
print("File Name: " +file_name) | |
with open(file_name + ".csv", "w", newline="") as csvfile: | |
fieldnames = [ | |
"Account", "VpcId", | |
"VpcCidr", "Region", | |
"Subnet", "SubnetId", | |
"AvailableIPv4","Total_Network_IP", | |
"AvailabilityZone","Used_IP" | |
] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
aws_regions = describe_regions(session) | |
for aws_region in aws_regions: | |
ec2 = session.client('ec2', region_name=aws_region) | |
print("Scanning region: {}".format(aws_region)) | |
describe_vpc(ec2,aws_region, writer) | |
except Exception: | |
print("Unexpected error:", sys.exc_info()[0]) | |
raise | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment