Use the following Python code to obtain the monthly total bill for AWS China. After the code is executed, some information is obtained. However, when the obtained data is compared with the final bill result on the console, it is found that the bill number obtained by using the Python code is inconsistent with the total bill number displayed on the console. How should this be solved? Is the parameter of the above Python code wrong? For example, the total bill displayed on the console is 649.53, but the total bill obtained by Python is 646.95
import boto3
import ast
time_start = '2024-06-01'
time_end = '2024-07-01'
billing_results = []
def get_aws_bill_summary(access_key_id, access_key_num):
# user security key
access_key_id = access_key_id
access_key_num = access_key_num
# get account id
sts_client = boto3.client('sts',
aws_access_key_id=access_key_id,
aws_secret_access_key=access_key_num,
region_name='cn-northwest-1'
)
response = sts_client.get_caller_identity()
account_id = response['Account']
# print(account_id)
# get billing cost total
client = boto3.client('ce',
aws_access_key_id=access_key_id,
aws_secret_access_key=access_key_num,
region_name='cn-northwest-1'
)
response = client.get_cost_and_usage(
# [start, end) -- excluding end date, follow front closed and back open.
TimePeriod={
'Start': time_start,
'End': time_end
},
Granularity='MONTHLY',
Metrics=['UnblendedCost']
)
cost_total = response['ResultsByTime'][0]['Total']['UnblendedCost']['Amount']
return account_id, round(float(cost_total), 2)
with open('access_key_cn_new', 'r') as file:
for line in file.readlines():
account = ast.literal_eval(line.strip()) # change str to dict
key_id = account['key_id']
key_num = account['key_num']
account_name = account['account_name']
account_id, cost_total = get_aws_bill_summary(key_id, key_num)
print("Access ID: %s, Access Name: %s, Cost Total: %s" % (account_id,account_name, cost_total))
-
Without seeing the detailed data, the difference is probably attributed to either 1) discounts that are not being applied in Cost Explorer or 2) the blended costs are being displayed in Cost Explorer...Trentium– Trentium2024年07月20日 11:12:33 +00:00Commented Jul 20, 2024 at 11:12
-
Have you checked the data per invoice id? The cost and usage includes all type of costs, i.e. Tax, Discount, RI and so on. They could have difference invoice id and so you should compare them per invoice which may reveal the difference from somewhere.Daeho Ro– Daeho Ro2024年08月04日 11:20:17 +00:00Commented Aug 4, 2024 at 11:20