AWS Script For Getting Bill

#!/bin/sh
export AWS_CLOUDWATCH_HOME=/home/charlie/cloudwatch/CloudWatch-1.0.13.4
export JAVA_HOME=/usr/lib/jvm/default-java

# Get the timestamp from 5 hours ago, to avoid getting > 1440 metrics (which errors).
# also, remove the +0000 from the timestamp, because the cloudwatch cli tries to enforce
# ISO 8601, but doesn’t understand it.
DATE=$(date –iso-8601=hours -d “5 hours ago” |sed s/+.*//)

#echo $COST

SERVICES=’AmazonS3 ElasticMapReduce AmazonRDS AmazonDynamoDB AWSDataTransfer AmazonEC2 AWSQueueService’

for service in $SERVICES; do

COST=$(/home/charlie/cloudwatch/CloudWatch-1.0.13.4/bin/mon-get-stats EstimatedCharges –aws-credential-file ~/.ec2_credentials –namespace “AWS/Billing” –statistics Sum –dimensions “ServiceName=${service},Currency=USD” –start-time $DATE |tail -1 |awk ‘{print $3}’)

if [ -z $COST ]; then
echo “failed to retrieve $service metric from CloudWatch..”
else
echo “stats.prod.ops.billing.ec2_${service} $COST `date +%s`” |nc graphite.example.com 2023
fi

done

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s