20181020 shell script check your AWS EC2 instance cost

System Blog

Shell script check your AWS EC2 cost

 


Warning, if you plan to setup a cronjob to keep an eye on your AWS EC2 bill, bear in mind every costExplore API call will be charged $0.01.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
year=$(date +%Y)
month=$(date +%m)
calMonth=$(cal $month $year)
lastDay=$(echo $calMonth|awk '{printf $NF}')

cost=$(aws ce get-cost-and-usage --time-period Start=$year-$month-01,End=$year-$month-$lastDay --granularity MONTHLY --metrics BlendedCost|jq -r '.ResultsByTime[0].Total.BlendedCost.Amount')
echo $cost
if [ $(echo "$cost > 20"|bc) -eq 1 ]
then
 echo 'WARNING, current Monthly bill is more than 20 dollars.'|mailx -s "EC2 is fucked" YOUR_EMAIL@something.com
else
 echo 'good'
fi





 

Posted by: leaonow on: Oct 20, 2018

 

Your Name:

OneTime Password:



Pic of the day