LoginSignup
1
0

More than 1 year has passed since last update.

aws cli で組織子アカウントのサービス単位課金を抽出したい

Last updated at Posted at 2023-01-20

ちょいコツがいる処理だったので備忘も兼ねて投稿

組織アカウントのプロファイルでサービス単位の課金情報は
こんな風にSERVICE の DIMENSIONで取れますが

aws ce get-cost-and-usage --profile ${PROFILE} --region ${REGION} --time-period Start=${LASTMONTH},End=${THISMONTH}\
  --granularity MONTHLY --metrics UnblendedCost  --group-by Type=DIMENSION,Key=SERVICE \

子アカウント(LINKED ACCOUNT)単位での情報を取ろうとすると、既にDIMENSIONが
使われていて指定できない。
でも子アカウント毎に都度profile作るのは面倒だしメンテもしたくない。
サポートに訊いたら--filter使えやって話だったので早速設定。
https://docs.aws.amazon.com/ja_jp/cli/latest/reference/ce/get-cost-and-usage.html

filter ファイルを準備

$ cat filter-linkedaccount 
{ "Dimensions": { "Key": "LINKED_ACCOUNT",  "Values":  [ "17xxxxxxxx58" ] } }

実行コード。実行日の前月の課金情報を取得

#!/bin/csh 

set LASTMONTH=` date +%Y-%0m-01  -d '1 month ago'`
set THISMONTH=` date +%Y-%0m-01`
set REGION=us-east-1
set PROFILE=orgprofile

echo TimePeriod : $LASTMONTH to $THISMONTH
echo Region : $REGION
echo PROFILE : $PROFILE

aws ce get-cost-and-usage --profile ${PROFILE} --region ${REGION} \
  --time-period Start=${LASTMONTH},End=${THISMONTH}\
  --granularity MONTHLY --metrics UnblendedCost  --group-by Type=DIMENSION,Key=SERVICE \
  --filter file://filter-linkedaccount \
 |  jq -c '.ResultsByTime[].Groups[]' | awk -F'"' '{printf "%-50s %10.3f \n",$4,$12}'

実行結果。CostExplorer の値と比較したけど正しい値拾えてました。

$ ./ServiceBillUnderOU.sh 
TimePeriod : 2022-12-01 to 2023-01-01
Region : us-east-1
PROFILE : orgprofile
AWS CloudTrail                                          0.000 
AWS Config                                              0.102 
AWS Key Management Service                              0.000 
AWS Secrets Manager                                     0.000 
AWS Security Hub                                        0.000 
AWS Service Catalog                                     0.000 
EC2 - Other                                           122.220 
Amazon Elastic Compute Cloud - Compute                  7.691 
Amazon GuardDuty                                        0.000 
Amazon Simple Notification Service                      0.000 
Amazon Simple Queue Service                             0.000 
Amazon Simple Storage Service                           0.015 
AmazonCloudWatch                                        0.000 
Tax                                                    13.000 

AWSのCLIリファレンスは初心者に厳しい。もっと設定のサンプルを載せて欲しいといつも思う。

1
0
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
1
0