Skip to content
Menu
Tech Automation Blog
  • About Author
  • Contact
Tech Automation Blog

AWS EBS Volumes Billing Report

Posted on December 24, 2019February 27, 2022

If you are familiar with AWS services Usage Billing reports you might have noticed there are two types of billing for EC2. Once for the EC2 Compute resources and it does not include Storage, EIP and some other resources attached with the EC2. AWS has categorized all those costs in to “EC2 – Other”. So, if you just want to generate a report on the cost incurred by EBS Volumes, there is no straight forward way for you.

Refer my previous blog here , if you want to generate EC2 usage billing report for your accounts.

In this blog, I will show you how to generate EBS volume billing report. For that you need to set up few things in your environment.

First, apply a Tag to represent EBS volume property. For this example, I am using tag named “EBSName” as Key and the actual EBS Volume name as the Value. I will share another script in a different blog on how to set Tags on a list of resources.

Once you have set Tags on all EBS volumes, you need to add this new Tag Key to AWS Cost allocation Tag list. If you have already set it , you are good! If not, Open Billing Console through link – https://console.aws.amazon.com/billing/home#/ .

Now, click on Cost Allocation tag option from the left side menu as shown in the screenshot .

Finally, from the list of available tag keys used in your subscription, you should see the Tag applied on EBS Volumes (Example, EBSName) as mentioned previously. Select the tag and click on Activate button under User-Defined Cost Allocation Tags. Once activated, it takes few hours before you can actually use it to generate Billing report using that tag.

Ok, now we are all set to start writing our Powershell script. This script assumes you have already setup your AWS credentials and AWS Powershell SDK to run this script on your system. If you face any issue setting those, please refer following documents to set those:

  • https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html
  • https://aws.amazon.com/powershell/

Let’s provide the time range for which this billing report will be generated. Make sure, the time range you are providing is with in the allowed range of AWS Billing API. I will be setting this script to either accept start and end date as parameters or generate it for the last month if nothing is provided. Then we will set CostExplorer Date interval using the start and end date as below

$interval = New-Object Amazon.CostExplorer.Model.DateInterval
$interval.Start = $firstDay
$interval.End = $lastDay

We now need to set Dimension, Filter and Grouping Definition as below. Note, Service set to “EC2 – Other”.

$dimension = New-Object Amazon.CostExplorer.Model.DimensionValues
$dimension.Key = "SERVICE"
$dimension.Values ="EC2 - Other"
$Filter = New-Object Amazon.CostExplorer.Model.Expression
$Filter.Dimensions = $dimension
$groupInfo = New-Object Amazon.CostExplorer.Model.GroupDefinition
$groupInfo.Type = "TAG"
$groupInfo.Key = "EBSName"

Define AWS Billing Metric as BlendedCost and UsageQuantity.

$metric = @("BlendedCost","UsageQuantity")

Finally, call the AWS CostExplorer API and parse through the result to generate CSV output with all required information.

$costUsage = Get-CECostAndUsage -TimePeriod $interval -Granularity DAILY -Metric $metric -Filter $Filter -GroupBy $groupInfo
ForEach($c in $costUsage.ResultsByTime){
	$sTime = $cost = $ebsName = ""
	$sTime = $c.TimePeriod.Start
	ForEach($grp in $c.Groups){		
		$ebsName = $grp.Keys.Split("$")[1]
		if([String]::IsNullOrEmpty($ebsName)){$ebsName = "No Name Tag"}
		$cost = $grp.Metrics["BlendedCost"].Amount
		$usageHours = $grp.Metrics["UsageQuantity"].Amount
		"$ebsName,$sTime,$usageHours,$cost" | Out-File $tFile -Append -Encoding ASCII
	}
}

Here is the complete script. Leave your comment in the comment section if you have any suggestion or question.

Param(
	[Parameter(Mandatory=$false)]
	[String]$firstDay,
	[Parameter(Mandatory=$false)]
	[String]$lastDay
)

