お金がかかりそうなAWSサービスの全リージョン状況把握 boto スクリプト

* credential 設定
* IAM Role の権限必要

```python
#!/usr/bin/env python3

import boto3
import json
from bson import json_util

class get_all_region_resources:

        ##########
        # init
        ##########
        def __init__(self):
                self.default_region = "us-east-1"

                # ec2
                ec2_stats = [
                        "InstanceId"
                        ,"PrivateIpAddress"
                        ,"State.Name"
                        ,"PublicIpAddress"
                        ,"InstanceType"
                        ,"Placement.AvailabilityZone"
                ]
                self.ec2_stats = ec2_stats
#                       print_list.append(dev_instances["SecurityGroups"])
#                       print_list.append(dev_instances["Tags"])

                # rds
                rds_stats = [
                        "DBClusterIdentifier"
                        ,"Engine"
                        ,"Status"
                        ,"MultiAZ"
                        ,"EngineVersion"
                        ,"AvailabilityZones"
                ]
                self.rds_stats = rds_stats

                # elasticache
                elasticache_stats = [
                        "CacheClusterId"
                        ,"ReplicationGroupId"
                        ,"Engine"
                        ,"EngineVersion"
                        ,"CacheClusterStatus"
                        ,"PreferredAvailabilityZone"
                ]
                self.elasticache_stats = elasticache_stats

        ##########
        # get all regions
        ##########
        def _get_regions(self):
                ec2 = boto3.client('ec2',region_name=self.default_region)
                return ec2.describe_regions()['Regions']

        ##########
        # get stats
        ##########
        def _get_stats(self ,settings , resource ,stats):
                print_list = [ "==" + resource + "==" ]
                for stat in stats:
                        value = stat + ": "
                        try:
                                value +=  self._get_multi_stat(settings ,stat) + " "
                        except:
                                pass
                        print_list.append(value)
                print(print_list)

        ##########
        # split delimita
        ##########
        def _get_multi_stat(self ,settings ,stat):
                multi_value = settings
                for multi_stat in stat.split('.'):
                        multi_value = multi_value[multi_stat]
                return multi_value

        ##########
        # ec2
        ##########
        def _get_ec2(self ,region):
                resource = "ec2"
                ec2 = boto3.client(resource ,region_name=region)
                Reservations = ec2.describe_instances()['Reservations']

                for reservation in Reservations:
                        for instance in reservation['Instances']:
                                self._get_stats(instance ,resource ,self.ec2_stats)
                                
        ##########
        # rds
        ##########
        def _get_rds(self ,region):
                resource = "rds"
                rds = boto3.client(resource ,region_name=region)
                instances = rds.describe_db_clusters()['DBClusters']

                for instance in instances:
                        self._get_stats(instance ,resource ,self.rds_stats)

        ##########
        # elasticache
        ##########
        def _get_elasticaches(self ,region):
                resource = "elasticache"
                redis = boto3.client(resource ,region_name=region)
                instances = redis.describe_cache_clusters()['CacheClusters']

                for instance in instances:
                        self._get_stats(instance ,resource ,self.elasticache_stats)

# Main  
if __name__ == '__main__':

        this = get_all_region_resources()
        
        # regions
        regions = this._get_regions()

        for region in regions:
                region = region['RegionName']
                print("---------------------------")
                print(region)
                print("---------------------------")
        
                this._get_ec2(region)
                this._get_rds(region)
                this._get_elasticaches(region)


```