Reputation: 633
I launch an instance, stress it's cpu and delete the instance.
Using the aws cdk this takes a couple of minutes and I'm looping over 100 instance types (for benchmark purposes).
How can I get that instance's loop cost programmatically (aws cli or boto3)?
I have the instance-id
Upvotes: 0
Views: 1017
Reputation: 633
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': {}}]}
Upvotes: 1