-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_default_vpcs.py
More file actions
executable file
·74 lines (57 loc) · 2.27 KB
/
Copy pathcheck_default_vpcs.py
File metadata and controls
executable file
·74 lines (57 loc) · 2.27 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""Checks whether the default VPC is in use across all regions."""
import argparse
import boto3
def check_for_enis(ec2, vpcid):
"""
Checks if there are any Elastic Network Interfaces (ENIs) associated with
the specified VPC.
Args:
ec2 (boto3.client): The Boto3 EC2 client used to interact with AWS EC2 resources.
vpcid (str): The ID of the VPC to check for ENIs.
Returns:
bool: True if there are ENIs associated with the VPC, False otherwise.
"""
enis = ec2.describe_network_interfaces(
Filters=[{'Name': 'vpc-id', 'Values': [vpcid]}]
)['NetworkInterfaces']
return len(enis) > 0
def main():
"""
Checks all AWS regions for default VPCs using the specified AWS CLI profile,
and reports whether each default VPC is in use.
Args:
-p, --profile (str): The AWS CLI profile name to use for authentication.
Behavior:
- Iterates through all AWS regions.
- For each region, finds default VPCs.
- Checks if each default VPC is in use by calling `check_for_enis`.
- Prints the usage status of each default VPC.
"""
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--profile',
help="The aws cli profile to use")
arg = parser.parse_args()
session = boto3.Session(profile_name=arg.profile)
client = session.client('ec2')
regions = client.describe_regions()['Regions']
for region in regions:
ec2 = session.client('ec2', region_name=region["RegionName"])
vpcs = ec2.describe_vpcs()["Vpcs"]
if not vpcs:
print(f"No VPC's found in region {region['RegionName']}.")
continue
vpc_count = 0
for vpc in vpcs:
if vpc["IsDefault"] is True:
vpcid = vpc["VpcId"]
if check_for_enis(ec2, vpcid):
print(f"Default VPC {vpcid} is in use in region {region['RegionName']}.")
else:
print(f"Default VPC {vpcid} is not in use in region {region['RegionName']}.")
else:
vpc_count += 1
if vpc_count == 1:
print(f"No default VPC found in region {region['RegionName']}")
if __name__ == "__main__":
main()