Skip to content

Instantly share code, notes, and snippets.

@katzefudder
Created January 10, 2021 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katzefudder/0c57a9300b0f0718b88f46539f9ae446 to your computer and use it in GitHub Desktop.
Save katzefudder/0c57a9300b0f0718b88f46539f9ae446 to your computer and use it in GitHub Desktop.
fetch the last months' costs from AWS
#!/bin/bash
# displays the last month's costs in USD for tagged resources
# https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html
# FIRST=$(date --date="$(date +'%Y-%m-01') - 1 month" +%s) # date on general Linux distributions
# LAST=$(date --date="$(date +'%Y-%m-01') - 1 second" +%s)
PROJECT=del2
FIRST=$(date -v1d -v-1m +%Y-%m-%d) # date on Mac OS X
LAST=$(date -v1d -v-1d +%Y-%m-%d)
aws ce get-cost-and-usage --time-period Start=$FIRST,End=$LAST --granularity=MONTHLY --metrics BlendedCost --filter '{"Tags": {"Key": "Project","Values": ["'$PROJECT'"]}}' | jq -r '.ResultsByTime[].Total.BlendedCost.Amount'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment