开发者

Amazon AWS: How Do I Programmatically Calculate My Spending?

开发者 https://www.devze.com 2023-03-14 03:01 出处:网络
We are using Amazon\'s EC2 We would like开发者_Python百科 to be able to write a program that calculates how much we spent between time to time?

We are using Amazon's EC2 We would like开发者_Python百科 to be able to write a program that calculates how much we spent between time to time?

We figured that it's possible to download the usage report and apply the pricing rules.. Is there any better way? Direct API? Is it possible to do it in real-time(the reports are somewhat delayed)

Usage-report: https://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=usage-report


Unfortunately Amazon don't provide real-time data (and, in fact, you'll probably notice that the accuracy of Amazon's usage reports will vary within the current billing period), nor do they provide an API to query their current prices.

We currently do this for our systems and have found a combination of the following works well:

  • Real time estimates: estimate charges based on monitoring data (number of instances, start date, terminate date, bandwidth and IO usage per-box monitored by a custom agent looking at /proc). We pessimistically over-estimate here.
  • Recent estimate: replace total costs for real-time estimates with charges based on usage report.
  • Bill: replace total costs based on actual bill

At each estimation stage we do a very primitive adjustment so the totals work out: we apply a credit/charge to bring the figure in line with the next level of estimate.

You can spend a LOT of effort trying to get these estimates as accurate as possible - it largely depends on how much data collection and maintenance overhead you're willing to bear.

We account for usage on a per-application basis (where each Instance may be reused within an instance-hour for multiple applications) so the collection overhead and maintenance of this code is worth it.


First thing is to enable detailed billing export to a S3 bucket (see here)

Then I wrote a simplistic server in Python that retrieves your detailed bill and breaks it down per service-type and usage type (see it on this GitHib repo). The data is analyzed in real time and explains your current monthly expenses.

Thus you can check anytime what your costs are and which services cost you the most etc.

If you tag your EC2 instances, S3 buckets etc, they will also show up on a dedicated line.


Since this question was asked, a lot has changed with AWS billing. With AWS's Detailed Billing Files and programmatic access, you can get detailed, hourly billing data for all of your AWS usage.

Instead of writing your own parser. You should try a tool like Cloudability's AWS Cost Analytics to consolidate and break down your spending data.


As this question was asked long back, a lot has changed now. You can access cost programmatically using Cost Explorer API. With this API data will be available for last 12 months. Also each paginated request will incur $0.01. Though the cost will not available in real time, it takes 24 hours as Cost Explorer refreshes your cost data at least once every 24 hours where it uses the same data that is used to generate AWS Cost and Usage Reports.

Reference: Analyzing your costs with Cost Explorer


Found this Python code to scrape and download the XML-reports:

check_aws_usage.py


There are a few sites that will do analysis for you (stuff like trusted advisor) but you'll probably end up writing your own tool for your needs. For my clients I use my own tool (which is free for everyone). You can get current prices using ec2price/rdsprice python script rippers from github and apply prices to your current account - I think that is what everybody else does.


Answer update: there is now an API for this

AWS Cost Explorer Service

0

精彩评论

暂无评论...
验证码 换一张
取 消