def subnet(function, credentials, unused_resource_count, region_list):
    print('Scanning Subnets')
    subnet_count = 0
    unused_subnet_detail = []
    for region in region_list:
        try:
            ec2 = boto3.resource('ec2', aws_access_key_id=credentials['access_key'], aws_secret_access_key=credentials['secret_key'], region_name=region)
            subnets = list(ec2.subnets.all())
            network_interfaces = list(ec2.network_interfaces.all())
            all_subnets = set([subnet.subnet_id for subnet in subnets])
            all_active_subnets = set([subnet['SubnetId'] for ni in network_interfaces for subnet in ni.subnet])
            unused_subnets = (all_subnets - all_active_subnets)
            for subnetid in unused_subnets:
                unused_subnet_detail.append({'ResourceType': 'AWS::EC2::Subnet', 'ResourceId': subnetid, 'Region': region})
            subnet_count += len(unused_subnets)
        except:
            pass
    if subnet_count:
        unused_subnet_detail = json.loads(json.dumps(unused_subnet_detail))
        f = csv.writer(open('./aws_logs/subnet.csv', 'w', newline=''))
        f.writerow(['ResourceType', 'ResourceId', 'Region'])
        for unused_subnet_detail in unused_subnet_detail:
            f.writerow([unused_subnet_detail['ResourceType'], unused_subnet_detail['ResourceId'], unused_subnet_detail['Region']])
    unused_resource_count[function] = subnet_count
def subnet(function, credentials, unused_resource_count, region_list):
    print('Scanning Subnets')
    subnet_count = 0
    unused_subnet_detail = []
    for region in region_list:
        try:
            ec2 = boto3.resource('ec2', aws_access_key_id=credentials['access_key'], aws_secret_access_key=credentials['secret_key'], region_name=region)
            subnets = list(ec2.subnets.all())
            network_interfaces = list(ec2.network_interfaces.all())
            all_subnets = set([subnet.subnet_id for subnet in subnets])
            all_active_subnets = set([subnet['SubnetId'] for ni in network_interfaces for subnet in ni.subnet])
            unused_subnets = (all_subnets - all_active_subnets)
            for subnetid in unused_subnets:
                unused_subnet_detail.append({'ResourceType': 'AWS::EC2::Subnet', 'ResourceId': subnetid, 'Region': region})
            subnet_count += len(unused_subnets)
        except:
            pass
    if subnet_count:
        unused_subnet_detail = json.loads(json.dumps(unused_subnet_detail))
        f = csv.writer(open('./aws_logs/subnet.csv', 'w', newline=))
        f.writerow(['ResourceType', 'ResourceId', 'Region'])
        for unused_subnet_detail in unused_subnet_detail:
            f.writerow([unused_subnet_detail['ResourceType'], unused_subnet_detail['ResourceId'], unused_subnet_detail['Region']])
    unused_resource_count[function] = subnet_count