AWS SDK for Ruby
Developer Guide

Creating an Amazon CloudWatch Alarm

The following example creates a CloudWatch alarm named my-alarm that sends a message through the Amazon SNS topic with the ARN named ARN when the Amazon S3 bucket named my-bucket has more than 50 items in a 24-hour period.

require 'aws-sdk-cloudwatch' # v2: require 'aws-sdk' # Placeholder for put_metric_alarm args args = {} args[:alarm_name] = 'my-alarm' args[:alarm_description] = 'Triggers alarm when S3 bucket my-bucket has more than 50 items' args[:alarm_actions] = 'ARN' args[:namespace] = 'AWS/S3' args[:metric_name] = 'NumberOfObjects' dim1 = {} dim1[:name] = 'BucketName' dim1[:value] = 'my-bucket' dim2 = {} dim2[:name] = 'StorageType' dim2[:value] = 'AllStorageTypes' dimensions = [] dimensions << dim1 dimensions << dim2 args[:dimensions] = dimensions args[:statistic] = 'Maximum' # NumberOfObjects REQUIRES this value args[:period] = 86400 # NumberOfObjects REQUIRES this value args[:unit] = nil args[:evaluation_periods] = 1 args[:threshold] = 50 args[:comparison_operator] = 'GreaterThanThreshold' cw = Aws::CloudWatch::Client.new(region: 'us-west-2') cw.put_metric_alarm(args)

See the complete example on GitHub.