amazon-web-services - 所有实例的 Aws 成本和使用情况
问题描述
我想在一段时间内获取我的 aws 帐户中每个实例的使用成本报告。
我能够在输出中获得linked_account_id 和服务,但我也需要instance_id。请帮忙
import argparse
import boto3
import datetime
cd = boto3.client('ce', 'ap-south-1')
results = []
token = None
while True:
if token:
kwargs = {'NextPageToken': token}
else:
kwargs = {}
data = cd.get_cost_and_usage(
TimePeriod={'Start': '2019-01-01', 'End': '2019-06-30'},
Granularity='MONTHLY',
Metrics=['BlendedCost','UnblendedCost'],
GroupBy=[
{'Type': 'DIMENSION', 'Key': 'LINKED_ACCOUNT'},
{'Type': 'DIMENSION', 'Key': 'SERVICE'}
], **kwargs)
results += data['ResultsByTime']
token = data.get('NextPageToken')
if not token:
break
print('\t'.join(['Start_date', 'End_date', 'LinkedAccount', 'Service', 'blended_cost','unblended_cost', 'Unit', 'Estimated']))
for result_by_time in results:
for group in result_by_time['Groups']:
blended_cost = group['Metrics']['BlendedCost']['Amount']
unblended_cost = group['Metrics']['UnblendedCost']['Amount']
unit = group['Metrics']['UnblendedCost']['Unit']
print(result_by_time['TimePeriod']['Start'], '\t',
result_by_time['TimePeriod']['End'],'\t',
'\t'.join(group['Keys']), '\t',
blended_cost,'\t',
unblended_cost, '\t',
unit, '\t',
result_by_time['Estimated'])
解决方案
据我所知,Cost Explorer 无法处理每个实例的使用情况。有一个功能成本和使用情况报告,它通过转储文件提供详细的计费报告。在此文件中,您可以看到实例 ID。
它还可以连接到AWS Athena。完成此操作后,直接查询 Athena 上的文件。
这是我的例子。
select
lineitem_resourceid,
sum(lineitem_unblendedcost) as unblended_cost,
sum(lineitem_blendedcost) as blended_cost
from
<table>
where
lineitem_productcode = 'AmazonEC2' and
product_operation like 'RunInstances%'
group by
lineitem_resourceid
结果是
lineitem_resourceid unblended_cost blended_cost
i-***************** 279.424 279.424
i-***************** 139.948 139.948
i-******** 68.198 68.198
i-***************** 3.848 3.848
i-***************** 0.013 0.013
其中resourceid 包含实例ID。本月所有使用的费用总额。对于其他类型的 product_operation,它将包含不同的资源 id。
推荐阅读
- javascript - document.querySelectorAll 在“:not”中的条件不起作用
- python - 修复语法错误
- spring - Spring Boot Test 不加载应用程序上下文
- java - Integer.class 的“名称”字段返回 null
- python - 我可以在抽象语法树中处理导入吗?
- java - 多线程环境中的文件处理问题
- php - 当我创建 users.blade.php 时,在 Laravel 中找不到类“App\users”
- php - 如何使用 new DateTime() 修复 if-else 语句的奇怪结果
- jenkins - 从失败的 Jenkins 流水线阶段重新启动
- css - 多个可滑动侧面板