作为运维要经常关注公司账户消耗情况,账户多了看账单的时间都需要优化,搞个脚本定期推送AWS账单,后续在搞监控,AWS就是api不太好找文章来源:https://www.toymoban.com/news/detail-573802.html
#!/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))
代码执行结果
结合企业微信推送到企业微信,每天关注费用消耗
文章来源地址https://www.toymoban.com/news/detail-573802.html
到了这里,关于AWS api查询账单的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!