1.Script to generate Amazon EC2 Inventory report
import boto3
from datetime import datetime
import csv
import argparse
import time
import os
import sys
import logging
import time
region_name = 'us-east-1'
DEFAULT_REGION = "us-east-1"
def parse_commandline_arguments():
global REGION
global ACCOUNT_ID
global report_filename
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Create a CSV Report for listing all EC2 for a given account and region')
parser.add_argument("-id", "--accountID", dest="account_id", type=str,required=True,
help="The AWS Account Name for which the EC2 info is neeeded")
parser.add_argument("-r", "--region", dest="region", type=str,
default=DEFAULT_REGION, help="Specify the global region to pull the report")
parser.add_argument("-f", "--report", dest="reportname", type=str,
help="Specify the report file Name with path")
args = parser.parse_args()
ACCOUNT_ID= args.account_id
REGION = args.region
report_filename = args.reportname
logger = logging.getLogger()
logger.setLevel(logging.INFO)
today = datetime.now()
date_time_now = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
def date_on_filename(filename, file_extension):
from datetime import datetime
date = str(datetime.now().date())
return filename + "-" + date + "." + file_extension
def generate_ec2_report():
client = boto3.client('ec2')
for attempt in range(5):
try:
response = client.describe_instances()
except BaseException as err:
logger.error(err)
logger.info("*** ERROR *** while calling describe instances API - retry...")
time.sleep(0.5)
else:
logger.info("--> Done")
break
else:
logger.error("*** ERROR *** - All attempt to call describe instance API failed - exit with error")
raise Exception("*** ERROR *** - Can't get EC2 Data")
owner_id = reservation_id = instance_id = state = image_id = instance_type = network_interfaces = ""
vpc_id = launch_time = private_ip = private_dns_name = platform = public_dns_name = subnet_id = ""
name = keep_until = managed_by = tag_qty = age = Last_Backup_Dt = backup_flag = reboot_schedule = ""
print("*********************************************************")
count_total = count_old = oldest = 0
for key1, value1 in response.items():
if key1 == "Reservations":
for object_a in value1:
for key2, value2 in object_a.items():
if key2 == "OwnerId":
owner_id = value2
if key2 == "ReservationId":
reservation_id = value2
if key2 == "Instances":
instances = value2
for object_b in instances:
for key3, value3 in object_b.items():
if key3 == "State":
state = value3['Name']
if key3 == "NetworkInterfaces":
network_interfaces = str(len(value3))
if key3 == "ImageId":
image_id = value3
if key3 == "InstanceType":
instance_type = value3
if key3 == "VpcId":
vpc_id = value3
if key3 == "InstanceId":
instance_id = value3
if key3 == "LaunchTime":
launch_time = str(value3)
launch_time_date = datetime.strptime(
launch_time, "%Y-%m-%d %H:%M:%S+00:00")
delta = today - launch_time_date
agestring = str(delta).split(" ")
age = str(agestring[0])
if len(agestring) != 3: # fix for ages less than 24 hours
age = str(0)
count_total += 1
if int(age) > 30:
count_old += 1
if int(age) > oldest:
oldest = int(age)
if key3 == "SubnetId":
subnet_id = value3
if key3 == "PrivateIpAddress":
private_ip = value3
if key3 == "PrivateDnsName":
private_dns_name = value3
if key3 == "PublicDnsName":
public_dns_name = value3
if key3 == "PlatformDetails":
OS_info = value3
if key3 == "Tags":
name = keep_until = managed_by = tag_qty = Last_Backup_Dt = backup_flag = reboot_schedule = ""
for dictionary in value3:
key4 = dictionary['Key']
value4 = dictionary['Value']
if key4 == 'Name':
name = '"' + value4 + '"'
if key4 == 'KeepUntil':
keep_until = '"' + value4 + '"'
if key4 == 'ManagedBy':
managed_by = '"' + value4 + '"'
if key4 == 'LastAMIBackup':
Last_Backup_Dt = '"' + value4 + '"'
if key4 == 'AMIBackup':
backup_flag = '"' + value4 + '"'
if key4 == 'Schedule':
reboot_schedule = '"' + value4 + '"'
tag_qty = str(len(value3))
print_string = ACCOUNT_ID + "," + REGION + "," + reservation_id + "," + instance_id + "," + name + "," + \
state + "," + image_id + "," + OS_info + "," + instance_type + "," + vpc_id + "," + launch_time + "," + \
age + "," + network_interfaces + "," + subnet_id + "," + private_ip + "," + private_dns_name + "," + \
public_dns_name + "," + keep_until + "," + managed_by + "," + Last_Backup_Dt + "," + backup_flag + "," + reboot_schedule + "," + tag_qty + "," + date_time_now
file.write(print_string + "\n")
file.close()
print("CSV File generated.... - {}" .format(report_filename))
if __name__ == '__main__':
try:
parse_commandline_arguments()
if not os.path.isfile(report_filename):
file = open(report_filename, 'w+')
print_string_hdr = "Account-ID,Region,Reservation_id,Istance_id,Host-Name,State,Image_id,Platform,Instance_type,VpcID,Launch_Time,Age,Network_interfaces,Subnet_ID,Private_IP,Private_dns_name,Public_dns_name,Keep_until,Managed_by,Last_Backup_Dt,Backup_flag,Reboot_Schedule,No_Of_Tags,Reporting_Date_time\n"
file.write(print_string_hdr)
else:
file = open(report_filename, 'a')
generate_ec2_report()
except Exception as error:
print(str(error))
2.Script to generate Amazon RDS Inventory report
#!/usr/bin/env python
import boto3
import json
import csv
import argparse
import os
from datetime import datetime, timedelta, timezone
import logging
from botocore.exceptions import ClientError
DEFAULT_REGION="us-east-1"
date_time_now = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
DBIdentifier = DBInstanceClass = DBEngine = DBInstanceStatus = DBEndpointAddress = DBEndpointPort = DBMultiAZ = DBEngineVersion = DBLicenseModel = DBStorageEncrypted = ""
def rds_client(region):
"""
Connects to EC2, returns a connection object
"""
try:
conn = boto3.client('rds', region_name=region)
except Exception as e:
sys.stderr.write(
'Could not connect to region: %s. Exception: %s\n' % (region, e))
conn = None
return conn
def parse_commandline_arguments():
global REGION
global ACCOUNT_ID
global report_filename
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Create a CSV Report for RDS Reporting.')
parser.add_argument("-id", "--accountID", dest="account_id", type=str,required=True,
help="The AWS Account Name for which the RDS info is neeeded")
parser.add_argument("-r", "--region", dest="region", type=str,
default=DEFAULT_REGION, help="Specify the global region to pull the report")
parser.add_argument("-f", "--report", dest="reportname", type=str,
help="Specify the report file Name with path")
args = parser.parse_args()
ACCOUNT_ID= args.account_id
REGION = args.region
report_filename = args.reportname
def getrdsinfo(rds):
try:
# get all of the db instances
dbs = rds.describe_db_instances()
for db in dbs['DBInstances']:
#print(db)
DBIdentifier = db['DBInstanceIdentifier']
DBInstanceClass = db['DBInstanceClass']
DBEngine = db['Engine']
DBInstanceStatus = db['DBInstanceStatus']
DBEndpointAddress = db['Endpoint']['Address']
DBEndpointPort = db['Endpoint']['Port']
DBMultiAZ = db['MultiAZ']
DBEngineVersion = db['EngineVersion']
DBLicenseModel = db['LicenseModel']
DBStorageEncrypted = db['StorageEncrypted']
DBMaxStorage = db['MaxAllocatedStorage']
print_string = ACCOUNT_ID + "," + REGION + "," + DBIdentifier + "," + DBInstanceClass + "," + DBEngine + "," + DBInstanceStatus + "," + DBEndpointAddress + "," + str(DBEndpointPort) + "," + str(DBMultiAZ) + "," + \
DBEngineVersion + "," + DBLicenseModel + "," + str(DBStorageEncrypted) + "," + str(DBMaxStorage) + "," + date_time_now
file.write(print_string + "\n")
except Exception as e:
logging.error(e)
if __name__ == '__main__':
try:
parse_commandline_arguments()
client = rds_client(REGION)
if not os.path.isfile(report_filename):
file = open(report_filename, 'w+')
print_string_hdr = "AccountID,Region,DBIdentifier,DBInstanceClass,DBEngine,DBInstanceStatus,DBEndpointAddress,DBEndpointPort,DBMultiAZ,DBEngineVersion,DBLicenseModel,DBStorageEncrypted,MaxStorageAllocated,Reporting_Date_Time\n"
file.write(print_string_hdr)
else:
file = open(report_filename, 'a')
getrdsinfo(client)
file.close()
print("CSV File generated.... - {}" .format(report_filename))
except Exception as error:
print(str(error))
How to run the script:
Make sure you have python/python3 installed, configured and is in path. Script takes 3 arguments.
- AWS Account ID for which you need to get the list of EC2
- AWS Region
- & CSV file with complete path that contains all the data you are looking for.
Usage: python <script_name> -id <AWS Account ID> -r <AWS Region> -f <CSV file name with path>
e.g. if the script name is – getEC2Inventory.py, you can run the script as
$ python getEC2Inventory.py -id 123456789 -r us-east-1 -f /tmp/getEC2List.sv
More scripts to come… & happy learning !!!
~Anand M
Leave a Reply