Maybe this works in bash as documented, but I am using powershell and windows...
I tried using json as described in the docs, but get the error:
Error parsing parameter '--dimensions': Invalid JSON: Expecting
property name enclosed in double quotes
After an inordinate amount of time attempting to determine the correct syntax, this works:
--dimensions '[{\"Name\":\"instance\",\"Value\":\"Z:\"},{\"Name\":\"InstanceId\",\"Value\":\"i-xxxxxxxxx\"},{\"Name\":\"objectname\",\"Value\":\"LogicalDisk\"}]'
Apparantly when passing json, " needs to be passed as "
The following creates LogicalDisk % Free Space alarms for all the volumes in my environment:
$NAMESPACE = "CWAgent"
$METRIC_NAME = "LogicalDisk % Free Space"
$RESULT = aws cloudwatch list-metrics --namespace $NAMESPACE --metric-name $METRIC_NAME
$METRICS = $RESULT | ConvertFrom-Json
$ALARM_NAME = "LogicalDisk Free Space < 25% (i-07eef2fd07e336ddb)"
$ALARM_DESCRIPTION = "Triggers if Free Disk Space is below 25%"
foreach ($METRIC in $METRICS.Metrics)
{
$INSTANCE = $METRIC.Dimensions | Where-Object { ($_.Name -eq 'instance')}
if ($null -ne $INSTANCE ) {
$ALARM_DIMENSIONS = $METRIC.Dimensions | Where-Object { ($_.Name -eq 'instance') -or ($_.Name -eq 'InstanceId') -or ($_.Name -eq 'objectname')}
$json = $ALARM_DIMENSIONS | ConvertTo-Json -Compress
$json = $json.Replace("""","\""") #Need to fix Syntax w '\'
aws cloudwatch put-metric-alarm `
--alarm-name $ALARM_NAME `
--alarm-description $ALARM_DESCRIPTION `
--namespace $NAMESPACE `
--metric-name $METRIC_NAME `
--statistic "Average" `
--period 300 `
--evaluation-periods 1 `
--threshold 25 `
--comparison-operator "LessThanThreshold" `
--dimensions $json `
--no-actions-enabled
}
}