简体   繁体   中英

create Disk-Read-Throughput-IOPS cloudwatch alarm with boto3

i want to create Disk-Read-Throughput-IOPS cloudwatch alarm with boto3 with math Expression but i have error the error "errorMessage": "An error occurred (ValidationError) when calling the PutMetricAlarm operation: The parameters MetricDataQuery Expression and MetricStat are mutually exclusive and you have specified both."

the code

from __future__ import print_function
from string import Template
import json
import boto3

def lambda_handler(event, context):
    CW_client = boto3.client('cloudwatch', region_name='eu-west-1')

   volume_id = 'vol-01903a31c2c4d5690'
   response7 = CW_client.put_metric_alarm(
    AlarmName='Disk-Read-Throughput-IOPS',
    AlarmDescription='Disk-Read-Throughput-IOPS',
    ActionsEnabled=True,
    AlarmActions=[
        'topic',
    ],
    MetricName='VolumeReadOps',
    Namespace='AWS/EBS',
    Statistic='Sum',
    Dimensions=[
        {
            'Name': 'VolumeId',
            'Value': 'volume_id'
        },
    ],
    Period=300,
    EvaluationPeriods=3,
    DatapointsToAlarm=3,
    Threshold=600.0,
    ComparisonOperator='GreaterThanThreshold',
    Metrics=[
        {
            'Id': 'm1',
            'MetricStat': {
                'Metric': {
                    'Namespace': 'AWS/EBS',
                    'MetricName': 'VolumeReadOps',
                    'Dimensions': [
                        {
                            'Name': 'VolumeId',
                            'Value': 'volume_id'
                        },
                    ]
                },
                'Period': 300,
                'Stat': 'Sum',
            },
            'Expression': 'SUM(METRICS())/300',
            'Label': 'Expression1',
            'Period': 300
        },
    ],
 )
  1. You can't have MetricStat and Expression in the same Metric object, you need to split those out.
  2. Then if you have multiple Metric objects, exactly 1 can return data, the rest should have 'ReturnData': False , which means data will be used in the expression but it won't result in a separate line on a graph (you only need 1 line, the one generated by the expression).
  3. If you specify the Metric list, you can't have the metric defined with the Namespace , MetricName and Dimension on the top level, so you need to remove those.

This should work (as far as metrics go, not sure about the action part):

from __future__ import print_function
from string import Template
import json
import boto3

def lambda_handler(event, context):
    CW_client = boto3.client('cloudwatch', region_name='eu-west-1')

    volume_id = 'vol-01903a31c2c4d5690'
    response7 = CW_client.put_metric_alarm(
        AlarmName='Disk-Read-Throughput-IOPS',
        AlarmDescription='Disk-Read-Throughput-IOPS',
        ActionsEnabled=True,
        AlarmActions=[
            'topic',
        ],
        EvaluationPeriods=3,
        DatapointsToAlarm=3,
        Threshold=600.0,
        ComparisonOperator='GreaterThanThreshold',
        Metrics=[
            {
                'Id': 'm1',
                'MetricStat': {
                    'Metric': {
                        'Namespace': 'AWS/EBS',
                        'MetricName': 'VolumeReadOps',
                        'Dimensions': [
                            {
                                'Name': 'VolumeId',
                                'Value': 'volume_id'
                            },
                        ]
                    },
                    'Period': 300,
                    'Stat': 'Sum'
                },
                'Label': 'Metric1',
                'ReturnData': False
            },
            {
                'Id': 'm2',
                'Expression': 'SUM(METRICS())/300',
                'Label': 'Expression1'
            },
        ],
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM