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
}
[…] get billing details segregated by tag (Use Cost Allocation Tag). Refer my Amazon Resource Billing Blog to get more details on how to use […]