Last active
April 6, 2016 09:20
-
-
Save okiwan/67d97f5f9975887d9ec9868c7bb235cd to your computer and use it in GitHub Desktop.
AWS Billing Bash script to consolidate costs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env /bin/bash | |
| # Checks if element (first argument) is in array (second argument) | |
| elementIn() { | |
| local e | |
| for e in "${@:2}"; do [[ "\"$e\"" == "$1" ]] && return 0; done | |
| return 1 | |
| } | |
| # Id strings used to identify resources | |
| PROJECT_STRING_IDS=( | |
| "resource1" | |
| "resource2" | |
| "resource3" | |
| ) | |
| # By default the scripts downloads the report from the previous month, | |
| # but you can specify reports from other monts (Y-m format) | |
| if [ -z "$1" ]; then | |
| if [[ "$OSTYPE" == "darwin"* ]]; then | |
| PERIOD=$(date -j -v -1m +%Y-%m) | |
| else | |
| PERIOD=$(date +%Y-%m -d 'last month') | |
| fi | |
| else | |
| PERIOD=$1 | |
| fi | |
| SRC_FILE=488669976369-aws-billing-detailed-line-items-with-resources-and-tags-$PERIOD.csv | |
| SRC_PATH=s3://my-beloved-bucket | |
| TMP_PATH=$(mktemp -d) | |
| COST_FORMACION=0 | |
| COST_IMPACT=0 | |
| COST_OTHERS=0 | |
| LINE_COUNTER=1 | |
| # Download billing report automatically stored on S3 bucket | |
| echo "Descargando reporte..." | |
| aws s3 cp $SRC_PATH/$SRC_FILE.zip $TMP_PATH --only-show-errors | |
| # Report is not available | |
| if ! [ -f $TMP_PATH/$SRC_FILE.zip ]; then | |
| echo "El reporte no ha podido ser descargado." | |
| exit 1 | |
| fi | |
| unzip -qq -o -d $TMP_PATH $TMP_PATH/$SRC_FILE | |
| # Calculation (depressing performance, but whatever...) | |
| echo "Calculando..." | |
| while read line; do | |
| CLEAN_LINE=$(echo "$line" | awk -F'"' -v OFS='"' '{ for (i=2; i<=NF; i+=2) gsub(",", "", $i) } 1') | |
| IFS=',' read -ra sline <<< "$CLEAN_LINE" | |
| if [[ ${sline[9]} != "\"\"" ]]; then | |
| elementIn ${sline[19]} ${PROJECT_STRING_IDS[@]} | |
| if [[ $? -eq 0 ]]; then | |
| COST_FORMACION=$(perl -e "print $COST_FORMACION+${sline[18]}") | |
| else | |
| if [[ ${sline[19]} != "\"\"" ]]; then | |
| COST_IMPACT=$(perl -e "print $COST_IMPACT+${sline[18]}") | |
| else | |
| COST_OTHERS=$(perl -e "print $COST_OTHERS+${sline[18]}") | |
| fi | |
| fi | |
| # Debugging | |
| # echo "$LINE_COUNTER => $COST_FORMACION vs $COST_IMPACT" | |
| # LINE_COUNTER=$(($LINE_COUNTER + 1)) | |
| fi | |
| done < "$TMP_PATH/$SRC_FILE" | |
| # Clean up | |
| if [ -n "$TMP_PATH" ]; then | |
| rm -r $TMP_PATH | |
| fi | |
| echo "------------------------------------" | |
| echo "Formacion... $COST_FORMACION" | |
| echo "Impact...... $COST_IMPACT" | |
| echo "Others...... $COST_OTHERS" | |
| echo "------------------------------------" | |
| # Just in case, we like to leave everything just as we found it | |
| unset IFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment