Programmatic Access to Billing Data (Python Example)

Many of our customers have requested an example of how programmatically access billing data.


Below is a working python example.

You will need to change:

  • local and working paths
  • Add your AWS Access ID and Secret ID
  • Add your 12-digit account number to the 
  • correct the BILLING_MONTH using yyyy-mm format.




import boto
from boto.s3.key import Key
import zipfile
import csv

BILLING_MONTH = '2017-02'
LOCAL_PATH = '/home/localuser/s3/'
WORKING_PATH = '/home/localuser/csv/'
AWS_ACCESS_KEY_ID = '--------------------'
AWS_SECRET_ACCESS_KEY = '----------------------------------------'
bucket_name = 'xxxxxxxxxxxx-dlt-utilization'

# connect to the bucket
conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket(bucket_name)

#go through list of files
bucket_list = bucket.list()
for l in bucket_list:
  keyString = str(l.key)
  #download mothly (to date) billing file
  if BILLING_MONTH in keyString:
    FILE_DOWNLOADED = keyString

#unzip file
zip_ref = zipfile.ZipFile(LOCAL_PATH+FILE_DOWNLOADED, 'r')
CSV_FILE = zip_ref.namelist()[0]

#report on various data points
with open(WORKING_PATH+CSV_FILE) as csvfile:
  reader = csv.DictReader(csvfile)
  for row in reader:
    if row['UnBlendedCost'] > 0:
      print (row['ProductName'] + '\t' + row['Operation'] + '\t' + row['UnBlendedCost'] + '\t' + row['ResourceId'])
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request


Please sign in to leave a comment.
Powered by Zendesk