#Print cost details for the Account for previous month
if([String]::IsNullOrEmpty($firstDay)){
	$firstDay = Get-Date -Year (Get-Date).Year -Month (Get-Date).AddMonths(-1).Month -Day 1 -UFormat "%Y-%m-%d"
}
if([String]::IsNullOrEmpty($lastDay)){
	$lastDay = Get-Date -Year (Get-Date).Year -Month (Get-Date).AddMonths(-1).Month -Day ([DateTime]::DaysInMonth((Get-Date).Year, (Get-Date).Month)) -UFormat "%Y-%m-%d"
}
$currentDir = $(Get-Location).Path
$oFile = "$($currentDir)\aws_ebs_billing_usage_data.csv"
$tFile = "$($currentDir)\aws_ebs_billing_usage_data_temp.csv"
if(Test-Path $oFile){
	Remove-Item $oFile -Force
}
if(Test-Path $tFile){
	Remove-Item $tFile -Force
}

Import-Module AWSPowershell

"Name,Date,UsageHours,Cost" | Out-File $tFile -Append -Encoding ASCII
$interval = New-Object Amazon.CostExplorer.Model.DateInterval
$interval.Start = $firstDay
$interval.End = $lastDay

$dimension = New-Object Amazon.CostExplorer.Model.DimensionValues
$dimension.Key = "SERVICE"
$dimension.Values ="EC2 - Other"
$Filter = New-Object Amazon.CostExplorer.Model.Expression
$Filter.Dimensions = $dimension
$groupInfo = New-Object Amazon.CostExplorer.Model.GroupDefinition
$groupInfo.Type = "TAG"
$groupInfo.Key = "EBSName"
$metric = @("BlendedCost","UsageQuantity")
####
$costUsage = Get-CECostAndUsage -TimePeriod $interval -Granularity DAILY -Metric $metric -Filter $Filter -GroupBy $groupInfo
ForEach($c in $costUsage.ResultsByTime){
	$sTime = $cost = $ebsName = ""
	$sTime = $c.TimePeriod.Start
	ForEach($grp in $c.Groups){		
		$ebsName = $grp.Keys.Split("$")[1]
		if([String]::IsNullOrEmpty($ebsName)){$ebsName = "No Name Tag"}
		$cost = $grp.Metrics["BlendedCost"].Amount
		$usageHours = $grp.Metrics["UsageQuantity"].Amount		"$ebsName,$sTime,$usageHours,$cost" | Out-File $tFile -Append -Encoding ASCII
	}
}

Import-Csv $tFile | Sort-Object 'Name','Date' | Export-Csv -Path $oFile -NoTypeInformation
if(Test-Path $tFile){
	Remove-Item $tFile -Force
}

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
Amazon Elastic IP Billing Report - Tech Automation Blog
5 years ago

[…] my previous blog I wrote about billing report for EBS Volumes under EC2 – Other service category of AWS Cost […]

0
trackback
Amazon EC2 Instance Tagging - Tech Automation Blog
5 years ago

[…] get billing details segregated by tag (Use Cost Allocation Tag). Refer my Amazon Resource Billing Blog to get more details on how to use […]

0
trackback
Amazon EBS Volumes Tagging - Tech Automation Blog
5 years ago

[…] get billing details segregated by tag (Use Cost Allocation Tag). Refer my Amazon Resource Billing Blog to get more details on how to use […]

0
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« May    

Recent Posts

  • Monitor and alert Azure Service Health issues May 5, 2020
  • AWS IAM User access review May 3, 2020
  • Integrate Azure Security Center with Event Hub April 28, 2020
  • Add Tags to Azure Subscription April 24, 2020
  • Automate Azure billing report in Excel March 6, 2020

Categories

©2025 Tech Automation Blog | Powered by SuperbThemes
wpDiscuz