繁体   English   中英

所有实例的 AWS 成本和使用量

[英]Aws cost and usage for all the instances

我想在一段时间内获取我的 aws 帐户中每个实例的使用成本报告。

我可以在输出中获得linked_account_id 和service,但我也需要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'])

据我所知,成本管理器无法处理每个实例的使用情况。 有一个功能成本和使用报告,它通过转储文件提供详细的计费报告。 在此文件中,您可以看到实例 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。

您可以将单个标签添加到所有实例(例如Id ),然后按该标签分组:

GroupBy=[
   {
       'Type': 'TAG',
       'Key': 'Id'
   },
],

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM