17
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

17
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

追加

少し気の利いた感じになるように修正したバージョンです。

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 '.ResultsByTime[].Groups | map({(.Keys[0]): (.Metrics.BlendedCost.Amount | tonumber)}) | add | .Total = ([.[]] | add)'

結果サンプル

{
  "AWS Amplify": 0.0002415038,
  "AWS AppSync": 0.0557706202,
  "AWS Certificate Manager": 0,
  "AWS CloudFormation": 0,
  "AWS CloudShell": 0.0000213415,
  "AWS CloudTrail": 27.4737555,
  "AWS Config": 3.282,
  "AWS End User Messaging": 0.07451,
  "AWS Glue": 0,
  "AWS Key Management Service": 8.212037884,
  "AWS Lambda": 0.0066676012,
  "AWS Secrets Manager": 3.2410312066,
  "AWS Security Hub": 5.9983593788,
  "AWS Step Functions": 2.96E-7,
  "Amazon API Gateway": 0.0007115707,
  "Amazon Athena": 0.00155,
  "Amazon CloudFront": 0,
  "Amazon Connect": 3.511999999,
  "Amazon Connect Customer Profiles": 0.01,
  "Amazon DynamoDB": 0.0437196857,
  "Amazon EC2 Container Registry (ECR)": 0.2209780434,
  "EC2 - Other": 1.6077419596,
  "Amazon GuardDuty": 8.61806964,
  "Amazon Inspector": 6.4762311277,
  "Amazon Kinesis": 7.2964195396,
  "Amazon Redshift": 0.0002504,
  "Amazon Simple Notification Service": 0.0004171709,
  "Amazon Simple Queue Service": 0.1373739122,
  "Amazon Simple Storage Service": 0.5766075792,
  "AmazonCloudWatch": 8.3585480795,
  "AmazonConnectCases": 0.12,
  "Claude Sonnet 4.5 (Amazon Bedrock Edition)": 0.0050028,
  "Claude Sonnet 4.6 (Amazon Bedrock Edition)": 0.2160015,
  "CloudWatch Events": 0.000026,
  "Contact Center Telecommunications (service sold by AMCS, LLC)": 30.9882514815,
  "Contact Lens for Amazon Connect": 0,
  "Total": 116.5342958211
}
17
14
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
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?