Skip to content

Instantly share code, notes, and snippets.

@germainlefebvre4
Last active April 19, 2019 12:41
Show Gist options
  • Save germainlefebvre4/9c266068e6a660dcfb2623849d506802 to your computer and use it in GitHub Desktop.
Save germainlefebvre4/9c266068e6a660dcfb2623849d506802 to your computer and use it in GitHub Desktop.
Collect billing costs for each AWS Accounts registered and push dataset in InfluxDB
#!/usr/bin/env python
# Author: Germain LEFEBVRE [Ineat]
# Prerequisites
# 00 - Create API Access Key in AWS Accounts to monitor
# 00 - Fill ~/.aws/credentials with API Access Keys
# 01 - Fill ./config.yml 'aws_account' dict with aws profiles or access/secret key as following:
# aws_accounts:
# - name: Ineat iLab
# profile: INEAT_ILAB
# - name: Ineat iLab
# access_key: ACCESS_KEY
# secret_key: SECRET_KEY
# 04 - Create InfluxDB database 'aws'
# $ influx -execute 'create databases aws'
# 05 - Run the python script
# 06 - Stalk your AWS billing costs in Grafana
import yaml, boto3
from datetime import date,datetime
from dateutil.relativedelta import relativedelta
from influxdb import InfluxDBClient
# AWS type cost
aws_cost_type=['UnblendedCost']
# Load configuration file
with open("config.yml", 'r') as ymlfile:
cfg = yaml.load(ymlfile, Loader=yaml.SafeLoader)
# InfluxDB connexion
inf_cl = InfluxDBClient('localhost', 8086, '', '', 'aws')
# Browse AWS Accounts Profiles
# Located in ~/.aws/credentials
for account in cfg['aws_accounts']:
print(account['name'])
# Force region to work
region_name="eu-west-1"
# Open AWS Cost Explore
# When use Profile or Access Key
if 'profile' in account.keys():
session = boto3.Session(region_name=region_name,
profile_name=account['profile'])
elif 'access_key' in account.keys():
session = boto3.Session(region_name=region_name,
aws_access_key_id=account['access_key'],
aws_secret_access_key=account['secret_key'])
aws_cl_ce = session.client('ce')
# Load Costs dataset
cost_usage = aws_cl_ce.get_cost_and_usage(
TimePeriod={
#'Start': (date.today() + relativedelta(days=-1)).strftime('%Y-%m-%d'),
'Start': (date.today() + relativedelta(months=-1)).strftime('%Y-%m-01'),
'End': (date.today() + relativedelta(months=+1)).strftime('%Y-%m-01'),
},
Granularity='MONTHLY',
Metrics=[
",".join(aws_cost_type)
]
)
# Print dateset
for type in aws_cost_type:
for metric in cost_usage['ResultsByTime']:
account_name = account['name']
metric_time_type = "monthly"
metric_start = metric['TimePeriod']['Start']
metric_amount = metric['Total'][type]['Amount']
metric_unit = metric['Total'][type]['Unit']
print("%s %s %s" % (metric_start, metric_amount, metric_unit) )
json_body = [
{
"measurement": "billing",
"tags": {
"account": account_name,
"time type": metric_time_type,
},
#"time": datetime.now().isoformat(),
"time": datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
"fields": {
"value": metric['Total'][type]['Amount']
}
}
]
inf_cl.write_points(json_body)
result = inf_cl.query('select value from billing;')
print("Result: {0}".format(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment