【问题标题】:How to get cache_cluster_id from elasticache using boto如何使用 boto 从 elasticache 获取 cache_cluster_id
【发布时间】:2021-09-28 15:44:18
【问题描述】:

我想获取 elasticache cache_cluster_id 以填充 jinja2 模板。

我正在使用 boto 从 API 中获取一些东西,例如 ec2 实例或 elb 数据,但对于 elasticache 来说似乎更困难。

import boto.elasticache

conn = boto.elasticache.connect_to_region(region)
elasticache_data = conn.describe_cache_clusters()

这就是我设法得到的,但我不确定如何循环通过 cache_cluster_id 来获取我需要的所有数据。

参考:http://docs.pythonboto.org/en/latest/ref/elasticache.html

api 答案是这样的:

> {u'DescribeCacheClustersResponse': 
>     {u'ResponseMetadata': 
>         {u'RequestId': u'ID'}, 
>      u'DescribeCacheClustersResult': 
>            {u'Marker': None, 
>            u'CacheClusters': 
>                 [
>                     {u'Engine': u'redis', u'CacheParameterGroup': 
>                         {u'CacheNodeIdsToReboot': [], 
>                         u'CacheParameterGroupName': u'default', 
>                         u'ParameterApplyStatus': u'in-sync'},
>                      u'CacheClusterId': u'whatever', 
>                      u'CacheSecurityGroups': [], 
>                      u'ConfigurationEndpoint': None, 
>                      u'CacheClusterCreateTime': 1415217879.493, 
>                      u'ReplicationGroupId': u'whatever', 
>                      u'AutoMinorVersionUpgrade': True, 
>                      u'CacheClusterStatus': u'available', 
>                      u'NumCacheNodes': 1, 
>                      u'ClientDownloadLandingPage': u'https://console.aws.amazon.com...', 
>                      u'PreferredAvailabilityZone': u'us-east-1a', 
>                      u'SecurityGroups': [{u'Status': u'active', u'SecurityGroupId': u'123'}],   
>                      u'CacheSubnetGroupName': u'whatever',
>                      u'EngineVersion': u'2.8.6', 
>                      u'PendingModifiedValues': 
>                         {u'NumCacheNodes': None, 
>                         u'EngineVersion': None,
>                         u'CacheNodeIdsToRemove': None}, 
>                      u'CacheNodeType': u'cache.m1.small', 
>                      u'NotificationConfiguration': None, 
>                      u'PreferredMaintenanceWindow': 
>                      u'sat:04:30-sat:05:30', 
>                      u'CacheNodes': None
>                     }
>                 ]
>             }
>     } }

【问题讨论】:

    标签: python amazon-web-services boto amazon-elasticache


    【解决方案1】:

    不确定这是否是正确的做法......但它似乎可以满足我的需要。 如果有人有更好的方法,请告诉我。

    import boto.elasticache
    
    conn = boto.elasticache.connect_to_region(region)
    data = conn.describe_cache_clusters()
    clusters = data["DescribeCacheClustersResponse"]["DescribeCacheClustersResult"]["CacheClusters"]
    
    for value in clusters:
        print value["CacheClusterId"]
    

    【讨论】:

      【解决方案2】:
      import boto3
      
      
      conn = boto3.client('elasticache')
      
      data = conn.describe_cache_clusters()
      #print(data)
      #print(data['CacheClusters'])
      
      for r in data['CacheClusters']:
          #print(r)
          db_engine = r["Engine"]
          print(db_engine)
      

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 2015-06-28
      相关资源
      最近更新 更多