AWS Script For Getting Bill

export AWS_CLOUDWATCH_HOME=/home/charlie/cloudwatch/CloudWatch-
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- 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..”
echo “${service} $COST `date +%s`” |nc 2023



Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s