diff --git a/aws/cost_explorer/aws_cost_and_usage_report.py b/aws/cost_explorer/aws_cost_and_usage_report.py new file mode 100755 index 0000000..c87ce1b --- /dev/null +++ b/aws/cost_explorer/aws_cost_and_usage_report.py @@ -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']) diff --git a/aws/pyproject.toml b/aws/pyproject.toml new file mode 100644 index 0000000..a23bbb8 --- /dev/null +++ b/aws/pyproject.toml @@ -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 "] + +[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" diff --git a/aws/tools_automation/aws_chatbot/README.md b/aws/tools_automation/aws_chatbot/README.md new file mode 100644 index 0000000..e0da9c7 --- /dev/null +++ b/aws/tools_automation/aws_chatbot/README.md @@ -0,0 +1,5 @@ +# AWS Chatbot + +Resources: +https://aws.amazon.com/blogs/devops/running-aws-commands-from-slack-using-aws-chatbot/ +