Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions aws/cost_explorer/aws_cost_and_usage_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3

import argparse
import boto3
import datetime

parser = argparse.ArgumentParser()
parser.add_argument('--days', type=int, default=30)
args = parser.parse_args()


now = datetime.datetime.utcnow()
start = (now - datetime.timedelta(days=args.days)).strftime('%Y-%m-%d')
end = now.strftime('%Y-%m-%d')

# to use a specific profile e.g. 'dev'
session = boto3.session.Session(profile_name='thebook')
#cd = session.client('ce', 'us-east-1')
#cd = session.client('ce', 'eu-central-1')
cd = session.client('ce')


TimePeriod = {
'Start': start,
'End': end
}

results = []

token = None
while True:
if token:
kwargs = {'NextPageToken': token}
else:
kwargs = {}
data = cd.get_cost_and_usage(TimePeriod=TimePeriod, Granularity='DAILY', Metrics=['UnblendedCost'], GroupBy=[{'Type': 'DIMENSION', 'Key': 'LINKED_ACCOUNT'}, {'Type': 'DIMENSION', 'Key': 'SERVICE'}], **kwargs)
results += data['ResultsByTime']
token = data.get('NextPageToken')
if not token:
break

print('\t'.join(['TimePeriod', 'LinkedAccount', 'Service', 'Amount', 'Unit', 'Estimated']))
for result_by_time in results:
for group in result_by_time['Groups']:
amount = group['Metrics']['UnblendedCost']['Amount']
unit = group['Metrics']['UnblendedCost']['Unit']
print(result_by_time['TimePeriod']['Start'], '\t', '\t'.join(group['Keys']), '\t', amount, '\t', unit, '\t', result_by_time['Estimated'])
15 changes: 15 additions & 0 deletions aws/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tool.poetry]
name = "swung_aws"
version = "0.1.0"
description = "Package to deal with AWS stuff for SWUNG"
authors = ["Filippo B <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.7"
boto3 = "^1.17.17"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
5 changes: 5 additions & 0 deletions aws/tools_automation/aws_chatbot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# AWS Chatbot

Resources:
https://aws.amazon.com/blogs/devops/running-aws-commands-from-slack-using-aws-chatbot/