Skip to content

Instantly share code, notes, and snippets.

@jimbo8098
Created January 10, 2024 18:16
Show Gist options
  • Save jimbo8098/bb49f0cef79e0cd0dbc09741b138f8af to your computer and use it in GitHub Desktop.
Save jimbo8098/bb49f0cef79e0cd0dbc09741b138f8af to your computer and use it in GitHub Desktop.
Retrieve CloudWatch Metrics
$payload = @{
Id = "test"
MetricStat = @{
Metric = @{
Namespace = "EC2Extended"
MetricName = "RootFreeSpace"
Dimensions = [System.Array] @(
@{
Name = "path"
Value = "/"
}
@{
Name = "InstanceId"
Value = "i-1234"
}
@{
Name = "device"
Value = "nvme0n1p1"
}
@{
Name = "fstype"
Value = "xfs"
}
)
}
Period = 5
Stat = "Average"
Unit = "Bytes"
}
}
$metrics = $(
aws cloudwatch list-metrics `
--namespace $payload.MetricStat.Metric.Namespace `
--metric-name $payload.MetricStat.Metric.MetricName `
--dimensions $($payload.MetricStat.Metric.Dimensions | ConvertTo-Json ) `
--output json | ConvertFrom-Json)
$payloadJson = $($payload | ConvertTo-Json -Depth 5)
$payload.MetricStat.Metric.Dimensions | ConvertTo-Json -Compress
$startTime = (Get-Date).Addhours(-4).ToString("yyyy-MM-ddTHH:mm:ssZ")
$endTime = Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ"
echo "${startTime} -> ${endTime}"
aws cloudwatch get-metric-statistics `
--metric-name RootFreeSpace `
--start-time $startTime `
--end-time $endTime `
--period 3600 `
--namespace EC2Extended `
--statistics Average `
--dimensions $($payload.MetricStat.Metric.Dimensions | ConvertTo-Json -Compress) `
--region eu-west-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment