AWS billing with python

Viewed 8738

I'm want to extract my current billing from aws by using amazon boto3 library for python, but couldn't find any API command that does so.

When trying to use previous version (boto2) with the fps connection and get_account_balance() method, i'm waiting for a response with no reply.

Can anyone tell me the correct way of doing so ?

3 Answers

You can get the current bill of your AWS account by using the CostExplorer API.

Below is an example:

import boto3

client = boto3.client('ce', region_name='us-east-1')

response = client.get_cost_and_usage(
    TimePeriod={
        'Start': '2018-10-01',
        'End': '2018-10-31'
    },
    Granularity='MONTHLY',
    Metrics=[
        'AmortizedCost',
    ]
)

print(response)

captainblack has the right idea. Note, however, that the End Date is exclusive. In the example provided, you would only retrieve cost data from 2018-10-01 - 2018-10-30. The end date needs to be the first day of the following month if you want the cost data for the entire month:

import boto3

client = boto3.client('ce', region_name='us-east-1')

response = client.get_cost_and_usage(
    TimePeriod={
        'Start': '2018-10-01',
        'End': '2018-11-01'
    },
    Granularity='MONTHLY',
    Metrics=[
        'AmortizedCost',
    ]
)

print(response)
Related