Cloud Costs: Charting Daily EC2 Usage and Cost
Tags: blog, cloudcosts
Published: 2023-04-19 19:42:58

---
title: Cloud Costs: Charting Daily EC2 Usage and Cost
published: true
date: 2023-03-22 09:25:44 UTC
tags: Blog,CloudCosts
canonical_url: https://unskript.com/cloud-costs-charting-daily-ec2-usage-and-cost/
---

<p>When you buy a t-shirt at the store, you (generally) pay the same price for the shirt if you are an XS or a 2XL.  In the cloud, the size of the Virtual machine you select has VERY large implications in the cost.  At AWS, launching a t2.nano instance costs $.0058 an hour – or 14 cents a day, while a t3.2xlarge instance is $.4628 – or $11.11 a day.</p>
<p>When sizing a new system, it is common to go a “bit larger” in size to ensure that the service performs well.  This has worked well for AWS, as <a href="https://www.cnbc.com/2021/09/05/how-amazon-web-services-makes-money-estimated-margins-by-service.html">over 50% of their revenue</a> comes from EC2 instances. In a <a href="https://unskript.com/keeping-your-cloud-costs-in-check-automated-aws-cost-charts-and-alerting/">recent post</a>, we built an automated RunBook to examine our daily spend for each AWS product, and EC2 (the green line) is far and away our biggest cost center:</p>
<p><img decoding="async" src="https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-11.03.59.jpg?resize=300%2C153&amp;ssl=1" alt="AWS costs over the last 7 days" width="300" height="153" srcset="https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-11.03.59.jpg?resize=300%2C153&amp;ssl=1 300w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-11.03.59.jpg?resize=1024%2C522&amp;ssl=1 1024w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-11.03.59.jpg?resize=768%2C392&amp;ssl=1 768w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-11.03.59.jpg?resize=1116%2C569&amp;ssl=1 1116w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-11.03.59.jpg?resize=806%2C411&amp;ssl=1 806w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-11.03.59.jpg?resize=558%2C284&amp;ssl=1 558w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-11.03.59.jpg?resize=655%2C334&amp;ssl=1 655w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-11.03.59.jpg?resize=24%2C12&amp;ssl=1 24w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-11.03.59.jpg?w=1228&amp;ssl=1 1228w" sizes="(max-width: 300px) 100vw, 300px" data-recalc-dims="1"></p>
<p> </p>
<p>When looking at your highest cost center – you may wish to drill deeper into what these expenses are.  In this post, we’ll break down our daily EC2 spend by the types of instances that are running.</p>
<h2>Creating the RunBook</h2>
<p>In our last post, we built a RunBook that used the AWS Cost and Usage report to break down unSkript’s costs product. By running the report daily, we could chart our daily cost spend per product, and if the change day over day exceeded a threshold, we could send an alert.</p>
<p>To study our daily EC2 usage and spend, we will use the same Cost and Usage report, and the RunBook is essentially the same – just with a different SQL query into the table.  Rather than recreate the RunBook, we will simply Duplicate the RunBook in the unSkript UI, and save it with a new name:</p>
<p><img decoding="async" loading="lazy" src="https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.21.11.jpg?resize=300%2C218&amp;ssl=1" alt="Duplicating a RunBook" width="300" height="218" srcset="https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.21.11.jpg?resize=300%2C218&amp;ssl=1 300w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.21.11.jpg?resize=1024%2C745&amp;ssl=1 1024w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.21.11.jpg?resize=768%2C559&amp;ssl=1 768w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.21.11.jpg?resize=1116%2C812&amp;ssl=1 1116w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.21.11.jpg?resize=806%2C586&amp;ssl=1 806w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.21.11.jpg?resize=558%2C406&amp;ssl=1 558w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.21.11.jpg?resize=655%2C477&amp;ssl=1 655w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.21.11.jpg?resize=24%2C17&amp;ssl=1 24w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.21.11.jpg?w=1292&amp;ssl=1 1292w" sizes="(max-width: 300px) 100vw, 300px" data-recalc-dims="1"></p>
<p>To run a AWS Redshift query, we need a SecretArn, the SQL Query, AWS Region and Redshift Cluster and database details.</p>
<p>The RunBook generates these all for us, but we need to update the SqlQuery variable to query EC2 data:</p>

