この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
中山(順)です
今日はみんな大好きなお金のネタですw
Cost ExplorerのAPIの提供が開始されました。
Introducing the AWS Cost Explorer API
併せて、AWS CLIにceコマンドも追加されました。
ce -AWS CLI Command Reference-
さわってみた (AWS CLI)
というわけで、さっそくCLIでさわってみたいと思います。
ちなみに、エンドポイントはバージニアリージョンのみで提供されています。
export AWS_DEFAULT_REGION="us-east-1"
提供されているサブコマンドは以下の4つです。
- get-cost-and-usage
- get-dimension-values
- get-reservation-utilization
- get-tags
この記事では、最初の2つのコマンドget-cost-and-usage
およびget-dimension-values
について紹介します。
get-dimension-values
このコマンドでは、料金のフィルタリングを行う際に利用できる値を確認することができます。
例えば、サービスごとの利用料金を表示させたいときに使用できる値(2017年の10月に私が実際に利用したサービスw)は以下の通りです。
aws ce get-dimension-values \
--time-period Start=2017-10-01,End=2017-11-01 \
--dimension SERVICE \
--context COST_AND_USAGE
{
"TotalSize": 11,
"DimensionValues": [
{
"Attributes": {},
"Value": "AWS CloudTrail"
},
{
"Attributes": {},
"Value": "AWS Config"
},
{
"Attributes": {},
"Value": "AWS Directory Service"
},
{
"Attributes": {},
"Value": "AWS Key Management Service"
},
{
"Attributes": {},
"Value": "EC2 - Other"
},
{
"Attributes": {},
"Value": "Amazon Elastic Compute Cloud - Compute"
},
{
"Attributes": {},
"Value": "Amazon Inspector"
},
{
"Attributes": {},
"Value": "Amazon Lex"
},
{
"Attributes": {},
"Value": "Amazon Simple Notification Service"
},
{
"Attributes": {},
"Value": "Amazon Simple Storage Service"
},
{
"Attributes": {},
"Value": "Tax"
}
],
"ReturnSize": 11
}
get-cost-and-usage
このコマンドは、期間や集計の単位(日/月)などを指定して料金を表示できます。
グルーピングやフィルタリングも可能です。
まず、月別の料金を表示してみます。
aws ce get-cost-and-usage \
--time-period Start=2017-09-01,End=2017-11-01 \
--granularity MONTHLY \
--metrics UnblendedCost
{
"ResultsByTime": [
{
"Estimated": false,
"TimePeriod": {
"Start": "2017-09-01",
"End": "2017-10-01"
},
"Total": {
"BlendedCost": {
"Amount": "1.1205283933",
"Unit": "USD"
}
},
"Groups": []
},
{
"Estimated": false,
"TimePeriod": {
"Start": "2017-10-01",
"End": "2017-11-01"
},
"Total": {
"BlendedCost": {
"Amount": "14.1349245945",
"Unit": "USD"
}
},
"Groups": []
}
]
}
次に、フィルタリングを行っています。
filter
オプションを使用して条件に合致する料金のみを表示させることができます。
ここではEC2に関する価格を表示してみたいと思います。
まずは、フィルターをJSONファイルとして作成します。
cat << EOF > ce_filter.json
{
"Dimensions": {
"Key": "SERVICE",
"Values": ["Amazon Elastic Compute Cloud - Compute", "EC2 - Other"]
}
}
EOF
作成したフィルターを指定して料金を表示してみます。
料金全体の6-7割くらいがEC2の料金で占められていたようです。
aws ce get-cost-and-usage \
--time-period Start=2017-10-01,End=2017-11-01 \
--granularity MONTHLY \
--metrics UnblendedCost \
--filter file://ce_filter.json
{
"ResultsByTime": [
{
"Estimated": false,
"TimePeriod": {
"Start": "2017-10-01",
"End": "2017-11-01"
},
"Total": {
"BlendedCost": {
"Amount": "9.8588610318",
"Unit": "USD"
}
},
"Groups": []
}
]
}
group-by
オプションを使用してより細かく価格を表示することもできます。
ここではサービスごとの価格を表示してみたいと思います。
aws ce get-cost-and-usage \
--time-period Start=2017-10-01,End=2017-11-01 \
--granularity MONTHLY \
--metrics UnblendedCost \
--group-by Type=DIMENSION,Key=SERVICE
{
"GroupDefinitions": [
{
"Type": "DIMENSION",
"Key": "SERVICE"
}
],
"ResultsByTime": [
{
"Estimated": false,
"TimePeriod": {
"Start": "2017-10-01",
"End": "2017-11-01"
},
"Total": {},
"Groups": [
{
"Keys": [
"AWS CloudTrail"
],
"Metrics": {
"BlendedCost": {
"Amount": "0",
"Unit": "USD"
}
}
},
{
"Keys": [
"AWS Config"
],
"Metrics": {
"BlendedCost": {
"Amount": "2.967",
"Unit": "USD"
}
}
},
{
"Keys": [
"AWS Directory Service"
],
"Metrics": {
"BlendedCost": {
"Amount": "0.0729171653",
"Unit": "USD"
}
}
},
{
"Keys": [
"AWS Key Management Service"
],
"Metrics": {
"BlendedCost": {
"Amount": "0.0004937424",
"Unit": "USD"
}
}
},
{
"Keys": [
"Amazon Elastic Block Store"
],
"Metrics": {
"BlendedCost": {
"Amount": "3.0857222006",
"Unit": "USD"
}
}
},
{
"Keys": [
"Amazon Elastic Compute Cloud - Compute"
],
"Metrics": {
"BlendedCost": {
"Amount": "6.7731388312",
"Unit": "USD"
}
}
},
{
"Keys": [
"Amazon Inspector"
],
"Metrics": {
"BlendedCost": {
"Amount": "0",
"Unit": "USD"
}
}
},
{
"Keys": [
"Amazon Lex"
],
"Metrics": {
"BlendedCost": {
"Amount": "0",
"Unit": "USD"
}
}
},
{
"Keys": [
"Amazon Simple Notification Service"
],
"Metrics": {
"BlendedCost": {
"Amount": "0.0000142504",
"Unit": "USD"
}
}
},
{
"Keys": [
"Amazon Simple Storage Service"
],
"Metrics": {
"BlendedCost": {
"Amount": "0.1656384046",
"Unit": "USD"
}
}
},
{
"Keys": [
"Tax"
],
"Metrics": {
"BlendedCost": {
"Amount": "1.07",
"Unit": "USD"
}
}
}
]
}
]
}
まとめ
このように、API/CLIでも(比較的簡単に)料金を確認・分析することができりようになりました。(フィルターを書くのはちょっとめんどうですが)
人が見て分析する場合にはこれまで通りマネージメントコンソールを見ればいいと思いますが、プロブラムへ情報を取り込んで何らかの処理をさせたいような場合には、このAPIの存在を思い出してみてください。
ではでは。