-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_EC2.py
More file actions
50 lines (40 loc) · 1.66 KB
/
list_EC2.py
File metadata and controls
50 lines (40 loc) · 1.66 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
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_running_instances(region):
# Initialize a session using your credentials
session = boto3.Session(region_name=region)
# Initialize the EC2 client
ec2 = session.client('ec2')
# Fetch all instances that are currently running
response = ec2.describe_instances()
instances = []
for reservation in response['Reservations']:
for instance in reservation['Instances']:
# You can add more details as needed
instance_info = {
'Region': region,
'Instance ID': instance['InstanceId'],
'Instance Type': instance['InstanceType'],
'Public IP': instance.get('PublicIpAddress')
}
instances.append(instance_info)
return instances
# Specify your region (e.g., 'us-east-1')
regions = list_aws_regions()
# Print table head
print("| Region | Instance ID | Instance Type | Public IP |")
print("|---------|-------------------|---------------|-----------|")
# Iterate over regions
for region in regions:
running_instances = list_running_instances(region)
if running_instances:
for instance in running_instances:
# Print table row
print(f"| {instance['Region']} | {instance['Instance ID']} | {instance['Instance Type']} | {instance['Public IP']}|")