ce get-cost-and-usage とは
aws ce get-cost-and-usage はAWS Cost Explorer APIを使ってAWSのコストと使用量をCLIで取得するコマンド。マネジメントコンソールのCost Explorerで確認できる情報をコマンドラインから取得できる。
基本的な使い方
--time-period、--granularity、--metrics は必須パラメータ。
$ aws ce get-cost-and-usage \
--time-period Start=2026-04-01,End=2026-05-01 \
--granularity MONTHLY \
--metrics BlendedCost
--granularity には DAILY、MONTHLY、HOURLY を指定できる。
--metrics には以下を指定できる。
| メトリクス | 説明 |
|---|---|
BlendedCost | ブレンドレートを適用したコスト |
UnblendedCost | オンデマンドレートを適用したコスト |
AmortizedCost | リザーブドインスタンスや Savings Plans の前払い費用を日割りしたコスト |
UsageQuantity | 使用量 |
NormalizedUsageAmount | 正規化した使用量 |
サービス別にグループ化する
--group-by を指定するとサービスごとのコストを取得できる。
$ aws ce get-cost-and-usage \
--time-period Start=2026-04-01,End=2026-05-01 \
--granularity MONTHLY \
--metrics BlendedCost \
--group-by Type=DIMENSION,Key=SERVICE
レスポンスの Groups にサービスごとのコストが含まれる。
{
"ResultsByTime": [
{
"TimePeriod": {
"Start": "2026-04-01",
"End": "2026-05-01"
},
"Groups": [
{
"Keys": ["Amazon EC2"],
"Metrics": {
"BlendedCost": {
"Amount": "123.45",
"Unit": "USD"
}
}
},
...
]
}
]
}
DIMENSION の Key には SERVICE のほかに LINKED_ACCOUNT、REGION、USAGE_TYPE などを指定できる。
アカウント別にグループ化する
Organizationsを使っている場合、リンクアカウントごとのコストを確認できる。
$ aws ce get-cost-and-usage \
--time-period Start=2026-04-01,End=2026-05-01 \
--granularity MONTHLY \
--metrics UnblendedCost \
--group-by Type=DIMENSION,Key=LINKED_ACCOUNT
特定サービスに絞り込む
--filter を指定すると特定のサービスに絞り込める。
$ aws ce get-cost-and-usage \
--time-period Start=2026-04-01,End=2026-05-01 \
--granularity DAILY \
--metrics BlendedCost \
--filter '{"Dimensions": {"Key": "SERVICE", "Values": ["Amazon Elastic Compute Cloud - Compute"]}}'
複数サービスをまとめて指定するには Values に複数の値を渡す。
$ aws ce get-cost-and-usage \
--time-period Start=2026-04-01,End=2026-05-01 \
--granularity MONTHLY \
--metrics BlendedCost \
--filter '{"Dimensions": {"Key": "SERVICE", "Values": ["Amazon Elastic Compute Cloud - Compute", "Amazon Relational Database Service", "Amazon Simple Storage Service"]}}'
タグで絞り込む
リソースにタグを付けている場合、タグで絞り込める。事前にCost Explorerでタグの有効化が必要。
$ aws ce get-cost-and-usage \
--time-period Start=2026-04-01,End=2026-05-01 \
--granularity MONTHLY \
--metrics BlendedCost \
--filter '{"Tags": {"Key": "Environment", "Values": ["production"]}}'
jq でコストだけ抽出する
jq を使ってサービスとコストだけを見やすく整形できる。
$ aws ce get-cost-and-usage \
--time-period Start=2026-04-01,End=2026-05-01 \
--granularity MONTHLY \
--metrics BlendedCost \
--group-by Type=DIMENSION,Key=SERVICE \
| jq -r '.ResultsByTime[].Groups[] | [.Keys[0], .Metrics.BlendedCost.Amount] | @tsv' \
| sort -k2 -rn
コストの降順で並べた結果が得られる。
Amazon Elastic Compute Cloud - Compute 0
Amazon Relational Database Service 1.2491132517
AAmazon Simple Storage Service 0.3164459665
\手を動かしながらTerraformを学びたい人にオススメ!/
