在AWS-ElastiCache中,Reserved_Cache_Nodes-预留节点,也就类似于EC2与RDS的RI(预留实例),
都是为了节省成本而选择预付费用的一种方式 ,当AWS账号有多个时,如何通过编程的方式批量获取所有账号所有区域Region的RN信息呢
我们可以通过aws cli的方式,也可以通过AWS SDK for Python (Boto3)的方式,这里笔者将使用后者进行实现
首先,我们先看一个RN的数据信息结构,是json格式,详细如下:
{'ReservedCacheNodeId': 'webporoject2023', 'ReservedCacheNodesOfferingId': '65048c7a-79cc-48bf-a45a-163570d48906', 'CacheNodeType': 'cache.t2.micro', 'StartTime': datetime.datetime(2020, 8, 20, 9, 44, 48, 102000, tzinfo=tzutc()), 'Duration': 31536000, 'FixedPrice': 460.0, 'UsagePrice': 0.0, 'CacheNodeCount': 1, 'ProductDescription': 'redis', 'OfferingType': 'Heavy Utilization', 'State': 'retired', 'RecurringCharges': [{'RecurringChargeAmount': 0.052, 'RecurringChargeFrequency': 'Hourly'}], 'ReservationARN': 'arn:aws-cn:elasticache:cn-north-1:221234567891:reserved-instance:webporoject2023'}
这样我们可以通过 ElastiCacheclientclient.describe_reserved_cache_nodes()的方式获取如下一些关键的字段信息
那么,这里就会缺少,我们可能更希望直接看到的EndTime与AWS_Account ,对于AWS_Account 当然可以通过profile变量得到,可以设计成一样
对于EndTime,则我们可以自己计算出来,逻辑为Endtime=StartTime+Duration , 其中Duration的数字为秒数,一般来说就1年或者3年的秒数
接下来就是通过代码进行实现,笔者整理的核心代码如下:
ReservedCacheNodesResponse=ElastiCacheclient.describe_reserved_cache_nodes() for ReservedCacheNode in ReservedCacheNodesResponse["ReservedCacheNodes"]: ReservedCacheNodeId=ReservedCacheNode["ReservedCacheNodeId"] ReservedCacheNodesOfferingId=ReservedCacheNode["ReservedCacheNodesOfferingId"] CacheNodeType=ReservedCacheNode["CacheNodeType"] Duration=ReservedCacheNode["Duration"] StartTime=(ReservedCacheNode["StartTime"]+datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S") # Regarding Endtime, we can calculate it based on the StartTime and Duration EndTime=(ReservedCacheNode["StartTime"]+datetime.timedelta(seconds=Duration)+datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S") FixedPrice=ReservedCacheNode["FixedPrice"] UsagePrice=ReservedCacheNode["UsagePrice"] CacheNodeCount=ReservedCacheNode["CacheNodeCount"] ProductDescription=ReservedCacheNode["ProductDescription"] OfferingType=ReservedCacheNode["OfferingType"] State=ReservedCacheNode["State"] RecurringChargeAmount=ReservedCacheNode["RecurringCharges"][0]["RecurringChargeAmount"] RecurringChargeFrequency=ReservedCacheNode["RecurringCharges"][0]["RecurringChargeFrequency"] ReservationARN=ReservedCacheNode["ReservationARN"]
最后再将结果输出到Excel文件,通过邮件发送到reviwer的邮箱即可,结果大致如下:
尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/17364043.html
标签:-%,Reserved,python,Cache,AWS,ReservedCacheNode,StartTime,Duration,datetime From: https://www.cnblogs.com/5201351/p/17364043.html