简体   繁体   English

如何在 python 中获取 ec2 实例成本?

[英]How to get an ec2 instance cost in python?

I launch an instance, stress it's cpu and delete the instance.我启动一个实例,强调它的 cpu 并删除该实例。

Using the aws cdk this takes a couple of minutes and I'm looping over 100 instance types (for benchmark purposes).使用 aws cdk 这需要几分钟,我正在循环超过 100 种实例类型(用于基准测试)。

How can I get that instance's loop cost programmatically (aws cli or boto3)?如何以编程方式获取该实例的循环成本(aws cli 或 boto3)?

I have the instance-id我有实例 ID

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)

Returns (shortened excerpt):返回(缩短的摘录):

{'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': {}}]}

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

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