Skip to main content

awscost

PyPI version Build Status

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

Supports python 3.8 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.

Metadata

Release files for awscost 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for awscost 0.4.0
File Size Uploaded Provenance
awscost-0.4.0.tar.gz 12.0 kB Without provenance Details

Release files / awscost-0.4.0.tar.gz

Download URL awscost-0.4.0.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
79206094786d2371a34304660590b9c0bda5d9d5444a6c012718cdbf50795dca
BLAKE2b-256 checksum
How to use checksums
05ad1917c019fed61dcb5168bbb6cd486695a324e9d272b43dba4a538568ef33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.7

Security for awscost 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page