LoginSignup
17
13

More than 5 years have passed since last update.

AWSの利用料金をサっと確認するワンライナー

Last updated at Posted at 2017-11-28

概要

  • AWSの利用料金をサっと確認したい
  • AWS CLIとjqでサッと見やすく

準備

  • AWSアカウント
  • AWS CLIと利用準備
  • jq

awscliのバージョン

$ aws --version
aws-cli/1.12.2 Python/2.7.12 Linux/4.9.58-18.51.amzn1.x86_64 botocore/1.8.2

もちろんCygwinでも大丈夫です

$ aws --version
aws-cli/1.12.2 Python/2.7.13 CYGWIN_NT-10.0/2.9.0(0.318/5/3) botocore/1.8.2

jsonフォーマットでサッと

コマンド

aws ce get-cost-and-usage \
--region us-east-1 \
--time-period Start=`date '+%Y-%m'`-01,End=`date -d 'next month' '+%Y-%m'`-01 \
--granularity MONTHLY \
--metrics BlendedCost \
--group-by Type=DIMENSION,Key=SERVICE \
| jq -c '.ResultsByTime[].Groups[] | {(.Keys[]): .Metrics.BlendedCost.Amount}'

結果

{"AWS CloudTrail":"0"}
{"Amazon Elastic Block Store":"2.6873538247"}
{"Amazon Elastic Compute Cloud - Compute":"5.9323839592"}
{"Amazon Route 53":"0.5563328"}
{"Amazon Simple Email Service":"0"}
{"Amazon Simple Notification Service":"0.0000107185"}
{"Amazon Simple Queue Service":"0.0000004264"}
{"Amazon Simple Storage Service":"0.0428270202"}
{"AmazonCloudWatch":"0"}
{"Tax":"0.73"}

CSVでサッと

コマンド

aws ce get-cost-and-usage \
--region us-east-1 \
--time-period Start=`date '+%Y-%m'`-01,End=`date -d 'next month' '+%Y-%m'`-01 \
--granularity MONTHLY --metrics BlendedCost \
--group-by Type=DIMENSION,Key=SERVICE \
| jq -r '.ResultsByTime[].Groups[] | [(.Keys[]), .Metrics.BlendedCost.Amount] | @csv'

結果

"AWS CloudTrail","0"
"Amazon Elastic Block Store","2.6873538247"
"Amazon Elastic Compute Cloud - Compute","5.9323839592"
"Amazon Route 53","0.5563328"
"Amazon Simple Email Service","0"
"Amazon Simple Notification Service","0.0000107185"
"Amazon Simple Queue Service","0.0000004264"
"Amazon Simple Storage Service","0.0428270202"
"AmazonCloudWatch","0"
"Tax","0.73"

メモと振り返り

  • エンドポイントは us-east-1
  • 期間指定(--time-period)のEndは範囲の次の日を指定します
    • Endの指定日は期間に含まれません(The end date is exclusive.)
    • 式で書くと 日付 >= Start AND 日付 < End みたいに評価されるということですね
  • 日付までコマンドに埋め込んだのは、やり過ぎたかもしれないと反省している(見づらい)

リンク

AWS

jq

17
13
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
17
13