LoginSignup
0
0

More than 1 year has passed since last update.

aws cli で組織の子アカウント課金情報をOU単位で抽出したい

Last updated at Posted at 2023-01-20

Organizations使っていて、OU単位での課金情報取ろうとすると
大概じゃあCostCategolyを使ってみたいな話になるんですが

そうじゃねえんだよOUさえ分けてればアカウント単位での利用料金を
自動で振り分けしてレポートされるようにしたいんだよ
という思いで作ったスクリプトです。

#!/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=LINKED_ACCOUNT  \
| jq -c '.ResultsByTime[].Groups[]' | cut -d\" -f4,12,16 | sed -e 's/"/ /g' | awk -e '{printf "%-15s %8.2f USD\n",$1,$2}' > /var/tmp/AWSCostList

set OUID=ou-mxus-12345678
set OUNAME=MyOU1
echo OU : $OUNAME , OUID : $OUID

foreach AWSID (`aws organizations list-accounts-for-parent --profile $PROFILE --parent-id ${OUID} --query 'Accounts[].Id' --output text`)
 egrep $AWSID /var/tmp/AWSCostList
end

実行結果。

$ ./BillUnderOU.sh 
TimePeriod : 2022-12-01 to 2023-01-01
Region : us-east-1
PROFILE : orgprofile
OU : MyOU1 , OUID : ou-mxus-12345678
17xxxxxxxx58      143.03 USD
84xxxxxxxx89        4.38 USD

このスクリプトでは1 OUだけの結果を出してますが、増えてきたらループで回せばいい感じになるかと思います。

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