```
sqlQuery = f"SELECT date_part(day, cast(lineitem_usagestartdate as date)) as day, product_instancetype,SUM(lineitem_usageamount)::numeric(37, 4) AS usage_hours, SUM((lineitem_unblendedcost)::numeric(37,4)) AS usage_cost FROM {tableName} WHERE length(lineitem_usagestartdate)&gt;8 AND product_productfamily = 'Compute Instance' AND pricing_unit IN ('Hours', 'Hrs') GROUP BY day, product_instancetype ORDER BY 1 DESC, 3 DESC, 2 
```
<p>The next few steps of this RunBook is unchanged from the Product Cost RunBook:</p>
<p><img decoding="async" loading="lazy" src="https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50.jpg?resize=945%2C546&amp;ssl=1" alt="" width="945" height="546" srcset="https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=1024%2C592&amp;ssl=1 1024w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=300%2C173&amp;ssl=1 300w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=768%2C444&amp;ssl=1 768w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=1536%2C887&amp;ssl=1 1536w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=2048%2C1183&amp;ssl=1 2048w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=1612%2C931&amp;ssl=1 1612w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=1116%2C645&amp;ssl=1 1116w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=806%2C466&amp;ssl=1 806w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=558%2C322&amp;ssl=1 558w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=655%2C378&amp;ssl=1 655w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?resize=24%2C14&amp;ssl=1 24w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot_2023-03-20_at_14_27_50-scaled.jpg?w=1890&amp;ssl=1 1890w" sizes="(max-width: 945px) 100vw, 945px" data-recalc-dims="1"></p>
<p>We take the Ann and the query – and run the query – then pull the results into the RunBook.</p>
<p>We must now change the chart – since the inputs are different. Our Columns are now “product_instancetype” and the y values are “usage_cost.”</p>
<p>Charting this data helps us see which instance types are costing the most money:</p>
<p><img decoding="async" loading="lazy" src="https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=945%2C460&amp;ssl=1" alt="Daily EC2 costs by size" width="945" height="460" srcset="https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=1024%2C498&amp;ssl=1 1024w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=300%2C146&amp;ssl=1 300w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=768%2C374&amp;ssl=1 768w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=1536%2C747&amp;ssl=1 1536w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=1612%2C784&amp;ssl=1 1612w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=1116%2C543&amp;ssl=1 1116w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=806%2C392&amp;ssl=1 806w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=558%2C271&amp;ssl=1 558w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=655%2C319&amp;ssl=1 655w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?resize=24%2C12&amp;ssl=1 24w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-14.52.44.jpg?w=1850&amp;ssl=1 1850w" sizes="(max-width: 945px) 100vw, 945px" data-recalc-dims="1"></p>
<p>Our daily costs are very much proportional to size, with the 2xLarge and Large instances accounting for ~$20/day.  It is interesting to note that earlier this month, our t2.large went down at the same time our t2.micro grew – this could possibly be interpreted as resizing of an EC2 instance that was too large.</p>
<p> </p>
<p>Finally, we can build alerts to tell us if any of our costs jump by over $1 a day, or 10%.  If the costs make a jump we can send the chart EC2 usage to Slack using the <em>Send Image To Slack</em> Action (coming this week to our Open Source).  We also send this image every Monday for a historical record:</p>
<p><img decoding="async" loading="lazy" src="https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-15.16.00.jpg?resize=874%2C546&amp;ssl=1" alt="Slack message with the chart" width="874" height="546" srcset="https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-15.16.00.jpg?w=874&amp;ssl=1 874w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-15.16.00.jpg?resize=300%2C187&amp;ssl=1 300w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-15.16.00.jpg?resize=768%2C480&amp;ssl=1 768w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-15.16.00.jpg?resize=806%2C504&amp;ssl=1 806w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-15.16.00.jpg?resize=558%2C349&amp;ssl=1 558w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-15.16.00.jpg?resize=655%2C409&amp;ssl=1 655w, https://i0.wp.com/unskript.com/wp-content/uploads/2023/03/Screenshot-2023-03-20-at-15.16.00.jpg?resize=24%2C15&amp;ssl=1 24w" sizes="(max-width: 874px) 100vw, 874px" data-recalc-dims="1"></p>
<p> </p>
<h2>Conclusion</h2>
<p>When it comes to FinOps – the team in charge of understanding and accounting for your cloud bill, one of the most critical tools to have is observability into your daily spend. If there is a large change – that might be ok – but it is good to know in advance, and double check that the increase is valid – thereby potentially avoiding a large bill at the end of the month.</p>
<p> </p>
<p>In our <a href="https://unskript.com/keeping-your-cloud-costs-in-check-automated-aws-cost-charts-and-alerting/">previous post</a>, we looked at all AWS products for daily cost spend, and in this post, we dug deeper to better understand our EC2 spend – the largest percentage of our AWS bill.  Are you interested in trying out our alerting in unSkript?  Try out our Open Source Docker build (and give us a star!), or sign up for a <a href="https://us.app.unskript.io/">free cloud trial</a>.  Is there a segment of YOUR AWS bill that you’d like to investigate with us?  Reach out in our <a href="https://communityinviter.com/apps/cloud-ops-community/awesome-cloud-automation">Slack channel</a>, and we’d be happy to help you create a RunBook for your use case!</p>