-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_VPC.py
More file actions
58 lines (43 loc) · 1.58 KB
/
list_VPC.py
File metadata and controls
58 lines (43 loc) · 1.58 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
import boto3
def list_aws_regions():
# Initialize the EC2 client with default region
ec2 = boto3.client('ec2')
# Retrieve all regions that work with EC2
response = ec2.describe_regions()
# Extract region names from the response
regions = [region['RegionName'] for region in response['Regions']]
return regions
def list_vpcs(region):
try:
# Initialize the EC2 client
ec2 = boto3.client('ec2', region_name=region)
# Retrieve list of VPCs
response = ec2.describe_vpcs()
# Extract VPC details
vpcs = response['Vpcs']
vpc_details = []
# Collect details for each VPC
for vpc in vpcs:
vpc_id = vpc['VpcId']
cidr_block = vpc['CidrBlock']
state = vpc['State']
default = vpc['IsDefault']
# Append details to the list
vpc_details.append([region, vpc_id, cidr_block, state, default])
return vpc_details
except Exception as e:
print(f"An error occurred: {e}")
# Call the function to list VPCs
regions = list_aws_regions()
# Print table head
print("| Region | VPC ID | CIDR | State | Default |")
print("|--------|----------------|----------------|----------|---------|")
# Iterate over regions
for region in regions:
vpc_details = list_vpcs(region)
if vpc_details:
for details in vpc_details:
# Print table row
print(f"| {details[0]} | {details[1]} | {details[2]} | {details[3]} | {details[4]} |")
else:
print("No VPCs found in this region.")