0
votes

I am trying to create an alarm with Boto3 - CloudWatch - put_metric_alarm and having difficulties.

Here are the keyword arguments being supplied to the function:

{'ActionsEnabled': False,
 'AlarmDescription': 'Monitor for Idle instances',
 'AlarmName': 'IdleComponents',
 'ComparisonOperator': 'GreaterThanThreshold',
 'DatapointsToAlarm': 6,
 'EvaluationPeriods': 6,
 'Metrics': [{'Id': 'CPU',
              'MetricStat': {'Metric': {'Dimensions': [{'Name': 'InstanceId',
                                                        'Value': 'i-XX'}],
                                        'MetricName': 'CPUUtilization',
                                        'Namespace': 'AWS/EC2'},
                             'Period': 300,
                             'Stat': 'Average',
                             'Unit': 'Percent'},
              'ReturnData': False},
             {'Id': 'Net',
              'MetricStat': {'Metric': {'Dimensions': [{'Name': 'InstanceId',
                                                        'Value': 'i-XX'}],
                                        'MetricName': 'NetworkIn',
                                        'Namespace': 'AWS/EC2'},
                             'Period': 300,
                             'Stat': 'Average',
                             'Unit': 'Megabytes'},
              'ReturnData': False},
             {'Expression': 'IF(CPU < 5, 1, 0) AND IF(Net < 5, 1, 0)',
              'Id': 'e1',
              'ReturnData': True}],
 'Threshold': 0}

From visual inspection this appears to be a valid input, and the expression works as desired when creating it manually on the CloudWatch console, but when running this through Boto3 I get:

botocore.exceptions.ClientError: An error occurred (ValidationError) when calling the PutMetricAlarm operation: Invalid metrics list

The documentation here, claims that you may define an alarm such that you may use up to 10 metrics and 10 expressions, with one of the Metric queries returning data for the alarm.

I am not sure what I am doing wrong here. Thanks

1

1 Answers

3
votes

Id must start with a lower case letter, try changing CPU and Net to cpu and net.