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

AWS ELB Billing Report

Posted on December 24, 2019February 27, 2022

I talked about AWS EC2 and general services billing in my previous blogs. AWS Services usage and billing reports already provides summary billing report for Elastic Load Balancing. However, if you want to generate a detailed per instance based report, we need to go one step further and generate that.

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 Elastic Load Balancing billing report using Powershell script.

This script assumes you have already setup your AWS credentials and AWS Powershell SDK to run this script on your system. 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 ” Amazon Elastic Load Balancing”.

$dimension = New-Object Amazon.CostExplorer.Model.DimensionValues
$dimension.Key = "SERVICE"
$dimension.Values ="Amazon Elastic Load Balancing"
$Filter = New-Object Amazon.CostExplorer.Model.Expression
$Filter.Dimensions = $dimension
$groupInfo = New-Object Amazon.CostExplorer.Model.GroupDefinition
$groupInfo.Type = "TAG"
$groupInfo.Key = "Name"

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 = $elbName = ""
	$sTime = $c.TimePeriod.Start
	ForEach($grp in $c.Groups){		
		$elbName = $grp.Keys.Split("$")[1]
		if([String]::IsNullOrEmpty($elbName)){$elbName = "No Name Tag"}
		$cost = $grp.Metrics["BlendedCost"].Amount
		$usageHours = $grp.Metrics["UsageQuantity"].Amount
		"$elbName,$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_elb_billing_usage_data.csv"
$tFile = "$($currentDir)\aws_elb_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 ="Amazon Elastic Load Balancing"
$Filter = New-Object Amazon.CostExplorer.Model.Expression
$Filter.Dimensions = $dimension
$groupInfo = New-Object Amazon.CostExplorer.Model.GroupDefinition
$groupInfo.Type = "TAG"
$groupInfo.Key = "Name"
$metric = @("BlendedCost","UsageQuantity")
####
$costUsage = Get-CECostAndUsage -TimePeriod $interval -Granularity DAILY -Metric $metric -Filter $Filter -GroupBy $groupInfo
ForEach($c in $costUsage.ResultsByTime){
	$sTime = $cost = $elbName = ""
	$sTime = $c.TimePeriod.Start
	ForEach($grp in $c.Groups){		
		$elbName = $grp.Keys.Split("$")[1]
		if([String]::IsNullOrEmpty($elbName)){$elbName = "No Name Tag"}
		$cost = $grp.Metrics["BlendedCost"].Amount
		$usageHours = $grp.Metrics["UsageQuantity"].Amount
		"$elbName,$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
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
Amazon Elastic Load Balancing 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
June 2025
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« 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