LoginSignup
2
1

More than 3 years have passed since last update.

awsのcostを見やすくtable表示するawscostというコマンドを作った

Last updated at Posted at 2020-02-10

awscostというawsでかかっている料金をとっても見やすく表示するコマンドを作りました。

作った理由

  • 会社にアカウントが多いため、アカウントの行き来をしてcost explorerを見るのが辛い
  • cost explorerの機能が増えすぎて年々使いにくくなっている。
  • 最低限、Service別でいくらかかっているのか確認したいだけのケースがほとんど

インストール

pythonで作っているので、pythonが必要です。

pip install awscost

python3.6以上が必要です

使い方

$ awscost
key                                       2019-11    2019-12    2020-01    2020-02
--------------------------------------  ---------  ---------  ---------  ---------
Total                                       67.15      72.22      68.09      20.97
EC2 - Other                                 33.63      34.58      34.6        9.96
Amazon Elastic Compute Cloud - Compute      17.13      17.11      17.11       4.92
Tax                                          6.11       6.59       6.17       1.85
AWS CloudTrail                               4.44       6.17       4.38       1.38
AWS Key Management Service                   4          4          4          1.23
AWS Cost Explorer                            0.56       2.8        0.02       1.02
AmazonCloudWatch                             0.88       1.21       1.21       0.37
Amazon Simple Storage Service                0.4        0.54       0.45       0.14
Amazon Route 53                              0.1        0.1        0.1        0.1
AWS Lambda                                   0          0          0          0
Amazon DynamoDB                              0          0          0          0
Amazon Elastic File System                   0          0          0          0
Amazon SageMaker                             0          0          0          0
Amazon Simple Notification Service           0          0          0          0
Amazon Simple Queue Service                  0          0          0          0
Refund                                      -0.09      -0.89      -0.01       0
Amazon Polly                                 0          0          0.06       0

defaultではtotal+service別の料金が表示されます。

各種dimensionで集計してみる

instance type別

$ awscost -d INSTANCE_TYPE -p 3
key                      2019-11    2019-12    2020-01    2020-02
---------------------  ---------  ---------  ---------  ---------
Total                      67.15      72.22      68.09      20.97
NoInstanceType             50.03      55.11      50.98      16.05
t2.small                   16.56      17.11      17.11       4.92
t2.micro                    0.57       0          0          0
...

Service別かつOPERATION別(複数でgroup by)

$ awscost -d SERVICE -d OPERATION -p 3
key                                                              2019-11    2019-12    2020-01    2020-02
-------------------------------------------------------------  ---------  ---------  ---------  ---------
Total                                                              67.15      72.22      68.09      20.97
EC2 - Other,NatGateway                                             32.58      33.46      33.48       9.63
Amazon Elastic Compute Cloud - Compute,RunInstances                17.13      17.11      17.11       4.92
AWS CloudTrail,None                                                 4.44       6.17       4.38       1.38
AWS Key Management Service,Unknown                                  4          4          4          1.23
AWS Cost Explorer,GetCostAndUsage                                   0.55       2.8        0.02       1.02
Tax,NatGateway                                                      3.26       3.35       3.35       0.96
Tax,RunInstances                                                    1.72       1.71       1.71       0.49
Tax,NoOperation                                                     1.13       1.53       1.11       0.4
...

dimensionは以下を参照

各種formatで出力してみる

github

$ awscost -p 3 -t github
| key                                    |   2019-11 |   2019-12 |   2020-01 |   2020-02 |
|----------------------------------------|-----------|-----------|-----------|-----------|
| Total                                  |     67.15 |     72.22 |     68.09 |     20.97 |
| EC2 - Other                            |     33.63 |     34.58 |     34.6  |      9.96 |
| Amazon Elastic Compute Cloud - Compute |     17.13 |     17.11 |     17.11 |      4.92 |
| Tax                                    |      6.11 |      6.59 |      6.17 |      1.85 |
...

tsv

$ awscost -p 3 -t tsv
key                                       2019-11   2019-12   2020-01   2020-02
Total                                       67.15     72.22     68.09     20.97
EC2 - Other                                 33.63     34.58     34.6       9.96
Amazon Elastic Compute Cloud - Compute      17.13     17.11     17.11      4.92
Tax                                          6.11      6.59      6.17      1.85
AWS CloudTrail                               4.44      6.17      4.38      1.38
...

help

$ awscost --help
Usage: awscost [OPTIONS]

Options:
  --debug / --no-debug            enable debug logging. (default: False)
  --profile TEXT                  aws profile name.
  -g, --granularity [DAILY|MONTHLY]
                                  granularity. (default: MONTHLY)
  -p, --point INTEGER             duration. if granularity is MONTHLY, 7 month
                                  ago start. if granularity is DAILY, 7 day
                                  ago start. (default: 7)
  --start TEXT                    range of start day. default is 7 month ago.
  --end TEXT                      range of end day. default is now.
  -t, --tablefmt TEXT             tabulate format. (default: simple)
  -d, --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]
                                  group by dimensions. (default: ["SERVICE"])
  --filter LOADS                  filter of dimensions. default is no filter.
  --metrics [BlendedCost|UnblendedCost|AmortizedCost|NetAmortizedCost|NetUnblendedCost|UsageQuantity|NormalizedUsageAmount]
                                  metrics. (default: UnblendedCost)
  --total / --no-total            include total cost. (default: True)
  --help                          Show this message and exit.
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1