Skip to content

Instantly share code, notes, and snippets.

@ohsawa0515
Last active November 29, 2019 02:05
Show Gist options
  • Save ohsawa0515/2ec410e82143170f1f30c3fe4b9ef32d to your computer and use it in GitHub Desktop.
Save ohsawa0515/2ec410e82143170f1f30c3fe4b9ef32d to your computer and use it in GitHub Desktop.
Calculate Aurora backup storage cost
#!/bin/bash -eu
# Tokyo region
price=0.023
region='ap-northeast-1'
aws_accound_id=$(aws sts get-caller-identity --query 'Account' --output text)
echo "aws_accound_id,aurora_cluster,total_bk_storage,days,avg_bk_storage_per_month,avg_bk_storage_per_month_gb,cost_bk_storage"
aurora_clusters=$(aws cloudwatch list-metrics \
--namespace AWS/RDS \
--metric-name TotalBackupStorageBilled \
--query 'Metrics[].Dimensions[0].Value' \
--output text)
for aurora_cluster in ${aurora_clusters[@]}; do
bk_storage_datapoints=$(aws cloudwatch get-metric-statistics \
--namespace AWS/RDS \
--metric-name TotalBackupStorageBilled \
--dimensions Name=DBClusterIdentifier,Value=$aurora_cluster \
--statistics Average \
--start-time $(date --date "$(date +"%Y%m01")" "+%Y-%m-%dT%H:%M:%S") \
--end-time $(date --date "$(date +"%Y%m01") 1 second ago + 1 month" "+%Y-%m-%dT%H:%M:%S") \
--period 86400 \
--query 'Datapoints[].Average' --output text)
total_bk_storage=0
days=0
for bk_storage_datapoint in ${bk_storage_datapoints[@]}; do
total_bk_storage=$(awk "BEGIN { print $total_bk_storage + $bk_storage_datapoint }")
days=$((days + 1))
done
if [ $days -eq 0 ]; then
continue
fi
# 当月分の平均バックアップストレージ量(Bytes)
avg_bk_storage_per_month=$(awk "BEGIN { print $total_bk_storage / ($days * 24) }") # 1day = 24hours
# 当月分の平均バックアップストレージ量(GB)
avg_bk_storage_per_month_gb=$(awk "BEGIN { print $avg_bk_storage_per_month / 1000 / 1000 / 1000 }")
# バックアップストレージ料金
cost_bk_storage=$(awk "BEGIN { print $avg_bk_storage_per_month_gb * $price }")
echo "${aws_accound_id},${aurora_cluster},${total_bk_storage},${days},${avg_bk_storage_per_month},${avg_bk_storage_per_month_gb},${cost_bk_storage}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment