Creating an Amazon CloudWatch Alarm - AWS SDK for Ruby

Creating an Amazon CloudWatch Alarm

The following code example creates a new CloudWatch alarm (or updates an existing alarm, if an alarm with the specified name already exists).

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 require 'aws-sdk-cloudwatch' # Creates or updates an alarm in Amazon CloudWatch. # # @param cloudwatch_client [Aws::CloudWatch::Client] # An initialized CloudWatch client. # @param alarm_name [String] The name of the alarm. # @param alarm_description [String] A description about the alarm. # @param metric_name [String] The name of the metric associated with the alarm. # @param alarm_actions [Array] A list of Strings representing the # Amazon Resource Names (ARNs) to execute when the alarm transitions to the # ALARM state. # @param namespace [String] The namespace for the metric to alarm on. # @param statistic [String] The statistic for the metric. # @param dimensions [Array] A list of dimensions for the metric, specified as # Aws::CloudWatch::Types::Dimension. # @param period [Integer] The number of seconds before re-evaluating the metric. # @param unit [String] The unit of measure for the statistic. # @param evaluation_periods [Integer] The number of periods over which data is # compared to the specified threshold. # @param theshold [Float] The value against which the specified statistic is compared. # @param comparison_operator [String] The arithmetic operation to use when # comparing the specified statistic and threshold. # @return [Boolean] true if the alarm was created or updated; otherwise, false. # @example # exit 1 unless alarm_created_or_updated?( # Aws::CloudWatch::Client.new(region: 'us-east-1'), # 'ObjectsInBucket', # 'Objects exist in this bucket for more than 1 day.', # 'NumberOfObjects', # ['arn:aws:sns:us-east-1:111111111111:Default_CloudWatch_Alarms_Topic'], # 'AWS/S3', # 'Average', # [ # { # name: 'BucketName', # value: 'doc-example-bucket' # }, # { # name: 'StorageType', # value: 'AllStorageTypes' # } # ], # 86_400, # 'Count', # 1, # 1, # 'GreaterThanThreshold' # ) def alarm_created_or_updated?( cloudwatch_client, alarm_name, alarm_description, metric_name, alarm_actions, namespace, statistic, dimensions, period, unit, evaluation_periods, threshold, comparison_operator ) cloudwatch_client.put_metric_alarm( alarm_name: alarm_name, alarm_description: alarm_description, metric_name: metric_name, alarm_actions: alarm_actions, namespace: namespace, statistic: statistic, dimensions: dimensions, period: period, unit: unit, evaluation_periods: evaluation_periods, threshold: threshold, comparison_operator: comparison_operator ) return true rescue StandardError => e puts "Error creating alarm: #{e.message}" return false end # Full example call: def run_me alarm_name = 'ObjectsInBucket' alarm_description = 'Objects exist in this bucket for more than 1 day.' metric_name = 'NumberOfObjects' # Notify this Amazon Simple Notification Service (Amazon SNS) topic when # the alarm transitions to the ALARM state. alarm_actions = ['arn:aws:sns:us-east-1:111111111111:Default_CloudWatch_Alarms_Topic'] namespace = 'AWS/S3' statistic = 'Average' dimensions = [ { name: 'BucketName', value: 'doc-example-bucket' }, { name: 'StorageType', value: 'AllStorageTypes' } ] period = 86_400 # 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. region = 'us-east-1' cloudwatch_client = Aws::CloudWatch::Client.new(region: region) if alarm_created_or_updated?( cloudwatch_client, alarm_name, alarm_description, metric_name, alarm_actions, namespace, statistic, dimensions, period, unit, evaluation_periods, threshold, comparison_operator ) puts "Alarm '#{alarm_name}' created or updated." else puts "Could not create or update alarm '#{alarm_name}'." end end run_me if $PROGRAM_NAME == __FILE__