50 lines
1.6 KiB
Python
Executable File
50 lines
1.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
r"""
|
|
Documentation
|
|
|
|
License: This program is released under the MIT License
|
|
"""
|
|
|
|
# Imports
|
|
import boto3
|
|
import concurrent.futures
|
|
from openpyxl import load_workbook
|
|
from openpyxl.worksheet.worksheet import Worksheet
|
|
import projectlib.aws
|
|
|
|
|
|
def getResources(region_name: str) -> list[list[str | int]]:
|
|
return_data = []
|
|
client = boto3.client('ec2', region_name=region_name)
|
|
response = client.describe_instances()
|
|
for r in response['Reservations']:
|
|
for i in r['Instances']:
|
|
name_tag = 'Unset'
|
|
for tag in i['Tags']:
|
|
if tag['Key'] == "Name":
|
|
name_tag = tag['Value']
|
|
break
|
|
return_data.append([i['InstanceId'], name_tag, i.get('PlatformDetails'), i['InstanceType'], i['PrivateIpAddress'], i['Placement']['AvailabilityZone']])
|
|
return return_data
|
|
|
|
# Main function
|
|
def main() -> None:
|
|
# Open spreadsheet and add a sheet
|
|
wb = load_workbook('aws-inventory.xlsx')
|
|
ws = wb.create_sheet("Ec2")
|
|
|
|
ws.append(["InstanceId","NameTag","Platform","InstanceType","PrivateIpAddress","AZ"])
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
|
|
results = executor.map(getResources, projectlib.aws.getRegions())
|
|
for region_rows in results:
|
|
# append to worksheet only if resoruces are found in the region
|
|
if region_rows:
|
|
for row in region_rows:
|
|
ws.append(row)
|
|
|
|
wb.save('aws-inventory.xlsx')
|
|
|
|
# Call main function
|
|
if __name__ == '__main__':
|
|
main()
|