作为运维要经常关注公司账户消耗情况,账户多了看账单的时间都需要优化,搞个脚本定期推送AWS账单,后续在搞监控,AWS就是api不太好找
#!/usr/bin/env python3 # coding=utf-8 #AWS API参考:https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetCostAndUsage.html import boto3 #账户的访问密钥 aws_access_key_id = '' aws_secret_access_key = '' client = boto3.client('ce', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) response = client.get_cost_and_usage( TimePeriod={ #我只查询这个月的 就填写本月的时间 "Start": "2022-01-01", "End": "2022-01-31" }, Granularity='MONTHLY', Metrics=[ 'BlendedCost', ], ) BlendedCost = response['ResultsByTime'][0]['Total']['BlendedCost']['Amount'] print(f"Amazon 账单:%.2f$" % float(BlendedCost))
代码执行结果
结合企业微信推送到企业微信,每天关注费用消耗
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)