1

I am attempting to create a cloudwatch alarm based on a metric with multiple dimensions. I can create the alarm if I use ONE dimension; however the metric I am using has more than one.

The documentation shows this syntax:

aws cloudwatch put-metric-alarm --alarm-name "Default_Test_Alarm3" --alarm-description "The default example alarm" --namespace "CW EXAMPLE METRICS" --metric-name Default_Test --statistic Average --period 60 --evaluation-periods 3 --threshold 50 --comparison-operator GreaterThanOrEqualToThreshold --dimensions Name=key1,Value=value1 Name=key2,Value=value2

However this syntax only works with ONE dimension provided. My metric requires Three.

1 Answer 1

2

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
      }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.