8

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.

What's the correct way of doing so?

starball
  • 20,030
  • 7
  • 43
  • 238
Galpo
  • 81
  • 1
  • 1
  • 2
  • Have you looked at the answers to this question? http://stackoverflow.com/questions/27157080/aws-billing-information-using-aws-java-api – Mark B Mar 08 '16 at 14:51
  • 1
    Yes thank you, I hoped there would be a much easier solution since this response was made almost a year ago and the response did not refered to boto3. – Galpo Mar 08 '16 at 15:21
  • boto3 is just another name for the AWS SDK for Python. All the official AWS SDKs have the same capabilities. – Mark B Mar 08 '16 at 15:51
  • Ok, but if it has the same capabilities - how do i fetch my billing status? – Galpo Mar 13 '16 at 14:29
  • Use the answer the question I linked – Mark B Mar 13 '16 at 15:50

3 Answers3

11

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
  • 4,107
  • 5
  • 50
  • 60
1

I use the CloudWatch API to extract billing information. The "AWS/Billing" namespace has everything you need.

Ayush Sharma
  • 265
  • 4
  • 13
0

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)
Jhoov
  • 1