AWS SDK for Ruby
Developer Guide

Enabling and Disabling Amazon CloudWatch Alarm Actions

An Amazon CloudWatch alarm watches a single metric over a time period you specify. The CloudWatch alarm performs one or more actions based on the value of the metric, relative to a given threshold over a number of time periods. For more information, see Creating Amazon CloudWatch Alarms in the Amazon CloudWatch User Guide.

In this example, you use the AWS SDK for Ruby with CloudWatch to:

  1. Enable an action for a CloudWatch alarm by using Aws::CloudWatch::Client#put_metric_alarm.

  2. Disable all actions for an alarm by using Aws::CloudWatch::Client#disable_alarm_actions.

The complete code for this example is available on GitHub.


Before running the example code, you need to install and configure the AWS SDK for Ruby, as described in:

You also need to replace arn:aws:sns:REGION-ID:ACCOUNT-ID:TOPIC-NAME with the ARN for a valid Amazon SNS topic.


require 'aws-sdk-cloudwatch' # v2: require 'aws-sdk' # Uncomment for Windows. # Aws.use_bundled_cert! cw = 'us-east-1') # Enable an action for an Amazon CloudWatch alarm. # If the alarm does not exist, create it. # If the alarm exists, update its settings. alarm_name = "TooManyObjectsInBucket" cw.put_metric_alarm({ alarm_name: alarm_name, alarm_description: "Alarm whenever an average of more than one object exists in the specified Amazon S3 bucket for more than one day.", actions_enabled: true, # Run actions if the alarm's state changes. metric_name: "NumberOfObjects", alarm_actions: [ "arn:aws:sns:REGION-ID:ACCOUNT-ID:TOPIC-NAME" ], # Notify this Amazon SNS topic only if the alarm's state changes to ALARM. namespace: "AWS/S3", statistic: "Average", dimensions: [ { name: "BucketName", value: "my-bucket" }, { name: "StorageType", value: "AllStorageTypes" } ], period: 86400, # Daily (24 hours * 60 minutes * 60 seconds = 86400 seconds). unit: "Count", evaluation_periods: 1, # More than one day. threshold: 1, # One object. comparison_operator: "GreaterThanThreshold" # More than one object. }) # Disable all actions for the alarm. cw.disable_alarm_actions({ alarm_names: [ alarm_name ] })

On this page: