Giter Site home page Giter Site logo

awscost's Introduction

awscost

PyPI version Build Status

Command Line utility for Provides aws cost very easy to see.

Supports python 3.7 or later.

Install

$ pip install awscost

Examples

show cost

show cost latest 5 months.

$ awscost
key                                       2019-07    2019-08    2019-09    2019-10    2019-11    2019-12
--------------------------------------  ---------  ---------  ---------  ---------  ---------  ---------
Total                                      348.34      97.4       98.46     106.47      67.25       7.84
EC2 - Other                                 34.28      34.28      33.2       34.28      33.63       3.01
Amazon Elastic Compute Cloud - Compute      17.11      17.11      16.56      17.11      17.13       1.47
AWS Cost Explorer                                                                        0.56       1.44
Tax                                         25.8        7.22       7.27       9.72       6.12       0.69
AWS CloudTrail                               3.29       3.21       4.15       5.57       4.44       0.61
AWS Key Management Service                   0          0          2.8        4          4          0.35
AmazonCloudWatch                             1.5        0.01       0          0          0.88       0.11
Amazon Route 53                              0.1        0.1        0.1        0.1        0.1        0.1
Amazon Simple Storage Service                0.25       0.24       0.27       0.45       0.4        0.05
AWS Lambda                                   0          0          0          0          0          0
Amazon DynamoDB                              0          0          0          0          0
Amazon Elastic File System                   0          0          0          0          0          0
Amazon SageMaker                           266         35.22      34.11      35.22       0
Amazon Simple Notification Service           0          0                                0
Amazon Polly                                                                  0
Amazon Simple Queue Service                                                              0

show cost latest 3 days, group by SERVICE and operation.

$ awscost -g DAILY -p 3 -d SERVICE -d OPERATION
key                                                             2019-12-01    2019-12-02    2019-12-03
------------------------------------------------------------  ------------  ------------  ------------
Total                                                                 2.87          2.1           2.87
GetCostAndUsage,AWS Cost Explorer                                                   0.02          1.42
NatGateway,EC2 - Other                                                1.08          1.08          0.77
RunInstances,Amazon Elastic Compute Cloud - Compute                   0.55          0.55          0.37
None,AWS CloudTrail                                                   0.22          0.22          0.17
Unknown,AWS Key Management Service                                    0.13          0.13          0.09
Unknown,AmazonCloudWatch                                              0.04          0.04          0.03
CreateVolume-Gp2,EC2 - Other                                          0.03          0.03          0.01
PutObject,Amazon Simple Storage Service                               0.02          0.02          0.01
NoOperation,Tax                                                       0.25
...

support dimensions

  • AZ
  • INSTANCE_TYPE
  • LINKED_ACCOUNT
  • OPERATION
  • PURCHASE_TYPE
  • SERVICE
  • USAGE_TYPE
  • PLATFORM
  • TENANCY
  • RECORD_TYPE
  • LEGAL_ENTITY_NAME
  • DEPLOYMENT_OPTION
  • DATABASE_ENGINE
  • CACHE_ENGINE
  • INSTANCE_TYPE_FAMILY
  • REGION
  • BILLING_ENTITY
  • RESERVATION_ID
  • SAVINGS_PLANS_TYPE
  • SAVINGS_PLAN_ARN
  • OPERATING_SYSTEM

see. https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetDimensionValues.html

support filter

awscost --filter '{
  "Not": {
    "Dimensions": {
      "Key": "RECORD_TYPE",
      "Values": ["Credit", "Refund", "Upfront"]
    }
  }
}'

config file($HOME/.awscost)

default:
  metrics: AmortizedCost
  filter:
    Dimensions:
      Key: RECORD_TYPE
      Values:
        - Usage
        - Tax

discount:
  filter:
    Dimensions:
      Key: RECORD_TYPE
      Values:
        - Credit
        - Refund
        - Upfront

ec2:
  filter:
    Dimensions:
      Key: SERVICE
      Values:
        - "EC2 - Other"
        - "Amazon Elastic Compute Cloud - Compute"

You can exec command as below.

$ awscost --profile default
$ awscost --profile discount
$ awscost --profile ec2

group by tag

You can set up a group by tag.

Currently, this cannot be achieved using only the command line.

Write the following in yaml in $HOME/.awscost

The following is an example of grouping by the tag "Product"

tag:
  tags:
    - Product
  dimensions: []

You can exec command as below.

$ awscost --profile tag

Various format(-t option)

example, use -t tsv.

default is simple.

$ awscost -d SERVICE -d OPERATION -t tsv
key                                                             01-24   01-25   01-26   01-27   01-28   01-29   01-30   01-31   02-01   02-02
Total                                                            2       2       2       2       2.02    2.01    2.01    2.01    2.5     1.67
EC2 - Other,NatGateway                                           1.08    1.08    1.08    1.08    1.08    1.08    1.08    1.08    1.08    0.9
Amazon Elastic Compute Cloud - Compute,RunInstances              0.55    0.55    0.55    0.55    0.55    0.55    0.55    0.55    0.55    0.46
...

support format

  • simple(default)
  • plain
  • github
  • grid
  • fancy_grid
  • pipe
  • orgtbl
  • jira
  • presto
  • psql
  • rst
  • mediawiki
  • moinmoin
  • youtrack
  • html
  • latex
  • latex_raw
  • latex_booktabs
  • textile
  • tsv

Settings

aws auth support following.

  • environment variables
  • profile(use --aws-profile option.)
  • instance profile

Python API

from awscost.cost_explorer import CostExplorer
from dateutil.relativedelta import relativedelta
from datetime import datetime

start = (datetime.today() - relativedelta(months=3)).strftime("%Y-%m-01")
end = datetime.today().strftime("%Y-%m-01")

cost_explorer = CostExplorer(
    granularity="MONTHLY",
    start=start,
    end=end,
    dimensions=["SERVICE"],
    metrics="UnblendedCost",
    threshold=1.0,
).get_cost_and_usage_total_and_group_by()

# return dict data.
cost_explorer # =>
# {   'AWS CloudTrail': {'2019-11': 4.44, '2019-12': 6.17, '2020-01': 4.38},
#     'AWS Lambda': {'2019-11': 0.0, '2019-12': 0.0, '2020-01': 0.0},
# ...
#     'Total': {'2019-11': 67.15, '2019-12': 72.22, '2020-01': 68.11}}

matplotlib

...

import matplotlib.pyplot as plt

plt.figure(figsize=(25, 15), dpi=100)
plt.xlabel('month', fontsize=16)
plt.ylabel('$', fontsize=16)
plt.grid(True)
for i, (service_name, v) in enumerate(cost_explorer.items()):
    left = list(v.keys())
    height = list(v.values())
    plt.plot(left, height, linewidth=2, label=service_name, marker='o')
plt.legend(loc='best', fontsize=15, numpoints=5)

plt.show()

iam policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "xxxxxxxxx",
            "Effect": "Allow",
            "Action": [
                "ce:GetCostAndUsage"
            ],
            "Resource": "*"
        }
    ]
}

CI

install test package

$ ./scripts/ci.sh install-test

test

$ ./scripts/ci.sh run-test

flake8 and black and pytest.

release pypi

$ ./scripts/ci.sh release

git tag and pypi release.

awscost's People

Contributors

toyama0919 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

awscost's Issues

Filter out zero rows

Thanks for this. It's a convenient way to tabulate costs!

Is there an option to omit the rows for which the cost is always zero (0)?

Filtering the output programmatically would be awkward and make this a lot less convenient, so I'm just delete such lines by hand for now.

Add option for rowwise totals

The Cost Explorer's "Cost and usage breakdown" panel displays a grid like awscost.

The panel adds a rowwise total for the grouped dimensions.

The panel sorts by the descending rowwise total.

It would be convenient to replicate that feature in awscost.

Thank you :-)

UnboundLocalError: local variable 'days' referenced before assignment

Hi,
Can you explain how to set my aws account detail so could connect to it?

second, Trying to run the example :

from awscost.cost_explorer import CostExplorer
from dateutil.relativedelta import relativedelta
from datetime import datetime

start = (datetime.today() - relativedelta(months=3)).strftime("%Y-%m-01")
end = datetime.today().strftime("%Y-%m-01")

cost_explorer = CostExplorer(
    "MONTHLY", start, end, dimensions=["SERVICE"], metrics="UnblendedCost"
).get_cost_and_usage_total_and_group_by()

# return dict data. 
cost_explorer # =>

getting:

Traceback (most recent call last):
  File "c:\Users\avico\OneDrive\Desktop\בסד\5_or7.py", line 8, in <module>
    cost_explorer = CostExplorer(
  File "C:\Users\avico\AppData\Local\Programs\Python\Python310\lib\site-packages\awscost\cost_explorer.py", line 50, in __init__
    start or profile.get("start") or DateUtil.get_start(self.granularity, point)
  File "C:\Users\avico\AppData\Local\Programs\Python\Python310\lib\site-packages\awscost\date_util.py", line 16, in get_start
    start_datetime = datetime.today() - timedelta(days=days)
UnboundLocalError: local variable 'days' referenced before assignment

some errors

hi, when run reame.md example, find follow errors.
awscost -t , can output json or csv, i want aws bill data use pandas , get report on dango or Excel.

File "1.py", line 9, in
cost_explorer = CostExplorer(
File "/usr/local/lib/python3.8/dist-packages/awscost/cost_explorer.py", line 50, in init
start or profile.get("start") or DateUtil.get_start(self.granularity, point)
File "/usr/local/lib/python3.8/dist-packages/awscost/date_util.py", line 16, in get_start
start_datetime = datetime.today() - timedelta(days=days)
UnboundLocalError: local variable 'days' referenced before assignment

cat 1.py
from awscost.cost_explorer import CostExplorer
from dateutil.relativedelta import relativedelta
from datetime import datetime

start = (datetime.today() - relativedelta(months=3)).strftime("%Y-%m-01")
end = datetime.today().strftime("%Y-%m-01")

cost_explorer = CostExplorer(
"MONTHLY", start, end, dimensions=["SERVICE"], metrics="UnblendedCost"
).get_cost_and_usage_total_and_group_by()

get cost of resources by tags

I would like to get the cost of all resources in my account that contain a specific tag or set of tags. is this possible with this tool?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.