可能是因为购买了“Reserved Instances”导致的,可以通过以下代码检查是否存在这种情况,并将其排除:
import boto3
client = boto3.client('ce')
results = client.get_cost_and_usage(
TimePeriod={
'Start': '2019-01-01',
'End': '2019-02-01'
},
Granularity='MONTHLY',
Metrics=['UnblendedCost'],
GroupBy=[
{
'Type': 'DIMENSION',
'Key': 'INSTANCE_TYPE'
},
],
Filter={"Dimensions": {"Key": "USAGE_TYPE_GROUP", "Values": ["EC2: Running Hours"]}}
)['ResultsByTime']
for result in results:
for groups in result['Groups']:
keys = groups['Keys']
value = groups['Metrics']['UnblendedCost']['Amount']
if keys[0] == 'No Instance Type':
print(f'Cost of running EC2 with "No Instance Type": ${value}')
这段代码将返回一个月的费用,如果存在“Reserved Instances”导致的费用异常情况,可以通过设置“Filter”参数中的“Values”来排除这种情况。例如:
Filter={"And": [{"Dimensions": {"Key": "USAGE_TYPE_GROUP", "Values": ["EC2: Running Hours"]}}, {"Not": {"Dimensions": {"Key": "LEGAL_ENTITY_NAME", "Values": ["My RI"]}}}]})
这段代码表示筛选掉LEGAL_ENTITY_NAME为“My RI”的“Reserved Instances”费用。