【问题标题】:How to get an ec2 instance cost in python?如何在 python 中获取 ec2 实例成本?
【发布时间】:2021-12-29 17:16:16
【问题描述】:

我启动一个实例,强调它的 cpu 并删除该实例。

使用 aws cdk 这需要几分钟,我循环了 100 多个 实例类型(用于基准测试)。

如何以编程方式(aws cli 或 boto3)获取该实例的循环成本?

我有实例 ID

【问题讨论】:

  • Cost explorer API,但那里的数据可能会延迟。
  • 您想知道完成 CPU 密集型任务所花费的时间吗?在终止实例之前,让您的“压力”脚本记录某个位置(例如 Amazon S3)的时间可能是最简单的。
  • @JohnRotenstein 成本。这里似乎是一个相当奇特的要求cli doc
  • Amazon EC2 实例的成本取决于实例类型和运行时间(以秒为单位)。因此,您的脚本可以记录持续时间,您可以直接计算每个实例类型的相对成本,而不是尝试从计费记录中检索它。此外,您的程序使用每个实例类型的全部功能(例如,利用所有 CPU 的多个线程)也很重要,否则更大的实例不会提供任何更好的性能。
  • 为什么要花这么多精力来获得成本?

标签: python amazon-web-services amazon-ec2


【解决方案1】:
import boto3
import pprint

client = boto3.client('ce')

response = client.get_cost_and_usage_with_resources(
    Granularity='DAILY',
    Metrics=["BlendedCost", "UnblendedCost", "UsageQuantity"],
    TimePeriod={
        'Start': '2021-12-20',
        'End': '2021-12-28'
    },
    Filter={
        "Dimensions": {
            "Key": "SERVICE",
            "Values": ["Amazon Elastic Compute Cloud - Compute"]
        }
    },
    GroupBy=[{
        "Type": "DIMENSION",
        "Key": "RESOURCE_ID"
    }])

pprint.pprint(response)

返回(缩短的摘录):

{'DimensionValueAttributes': [],
 'GroupDefinitions': [{'Key': 'RESOURCE_ID', 'Type': 'DIMENSION'}],
 'ResponseMetadata': {'HTTPHeaders': {'cache-control': 'no-cache',
                                      'connection': 'keep-alive',
                                      'content-length': '8461',
                                      'content-type': 'application/x-amz-json-1.1',
                                      'date': 'Wed, 29 Dec 2021 09:08:16 GMT',
                                      'x-amzn-requestid': '2de9c92e-6d1c-4b1c-9087-bee17a41cb4f'},
                      'HTTPStatusCode': 200,
                      'RequestId': '2de9c92e-6d1c-4b1c-9087-bee17a41cb4f',
                      'RetryAttempts': 0},
 'ResultsByTime': [{'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2021-12-21T00:00:00Z',
                                   'Start': '2021-12-20T00:00:00Z'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'},
                              'UnblendedCost': {'Amount': '0', 'Unit': 'USD'},
                              'UsageQuantity': {'Amount': '0', 'Unit': 'N/A'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2021-12-22T00:00:00Z',
                                   'Start': '2021-12-21T00:00:00Z'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'},
                              'UnblendedCost': {'Amount': '0', 'Unit': 'USD'},
                              'UsageQuantity': {'Amount': '0', 'Unit': 'N/A'}}},
                   {'Estimated': True,
                    'Groups': [],
                    'TimePeriod': {'End': '2021-12-23T00:00:00Z',
                                   'Start': '2021-12-22T00:00:00Z'},
                    'Total': {'BlendedCost': {'Amount': '0', 'Unit': 'USD'},
                              'UnblendedCost': {'Amount': '0', 'Unit': 'USD'},
                              'UsageQuantity': {'Amount': '0', 'Unit': 'N/A'}}},
                   {'Estimated': True,
                    'Groups': [{'Keys': ['i-03ffa7c932a515d76'],
                                'Metrics': {'BlendedCost': {'Amount': '0.0027617772',
                                                            'Unit': 'USD'},
                                                            ....
                                                            ..
                                                            .
                                                            shortened here
                                                            .
                                                            .

                    'TimePeriod': {'End': '2021-12-27T00:00:00Z',
                                   'Start': '2021-12-26T00:00:00Z'},
                    'Total': {}},
                   {'Estimated': True,
                    'Groups': [{'Keys': ['i-0665a330b242714f2'],
                                'Metrics': {'BlendedCost': {'Amount': '0.216643501',
                                                            'Unit': 'USD'},
                                            'UnblendedCost': {'Amount': '0.216643501',
                                                              'Unit': 'USD'},
                                            'UsageQuantity': {'Amount': '0.554054168',
                                                              'Unit': 'N/A'}}},
                               {'Keys': ['i-080780d0d7e3394dd'],
                                'Metrics': {'BlendedCost': {'Amount': '2.7341269802',
                                                            'Unit': 'USD'},
                                            'UnblendedCost': {'Amount': '2.7341269802',
                                                              'Unit': 'USD'},
                                            'UsageQuantity': {'Amount': '1.0241218603',
                                                              'Unit': 'N/A'}}},
                               {'Keys': ['i-0b95613810475903b'],
                                'Metrics': {'BlendedCost': {'Amount': '0.432736006',
                                                            'Unit': 'USD'},
                                            'UnblendedCost': {'Amount': '0.432736006',
                                                              'Unit': 'USD'},
                                            'UsageQuantity': {'Amount': '0.5530218935',
                                                              'Unit': 'N/A'}}},
                               {'Keys': ['i-0eab899e392cf4f35'],
                                'Metrics': {'BlendedCost': {'Amount': '0.5645311508',
                                                            'Unit': 'USD'},
                                            'UnblendedCost': {'Amount': '0.5645311508',
                                                              'Unit': 'USD'},
                                            'UsageQuantity': {'Amount': '1.1896368629',
                                                              'Unit': 'N/A'}}}],
                    'TimePeriod': {'End': '2021-12-28T00:00:00Z',
                                   'Start': '2021-12-27T00:00:00Z'},
                    'Total': {}}]}

【讨论】:

    猜你喜欢
    • 2021-10-03
    • 2010-10-12
    • 1970-01-01
    • 2016-11-15
    • 2014-10-24
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2011-07-01
    相关资源
    最近更新 更多