From c6f9856de7d6ed8dfaa3cbe17c2a7caf799abbd5 Mon Sep 17 00:00:00 2001 From: KenF Date: Thu, 22 Jan 2026 09:50:30 +0800 Subject: [PATCH] feat: porting shell scripts to python --- aws-lambda-inventory.sh | 16 ---------------- aws-lambda.py | 33 +++++++++++++++++++++++++++++++++ aws-logs.py | 7 +++++-- aws-opensearch-inventory.sh | 16 ---------------- aws-opensearch.py | 32 ++++++++++++++++++++++++++++++++ aws-rds-inventory.sh | 16 ---------------- aws-rds.py | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 102 insertions(+), 50 deletions(-) delete mode 100755 aws-lambda-inventory.sh create mode 100755 aws-lambda.py delete mode 100755 aws-opensearch-inventory.sh create mode 100755 aws-opensearch.py delete mode 100755 aws-rds-inventory.sh create mode 100755 aws-rds.py diff --git a/aws-lambda-inventory.sh b/aws-lambda-inventory.sh deleted file mode 100755 index 4f02686..0000000 --- a/aws-lambda-inventory.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -function listRes() { - aws --region $1 lambda list-functions | jq -cr '.Functions[] | [.FunctionName, .Runtime, .Architectures[]] | @csv' | tr -d \" -} - -export -f listRes - -# Generate inventory in all regions - -echo "FunctionName, Runtime, Architecture" -aws --region=us-east-1 ec2 describe-regions --query Regions[].RegionName --output text | sed -e 's/\t/\n/g' | while read r; do - sem -j6 listRes $r -done - -sem --wait diff --git a/aws-lambda.py b/aws-lambda.py new file mode 100755 index 0000000..c7e8542 --- /dev/null +++ b/aws-lambda.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +r""" +Documentation + +License: This program is released under the MIT License +""" + +# Imports +import boto3 +import concurrent.futures + + +def getRegions(all_regions=False): + ec2 = boto3.client('ec2') + response = ec2.describe_regions(AllRegions=all_regions) + return [region['RegionName'] for region in response['Regions']] + +def printResources(region_name: str): + client = boto3.client('lambda', region_name=region_name) + response = client.list_functions() + for i in response['Functions']: + print(f"{i['FunctionName']}, {i['Runtime']}, {i['Architectures'][0]}, {region_name}") + +# Main function +def main() -> None: + print("FunctionName,Runtime,Architectures,region") + with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor: + futures = [executor.submit(printResources, region_name=r) for r in getRegions()] + results = [future.result() for future in concurrent.futures.wait(futures).done] + +# Call main function +if __name__ == '__main__': + main() diff --git a/aws-logs.py b/aws-logs.py index cdb1c6d..079ab79 100755 --- a/aws-logs.py +++ b/aws-logs.py @@ -7,6 +7,8 @@ License: This program is released under the MIT License # Imports import boto3 +import concurrent.futures + def getRegions(all_regions=False): ec2 = boto3.client('ec2') @@ -22,8 +24,9 @@ def printResources(region_name: str): # Main function def main() -> None: print("logGroupName,retentionInDays,storedBytes,region") - for r in getRegions(): - printResources(r) + with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor: + futures = [executor.submit(printResources, region_name=r) for r in getRegions()] + results = [future.result() for future in concurrent.futures.wait(futures).done] # Call main function if __name__ == '__main__': diff --git a/aws-opensearch-inventory.sh b/aws-opensearch-inventory.sh deleted file mode 100755 index dd63c8a..0000000 --- a/aws-opensearch-inventory.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -function listRes() { - aws --region $1 opensearch list-domain-names | jq -cr '.DomainNames[] | [.DomainName, .EngineType] | @csv' | sed "s/$/,$1/" -} - -export -f listRes - -# Generate inventory of ec2 in all regions - -echo "DomainName, EngineType, Region" -aws --region=us-east-1 ec2 describe-regions --query Regions[].RegionName --output text | sed -e 's/\t/\n/g' | while read r; do - sem -j6 listRes $r -done - -sem --wait diff --git a/aws-opensearch.py b/aws-opensearch.py new file mode 100755 index 0000000..66460df --- /dev/null +++ b/aws-opensearch.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +r""" +Documentation + +License: This program is released under the MIT License +""" + +# Imports +import boto3 +import concurrent.futures + +def getRegions(all_regions=False): + ec2 = boto3.client('ec2') + response = ec2.describe_regions(AllRegions=all_regions) + return [region['RegionName'] for region in response['Regions']] + +def printResources(region_name: str): + client = boto3.client('opensearch', region_name=region_name) + response = client.list_domain_names() + for i in response['DomainNames']: + print(f"{i['DomainName']}, {i['EngineType']}, {region_name}") + +# Main function +def main() -> None: + print("DomainName,EngineType,region") + with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor: + futures = [executor.submit(printResources, region_name=r) for r in getRegions()] + results = [future.result() for future in concurrent.futures.wait(futures).done] + +# Call main function +if __name__ == '__main__': + main() diff --git a/aws-rds-inventory.sh b/aws-rds-inventory.sh deleted file mode 100755 index b4d37ad..0000000 --- a/aws-rds-inventory.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -function listRes() { - aws rds describe-db-instances --region $1 | jq -cr '.DBInstances[] | [.DBInstanceIdentifier, .DBInstanceClass, .Engine, .AvailabilityZone, .MultiAZ] | @csv' | tr -d \" -} - -export -f listRes - -# Generate inventory of ec2 in all regions - -echo "InstanceId,InstanceClass,Engine,AZ,MultiAz" -aws --region=us-east-1 ec2 describe-regions --query Regions[].RegionName --output text | sed -e 's/\t/\n/g' | while read r; do - sem -j6 listRes $r -done - -sem --wait diff --git a/aws-rds.py b/aws-rds.py new file mode 100755 index 0000000..15f993c --- /dev/null +++ b/aws-rds.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +r""" +Documentation + +License: This program is released under the MIT License +""" + +# Imports +import boto3 +import concurrent.futures + +def getRegions(all_regions=False): + ec2 = boto3.client('ec2') + response = ec2.describe_regions(AllRegions=all_regions) + return [region['RegionName'] for region in response['Regions']] + +def printResources(region_name: str): + client = boto3.client('rds', region_name=region_name) + response = client.describe_db_instances() + for i in response['DBInstances']: + print(f"{i['DBInstanceIdentifier']}, {i['DBInstanceClass']}, {i['Engine']}, {i['EngineVersion']}, {i['MultiAZ']}, {region_name}") + +# Main function +def main() -> None: + print("DBInstanceIdentifier, DBInstanceClass, Engine, EngineVersion, MultiAZ, Region") + with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor: + futures = [executor.submit(printResources, region_name=r) for r in getRegions()] + results = [future.result() for future in concurrent.futures.wait(futures).done] + +# Call main function +if __name__ == '__main__': + main()