-
-
Save emir-munoz/47d25faabe8a9df56076a7e43b6d84eb to your computer and use it in GitHub Desktop.
A basic boto3 based tool for retrieving information from running EC2 instances.
This file contains 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
from collections import defaultdict | |
import boto3 | |
""" | |
A tool for retrieving basic information from the running EC2 instances. | |
""" | |
# Connect to EC2 | |
ec2 = boto3.resource('ec2') | |
# Get information for all running instances | |
running_instances = ec2.instances.filter(Filters=[{ | |
'Name': 'instance-state-name', | |
'Values': ['running']}]) | |
ec2info = defaultdict() | |
for instance in running_instances: | |
for tag in instance.tags: | |
if 'Name'in tag['Key']: | |
name = tag['Value'] | |
# Add instance info to a dictionary | |
ec2info[instance.id] = { | |
'Name': name, | |
'Type': instance.instance_type, | |
'State': instance.state['Name'], | |
'Private IP': instance.private_ip_address, | |
'Public IP': instance.public_ip_address, | |
'Launch Time': instance.launch_time | |
} | |
attributes = ['Name', 'Type', 'State', 'Private IP', 'Public IP', 'Launch Time'] | |
for instance_id, instance in ec2info.items(): | |
for key in attributes: | |
print("{0}: {1}".format(key, instance[key])) | |
print("------") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment