Skip to content

Instantly share code, notes, and snippets.

@mskutin
Last active November 12, 2020 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mskutin/0045131f4dd4f4e4f44c2b283ad2cf3f to your computer and use it in GitHub Desktop.
Save mskutin/0045131f4dd4f4e4f44c2b283ad2cf3f to your computer and use it in GitHub Desktop.
Posh script to generate an API Gateway weekly usage report in plain HTML. Ensure you have AWS Tools installed https://aws.amazon.com/powershell/
Import-Module -Name AWSPowerShell
Set-DefaultAWSRegion 'ap-southeast-1'
$numberOfDays = 7
$fileName = ('Gateways-' + (get-date -f yyyy-MM-dd-HH-mm-s) + '.html')
$reportLocation = Join-Path (Get-Location).path $fileName
$withMetrics = @()
$Header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
"@
$gateways = Get-AGRestApiList| Select id, Name, Description, CreatedDate, Version
function get-ApiUsageMetrics {
Param(
[Parameter(Mandatory = $true)]
[String]$ApiName
)
$startDate=[DateTime]::Today.AddDays(-$numberOfDays).ToString("u")
$endDate=[DateTime]::Today.ToString("u")
$res = Get-CWMetricStatistics `
-Metricname "Count" `
-Namespace "AWS/ApiGateway" `
-UtcStartTime $startDate `
-UtcEndTime $endDate `
-Period (60*60*24*$numberOfDays) `
-Statistics "Sum" `
-Dimension @{Name = "ApiName"; Value = $ApiName}
$res.Datapoints | select Sum
}
foreach ($gw in $gateways) {
$counter = get-ApiUsageMetrics -ApiName $gw.Name
$api = New-Object -TypeName PSObject -Property @{
Name = $gw.Name
Description = $gw.Description
CreatedDate = $gw.CreatedDate
Version = $gw.Version
WeeklyRequests = $counter.Sum
}
$withMetrics+=$api
}
$fileName = ('Gateways-' + (get-date -f yyyy-MM-dd-HH-mm-s) + '.html')
$reportLocation = Join-Path (Get-Location).path $fileName
$withMetrics | Sort-Object 'WeeklyRequests' -Descending| ConvertTo-html -Head $Header | Out-File $reportLocation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment