From 55d32b83ff2eba21a7f84d90ec6e99ebc6590377 Mon Sep 17 00:00:00 2001 From: KenF Date: Thu, 22 Jan 2026 09:31:32 +0800 Subject: [PATCH] feat: porting scripts to python --- .idea/.gitignore | 5 ++++ .idea/aws-inventory.iml | 10 +++++++ .idea/inspectionProfiles/Project_Default.xml | 14 +++++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++++ .idea/misc.xml | 7 +++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ README.md | 1 + aws-apigw-inventory.sh | 7 ----- aws-apigw.py | 23 ++++++++++++++ aws-cloudfront-inventory.sh | 7 ----- aws-cloudfront.py | 23 ++++++++++++++ aws-logs-inventory.sh | 16 ---------- aws-logs.py | 30 +++++++++++++++++++ aws-s3-inventory.sh | 5 ---- aws-s3.py | 26 ++++++++++++++++ run-inventory-scripts.sh | 4 +-- 17 files changed, 161 insertions(+), 37 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/aws-inventory.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml delete mode 100755 aws-apigw-inventory.sh create mode 100755 aws-apigw.py delete mode 100755 aws-cloudfront-inventory.sh create mode 100755 aws-cloudfront.py delete mode 100755 aws-logs-inventory.sh create mode 100755 aws-logs.py delete mode 100755 aws-s3-inventory.sh create mode 100755 aws-s3.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/aws-inventory.iml b/.idea/aws-inventory.iml new file mode 100644 index 0000000..555ffe3 --- /dev/null +++ b/.idea/aws-inventory.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..debf80d --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,14 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..236fbbf --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..61d771e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 1149cc6..99b15e6 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Scripts to collect AWS resource inventory in all regions. - awscli - gnu parallel - miller +- python 3.13+ ## Usage Run run-inventory-scripts.sh which will invoke a collection of scripts. TSV is returned for visibility. diff --git a/aws-apigw-inventory.sh b/aws-apigw-inventory.sh deleted file mode 100755 index def4f6c..0000000 --- a/aws-apigw-inventory.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -#RESTYPE=ApiGateway - -# Generate inventory in all regions -echo "RestAPI,Scope" -aws apigateway get-rest-apis | jq -cr '.items[] | [.name, .endpointConfiguration.types[0]] | @csv' diff --git a/aws-apigw.py b/aws-apigw.py new file mode 100755 index 0000000..a9d3cad --- /dev/null +++ b/aws-apigw.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +r""" +Documentation + +License: This program is released under the MIT License +""" + +# Imports +import boto3 + + +# Main function +def main() -> None: + client = boto3.client('apigateway') + response = client.get_rest_apis() + + print("RestAPIName,Scope") + for i in response['items']: + print(f'{i["name"]}, {i["endpointConfiguration"]["types"][0]}') + +# Call main function +if __name__ == '__main__': + main() diff --git a/aws-cloudfront-inventory.sh b/aws-cloudfront-inventory.sh deleted file mode 100755 index 23bee65..0000000 --- a/aws-cloudfront-inventory.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -# Generate inventory in all regions - -echo "CFDist,Alias" -aws cloudfront list-distributions | jq -cr '.DistributionList.Items[] | [.Id, .Aliases.Items[0]] | @csv' - diff --git a/aws-cloudfront.py b/aws-cloudfront.py new file mode 100755 index 0000000..e1ec59c --- /dev/null +++ b/aws-cloudfront.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +r""" +Documentation + +License: This program is released under the MIT License +""" + +# Imports +import boto3 + + +# Main function +def main() -> None: + client = boto3.client('cloudfront') + response = client.list_distributions() + + print("Distribution, Alias, OriginId") + for i in response['DistributionList']['Items']: + print(f'{i["Id"]}, {i["Aliases"]["Items"][0]}, {i["Origins"]["Items"][0]["Id"]}') + +# Call main function +if __name__ == '__main__': + main() diff --git a/aws-logs-inventory.sh b/aws-logs-inventory.sh deleted file mode 100755 index d581e08..0000000 --- a/aws-logs-inventory.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -function listRes() { - aws --region $1 logs describe-log-groups | jq -cr '.logGroups[] | [.logGroupName, .retentionInDays, .logGroupClass] | @csv' | tr -d \" -} - -export -f listRes - -# Generate inventory in all regions - -echo "LogGroup, Retention, Class" -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-logs.py b/aws-logs.py new file mode 100755 index 0000000..cdb1c6d --- /dev/null +++ b/aws-logs.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +r""" +Documentation + +License: This program is released under the MIT License +""" + +# Imports +import boto3 + +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('logs', region_name=region_name) + response = client.describe_log_groups() + for logGroup in response['logGroups']: + print(f"{logGroup['logGroupName']}, {logGroup.get('retentionInDays')}, {logGroup['storedBytes']}, {region_name}") + +# Main function +def main() -> None: + print("logGroupName,retentionInDays,storedBytes,region") + for r in getRegions(): + printResources(r) + +# Call main function +if __name__ == '__main__': + main() diff --git a/aws-s3-inventory.sh b/aws-s3-inventory.sh deleted file mode 100755 index 7f5bb77..0000000 --- a/aws-s3-inventory.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -# Generate inventory in all regions -echo "BucketName" -aws s3api list-buckets --query Buckets[].Name --output text | sed 's/\t/\n/g' diff --git a/aws-s3.py b/aws-s3.py new file mode 100755 index 0000000..5d88cc5 --- /dev/null +++ b/aws-s3.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +r""" +Documentation + +License: This program is released under the MIT License +""" + +# Imports +import boto3 + + +# Main function +def main() -> None: + s3_client = boto3.client('s3') + + # Get the list of all buckets + response = s3_client.list_buckets() + + # Print the names of the buckets + print('Buckets') + for bucket in response['Buckets']: + print(f'{bucket["Name"]}') + +# Call main function +if __name__ == '__main__': + main() diff --git a/run-inventory-scripts.sh b/run-inventory-scripts.sh index b35ebb2..72c1777 100755 --- a/run-inventory-scripts.sh +++ b/run-inventory-scripts.sh @@ -1,7 +1,7 @@ #!/bin/bash -for i in aws*.sh; do +for i in aws*.{py,sh}; do echo "# $i" - bash $i | mlr --c2t cat | column -t + ./"$i" | mlr --c2t cat | column -t wait $! done