View a markdown version of this page

Use PutMetricAlarm with an Amazon SDK or CLI - Amazon CloudWatch
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Use PutMetricAlarm with an Amazon SDK or CLI

The following code examples show how to use PutMetricAlarm.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code examples:

.NET
Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create an alarm that evaluates a PromQL query against OpenTelemetry metrics.

/// <summary> /// Create an alarm that evaluates a PromQL query. /// /// A PromQL alarm differs from a classic metric alarm in a few ways. The query can /// match many series at once, and each matching series is tracked separately as a /// contributor. Instead of counting breaching periods, you specify durations: a /// contributor moves to ALARM after it breaches continuously for the pending period, /// and back to OK after it stops breaching for the recovery period. A PromQL alarm /// starts in the OK state rather than INSUFFICIENT_DATA. /// /// EvaluationCriteria is a union and is mutually exclusive with the classic /// MetricName and Metrics properties. When you use it you must also set /// EvaluationInterval, and you must not set Period, Statistic, Threshold, /// ComparisonOperator, EvaluationPeriods, DatapointsToAlarm, or TreatMissingData. /// </summary> /// <param name="alarmName">The name of the alarm, unique within the Region.</param> /// <param name="query">The PromQL query to evaluate, such as /// avg(cpu_utilization_percent) &gt; 80. The comparison belongs in the query itself; /// there is no separate threshold property.</param> /// <param name="evaluationInterval">How often, in seconds, to run the query. Valid /// values are 10, 20, 30, and any multiple of 60, up to 3600.</param> /// <param name="pendingPeriod">How long, in seconds, a contributor must breach /// continuously before it moves to ALARM.</param> /// <param name="recoveryPeriod">How long, in seconds, a contributor must stop /// breaching before it moves back to OK.</param> /// <returns>True if successful.</returns> public async Task<bool> PutPromQLMetricAlarm(string alarmName, string query, int evaluationInterval = 60, int pendingPeriod = 300, int recoveryPeriod = 120) { var response = await _amazonCloudWatch.PutMetricAlarmAsync( new PutMetricAlarmRequest { AlarmName = alarmName, AlarmDescription = "A PromQL alarm created by the AWS SDK for .NET example.", EvaluationCriteria = new EvaluationCriteria { PromQLCriteria = new AlarmPromQLCriteria { Query = query, PendingPeriod = pendingPeriod, RecoveryPeriod = recoveryPeriod } }, EvaluationInterval = evaluationInterval }); _logger.LogInformation($"Created PromQL alarm {alarmName} for query {query}."); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

Create an alarm that evaluates a single CloudWatch metric.

/// <summary> /// Add a metric alarm to send an email when the metric passes a threshold. /// </summary> /// <param name="alarmDescription">A description of the alarm.</param> /// <param name="alarmName">The name for the alarm.</param> /// <param name="comparison">The type of comparison to use.</param> /// <param name="metricName">The name of the metric for the alarm.</param> /// <param name="metricNamespace">The namespace of the metric.</param> /// <param name="threshold">The threshold value for the alarm.</param> /// <param name="alarmActions">Optional actions to execute when in an alarm state.</param> /// <returns>True if successful.</returns> public async Task<bool> PutMetricEmailAlarm(string alarmDescription, string alarmName, ComparisonOperator comparison, string metricName, string metricNamespace, double threshold, List<string> alarmActions = null!) { try { var putEmailAlarmResponse = await _amazonCloudWatch.PutMetricAlarmAsync( new PutMetricAlarmRequest() { AlarmActions = alarmActions, AlarmDescription = alarmDescription, AlarmName = alarmName, ComparisonOperator = comparison, Threshold = threshold, Namespace = metricNamespace, MetricName = metricName, EvaluationPeriods = 1, Period = 10, Statistic = new Statistic("Maximum"), DatapointsToAlarm = 1, TreatMissingData = "ignore" }); return putEmailAlarmResponse.HttpStatusCode == HttpStatusCode.OK; } catch (LimitExceededException lex) { _logger.LogError(lex, $"Unable to add alarm {alarmName}. Alarm quota has already been reached."); } return false; } /// <summary> /// Add specific email actions to a list of action strings for a CloudWatch alarm. /// </summary> /// <param name="accountId">The AccountId for the alarm.</param> /// <param name="region">The region for the alarm.</param> /// <param name="emailTopicName">An Amazon Simple Notification Service (SNS) topic for the alarm email.</param> /// <param name="alarmActions">Optional list of existing alarm actions to append to.</param> /// <returns>A list of string actions for an alarm.</returns> public List<string> AddEmailAlarmAction(string accountId, string region, string emailTopicName, List<string>? alarmActions = null) { alarmActions ??= new List<string>(); var snsAlarmAction = $"arn:aws:sns:{region}:{accountId}:{emailTopicName}"; alarmActions.Add(snsAlarmAction); return alarmActions; }
  • For API details, see PutMetricAlarm in Amazon SDK for .NET API Reference.

C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Include the required files for a PromQL alarm.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/AlarmPromQLCriteria.h> #include <aws/monitoring/model/EvaluationCriteria.h> #include <aws/monitoring/model/PutMetricAlarmRequest.h> #include <iostream>

Create an alarm that evaluates a PromQL query against OpenTelemetry metrics.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); Aws::CloudWatch::Model::AlarmPromQLCriteria promQLCriteria; promQLCriteria.SetQuery(query); // A contributor moves to ALARM after breaching continuously for 300 seconds, // and back to OK after 120 seconds without breaching. promQLCriteria.SetPendingPeriod(300); promQLCriteria.SetRecoveryPeriod(120); Aws::CloudWatch::Model::EvaluationCriteria evaluationCriteria; evaluationCriteria.SetPromQLCriteria(promQLCriteria); Aws::CloudWatch::Model::PutMetricAlarmRequest request; request.SetAlarmName(alarm_name); request.SetAlarmDescription("A PromQL alarm created by the AWS SDK for C++."); request.SetEvaluationCriteria(evaluationCriteria); // Valid values are 10, 20, 30, and any multiple of 60, up to 3600. request.SetEvaluationInterval(30); auto outcome = cw.PutMetricAlarm(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to create PromQL alarm: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully created PromQL alarm " << alarm_name << " for query " << query << std::endl; }

Include the required files for a metric alarm.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/PutMetricAlarmRequest.h> #include <iostream>

Create the alarm to watch the metric.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::PutMetricAlarmRequest request; request.SetAlarmName(alarm_name); request.SetComparisonOperator( Aws::CloudWatch::Model::ComparisonOperator::GreaterThanThreshold); request.SetEvaluationPeriods(1); request.SetMetricName("CPUUtilization"); request.SetNamespace("AWS/EC2"); request.SetPeriod(60); request.SetStatistic(Aws::CloudWatch::Model::Statistic::Average); request.SetThreshold(70.0); request.SetActionsEnabled(false); request.SetAlarmDescription("Alarm when server CPU exceeds 70%"); request.SetUnit(Aws::CloudWatch::Model::StandardUnit::Seconds); Aws::CloudWatch::Model::Dimension dimension; dimension.SetName("InstanceId"); dimension.SetValue(instanceId); request.AddDimensions(dimension); auto outcome = cw.PutMetricAlarm(request); if (!outcome.IsSuccess()) { std::cout << "Failed to create CloudWatch alarm:" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully created CloudWatch alarm " << alarm_name << std::endl; }
  • For API details, see PutMetricAlarm in Amazon SDK for C++ API Reference.

CLI
Amazon CLI

To send an Amazon Simple Notification Service email message when CPU utilization exceeds 70 percent

The following example uses the put-metric-alarm command to send an Amazon Simple Notification Service email message when CPU utilization exceeds 70 percent:

aws cloudwatch put-metric-alarm --alarm-name cpu-mon --alarm-description "Alarm when CPU exceeds 70 percent" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 300 --threshold 70 --comparison-operator GreaterThanThreshold --dimensions "Name=InstanceId,Value=i-12345678" --evaluation-periods 2 --alarm-actions arn:aws:sns:us-east-1:111122223333:MyTopic --unit Percent

This command returns to the prompt if successful. If an alarm with the same name already exists, it will be overwritten by the new alarm.

To specify multiple dimensions

The following example illustrates how to specify multiple dimensions. Each dimension is specified as a Name/Value pair, with a comma between the name and the value. Multiple dimensions are separated by a space:

aws cloudwatch put-metric-alarm --alarm-name "Default_Test_Alarm3" --alarm-description "The default example alarm" --namespace "CW EXAMPLE METRICS" --metric-name Default_Test --statistic Average --period 60 --evaluation-periods 3 --threshold 50 --comparison-operator GreaterThanOrEqualToThreshold --dimensions Name=key1,Value=value1 Name=key2,Value=value2
  • For API details, see PutMetricAlarm in Amazon CLI Command Reference.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create an alarm that evaluates a PromQL query against OpenTelemetry metrics.

/** * Creates an alarm that evaluates a PromQL query. * * <p>A PromQL alarm differs from a classic metric alarm in a few ways. The query * can match many series at once, and each matching series is tracked separately as * a contributor. Instead of counting breaching periods, you specify durations: a * contributor moves to ALARM after it breaches continuously for the pending period, * and back to OK after it stops breaching for the recovery period. A PromQL alarm * starts in the OK state rather than INSUFFICIENT_DATA. * * <p>{@link EvaluationCriteria} is a union and is mutually exclusive with the * classic {@code metricName} and {@code metrics} parameters. When you use it you * must also set {@code evaluationInterval}, and you must not set {@code period}, * {@code statistic}, {@code threshold}, {@code comparisonOperator}, * {@code evaluationPeriods}, {@code datapointsToAlarm}, or * {@code treatMissingData}. * * @param cw the CloudWatch client * @param alarmName the name of the alarm, unique within the Region * @param query the PromQL query to evaluate, such as * {@code avg(cpu_utilization_percent) > 80}. The * comparison belongs in the query itself; there is no * separate threshold parameter. * @param evaluationInterval how often, in seconds, to run the query. Valid values * are 10, 20, 30, and any multiple of 60, up to 3600. * @param pendingPeriod how long, in seconds, a contributor must breach * continuously before it moves to ALARM * @param recoveryPeriod how long, in seconds, a contributor must stop breaching * before it moves back to OK */ public static void putPromQLMetricAlarm(CloudWatchClient cw, String alarmName, String query, int evaluationInterval, int pendingPeriod, int recoveryPeriod) { try { AlarmPromQLCriteria promQLCriteria = AlarmPromQLCriteria.builder() .query(query) .pendingPeriod(pendingPeriod) .recoveryPeriod(recoveryPeriod) .build(); PutMetricAlarmRequest request = PutMetricAlarmRequest.builder() .alarmName(alarmName) .alarmDescription("PromQL alarm created by the AWS SDK for Java 2.x example.") .evaluationCriteria(EvaluationCriteria.builder() .promQLCriteria(promQLCriteria) .build()) .evaluationInterval(evaluationInterval) .build(); cw.putMetricAlarm(request); System.out.printf("Created PromQL alarm %s for query %s.%n", alarmName, query); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

Create an alarm that evaluates a single CloudWatch metric.

/** * Creates an alarm based on the configuration provided in a JSON file. * * @param fileName the name of the JSON file containing the alarm configuration * @return a CompletableFuture that represents the asynchronous operation of creating the alarm * @throws RuntimeException if an exception occurs while reading the JSON file or creating the alarm */ public CompletableFuture<String> createAlarmAsync(String fileName) { com.fasterxml.jackson.databind.JsonNode rootNode; try { JsonParser parser = new JsonFactory().createParser(new File(fileName)); rootNode = new ObjectMapper().readTree(parser); } catch (IOException e) { throw new RuntimeException("Failed to read the alarm configuration file", e); } // Extract values from the JSON node. String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText(); String customMetricName = rootNode.findValue("customMetricName").asText(); String alarmName = rootNode.findValue("exampleAlarmName").asText(); String emailTopic = rootNode.findValue("emailTopic").asText(); String accountId = rootNode.findValue("accountId").asText(); String region = rootNode.findValue("region").asText(); // Create a List for alarm actions. List<String> alarmActions = new ArrayList<>(); alarmActions.add("arn:aws:sns:" + region + ":" + accountId + ":" + emailTopic); PutMetricAlarmRequest alarmRequest = PutMetricAlarmRequest.builder() .alarmActions(alarmActions) .alarmDescription("Example metric alarm") .alarmName(alarmName) .comparisonOperator(ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD) .threshold(100.00) .metricName(customMetricName) .namespace(customMetricNamespace) .evaluationPeriods(1) .period(10) .statistic("Maximum") .datapointsToAlarm(1) .treatMissingData("ignore") .build(); // Call the putMetricAlarm asynchronously and handle the result. return getAsyncClient().putMetricAlarm(alarmRequest) .handle((response, ex) -> { if (ex != null) { logger.info("Failed to create alarm: {}", ex.getMessage()); throw new RuntimeException("Failed to create alarm", ex); } else { logger.info("{} was successfully created!", alarmName); return alarmName; } }); }
  • For API details, see PutMetricAlarm in Amazon SDK for Java 2.x API Reference.

JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create an alarm that evaluates a PromQL query against OpenTelemetry metrics.

import { PutMetricAlarmCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; // Create an alarm that evaluates a PromQL query over OpenTelemetry metrics. // // A PromQL alarm differs from a classic metric alarm in a few ways. The query can match // many series at once, and each matching series is tracked separately as a contributor // (see describe-alarm-contributors.js). Instead of counting breaching periods, you // specify durations: a contributor moves to ALARM after it breaches continuously for // PendingPeriod seconds, and back to OK after it stops breaching for RecoveryPeriod // seconds. A PromQL alarm starts in OK rather than INSUFFICIENT_DATA. // // EvaluationCriteria is a union and is mutually exclusive with the classic MetricName // and Metrics parameters. When you use it you must also set EvaluationInterval, and you // must not set Period, Statistic, Threshold, ComparisonOperator, EvaluationPeriods, // DatapointsToAlarm, or TreatMissingData. const run = async () => { const command = new PutMetricAlarmCommand({ AlarmName: process.env.CLOUDWATCH_ALARM_NAME, // Set CLOUDWATCH_ALARM_NAME to the name of the alarm to create. AlarmDescription: "Average CPU over 80% per host for the checkout service.", EvaluationCriteria: { PromQLCriteria: { // The comparison belongs in the query itself. There is no separate Threshold. Query: 'avg by (host_name) (cpu_utilization_percent{service_name="checkout"}) > 80', PendingPeriod: 300, RecoveryPeriod: 120, }, }, // How often to run the query, in seconds. Valid values are 10, 20, 30, and any // multiple of 60, up to 3600. EvaluationInterval: 30, ActionsEnabled: false, }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

Create an alarm that evaluates a single CloudWatch metric.

import { PutMetricAlarmCommand } from "@aws-sdk/client-cloudwatch"; import { client } from "../libs/client.js"; const run = async () => { // This alarm triggers when CPUUtilization exceeds 70% for one minute. const command = new PutMetricAlarmCommand({ AlarmName: process.env.CLOUDWATCH_ALARM_NAME, // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm. ComparisonOperator: "GreaterThanThreshold", EvaluationPeriods: 1, MetricName: "CPUUtilization", Namespace: "AWS/EC2", Period: 60, Statistic: "Average", Threshold: 70.0, ActionsEnabled: false, AlarmDescription: "Alarm when server CPU exceeds 70%", Dimensions: [ { Name: "InstanceId", Value: process.env.EC2_INSTANCE_ID, // Set the value of EC_INSTANCE_ID to the Id of an existing Amazon EC2 instance. }, ], Unit: "Percent", }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();

Create the client in a separate module and export it.

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; export const client = new CloudWatchClient({});
SDK for JavaScript (v2)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create CloudWatch service object var cw = new AWS.CloudWatch({ apiVersion: "2010-08-01" }); var params = { AlarmName: "Web_Server_CPU_Utilization", ComparisonOperator: "GreaterThanThreshold", EvaluationPeriods: 1, MetricName: "CPUUtilization", Namespace: "AWS/EC2", Period: 60, Statistic: "Average", Threshold: 70.0, ActionsEnabled: false, AlarmDescription: "Alarm when server CPU exceeds 70%", Dimensions: [ { Name: "InstanceId", Value: "INSTANCE_ID", }, ], Unit: "Percent", }; cw.putMetricAlarm(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create an alarm that evaluates a PromQL query against OpenTelemetry metrics.

suspend fun putPromQlMetricAlarm( alarmNameVal: String, queryVal: String, evaluationIntervalVal: Int = 60, pendingPeriodVal: Int = 300, recoveryPeriodVal: Int = 120, ) { // The comparison belongs in the query itself. A PromQL alarm has no separate // threshold, comparison operator, statistic, period, or evaluation periods. // // Note that the Kotlin SDK spells this AlarmPromQlCriteria, with a lowercase l in // "Ql". Every other AWS SDK spells it PromQL, so don't be thrown by the difference // when comparing this example against the other language versions. val promQlCriteria = AlarmPromQlCriteria { query = queryVal pendingPeriod = pendingPeriodVal recoveryPeriod = recoveryPeriodVal } // EvaluationCriteria is a union and is mutually exclusive with the classic // metricName and metrics parameters. When you use it, you must also set // evaluationInterval. val request = PutMetricAlarmRequest { alarmName = alarmNameVal alarmDescription = "A PromQL alarm created by the Kotlin SDK" evaluationCriteria = EvaluationCriteria.PromQlCriteria(promQlCriteria) evaluationInterval = evaluationIntervalVal } CloudWatchClient.fromEnvironment { region = "us-east-1" }.use { cwClient -> cwClient.putMetricAlarm(request) println("Successfully created PromQL alarm $alarmNameVal for query $queryVal") } }

Create an alarm that evaluates a single CloudWatch metric.

suspend fun putMetricAlarm( alarmNameVal: String, instanceIdVal: String, ) { val dimensionOb = Dimension { name = "InstanceId" value = instanceIdVal } val request = PutMetricAlarmRequest { alarmName = alarmNameVal comparisonOperator = ComparisonOperator.GreaterThanThreshold evaluationPeriods = 1 metricName = "CPUUtilization" namespace = "AWS/EC2" period = 60 statistic = Statistic.fromValue("Average") threshold = 70.0 actionsEnabled = false alarmDescription = "An Alarm created by the Kotlin SDK when server CPU utilization exceeds 70%" unit = StandardUnit.fromValue("Seconds") dimensions = listOf(dimensionOb) } CloudWatchClient.fromEnvironment { region = "us-east-1" }.use { cwClient -> cwClient.putMetricAlarm(request) println("Successfully created an alarm with name $alarmNameVal") } }
  • For API details, see PutMetricAlarm in Amazon SDK for Kotlin API reference.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create an alarm that evaluates a PromQL query against OpenTelemetry metrics.

class CloudWatchOTelWrapper: """Encapsulates the OpenTelemetry-oriented Amazon CloudWatch operations.""" def __init__(self, cloudwatch_client): """ :param cloudwatch_client: A Boto3 CloudWatch client. The OpenTelemetry operations are only available on the client interface, not on the higher-level ``boto3.resource("cloudwatch")`` interface. """ self.cloudwatch_client = cloudwatch_client @classmethod def from_client(cls): """ Creates a wrapper backed by a default CloudWatch client. :return: A CloudWatchOTelWrapper. """ return cls(boto3.client("cloudwatch")) def create_promql_alarm( self, alarm_name, query, evaluation_interval, pending_period=300, recovery_period=120, description=None, alarm_actions=None, ): """ Creates an alarm that evaluates a PromQL query. A PromQL alarm differs from a classic metric alarm in a few ways. The query can match many series at once, and each matching series is tracked separately as a *contributor*. Instead of counting breaching periods, you specify durations: a contributor moves to ALARM after it breaches continuously for the pending period, and back to OK after it stops breaching for the recovery period. A PromQL alarm starts in the OK state rather than INSUFFICIENT_DATA. The PromQL evaluation parameters live in the EvaluationCriteria union, which is mutually exclusive with the classic MetricName and Metrics parameters. When you use EvaluationCriteria you must also set EvaluationInterval, and you must not set Period, Statistic, Threshold, ComparisonOperator, EvaluationPeriods, DatapointsToAlarm, or TreatMissingData. :param alarm_name: The name of the alarm. Must be unique within the Region. :param query: The PromQL query to evaluate, such as 'avg(cpu_utilization_percent) > 80'. The comparison belongs in the query itself; there is no separate threshold parameter. :param evaluation_interval: How often, in seconds, to run the query. Valid values are 10, 20, 30, and any multiple of 60, up to 3600. :param pending_period: How long, in seconds, a contributor must breach continuously before it moves to ALARM. :param recovery_period: How long, in seconds, a contributor must stop breaching before it moves back to OK. :param description: The description of the alarm. :param alarm_actions: A list of ARNs to notify when the alarm fires, such as an Amazon SNS topic. """ promql_criteria = { "Query": query, "PendingPeriod": pending_period, "RecoveryPeriod": recovery_period, } kwargs = { "AlarmName": alarm_name, "EvaluationCriteria": {"PromQLCriteria": promql_criteria}, "EvaluationInterval": evaluation_interval, } if description is not None: kwargs["AlarmDescription"] = description if alarm_actions is not None: kwargs["AlarmActions"] = alarm_actions try: self.cloudwatch_client.put_metric_alarm(**kwargs) logger.info("Created PromQL alarm %s for query %s.", alarm_name, query) except ClientError: logger.exception("Couldn't create PromQL alarm %s.", alarm_name) raise

Create an alarm that evaluates a single CloudWatch metric.

class CloudWatchWrapper: """Encapsulates Amazon CloudWatch functions.""" def __init__(self, cloudwatch_resource): """ :param cloudwatch_resource: A Boto3 CloudWatch resource. """ self.cloudwatch_resource = cloudwatch_resource def create_metric_alarm( self, metric_namespace, metric_name, alarm_name, stat_type, period, eval_periods, threshold, comparison_op, ): """ Creates an alarm that watches a metric. :param metric_namespace: The namespace of the metric. :param metric_name: The name of the metric. :param alarm_name: The name of the alarm. :param stat_type: The type of statistic the alarm watches. :param period: The period in which metric data are grouped to calculate statistics. :param eval_periods: The number of periods that the metric must be over the alarm threshold before the alarm is set into an alarmed state. :param threshold: The threshold value to compare against the metric statistic. :param comparison_op: The comparison operation used to compare the threshold against the metric. :return: The newly created alarm. """ try: metric = self.cloudwatch_resource.Metric(metric_namespace, metric_name) alarm = metric.put_alarm( AlarmName=alarm_name, Statistic=stat_type, Period=period, EvaluationPeriods=eval_periods, Threshold=threshold, ComparisonOperator=comparison_op, ) logger.info( "Added alarm %s to track metric %s.%s.", alarm_name, metric_namespace, metric_name, ) except ClientError: logger.exception( "Couldn't add alarm %s to metric %s.%s", alarm_name, metric_namespace, metric_name, ) raise else: return alarm
  • For API details, see PutMetricAlarm in Amazon SDK for Python (Boto3) API Reference.

Ruby
SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create an alarm that evaluates a PromQL query against OpenTelemetry metrics.

# Creates or updates an alarm that evaluates a PromQL query. # # A PromQL alarm differs from a classic metric alarm in a few ways. The query can match # many series at once, and each matching series is tracked separately as a contributor. # Instead of counting breaching periods, you specify durations: a contributor moves to # ALARM after it breaches continuously for the pending period, and back to OK after it # stops breaching for the recovery period. A PromQL alarm starts in the OK state rather # than INSUFFICIENT_DATA. # # The +evaluation_criteria+ union is mutually exclusive with the classic +metric_name+ # and +metrics+ parameters. When you use it you must also set +evaluation_interval+, and # you must not set +period+, +statistic+, +threshold+, +comparison_operator+, # +evaluation_periods+, +datapoints_to_alarm+, or +treat_missing_data+. # # @param cloudwatch_client [Aws::CloudWatch::Client] An initialized CloudWatch client. # @param alarm_name [String] The name of the alarm, unique within the Region. # @param criteria [Hash] The PromQL criteria, mirroring the +prom_ql_criteria+ shape: # * +:query+ [String] The PromQL query to evaluate, such as # 'avg(cpu_utilization_percent) > 80'. The comparison belongs in the query itself; # there is no separate threshold parameter. # * +:pending_period+ [Integer] How long, in seconds, a contributor must breach # continuously before it moves to ALARM. # * +:recovery_period+ [Integer] How long, in seconds, a contributor must stop # breaching before it moves back to OK. # @param evaluation_interval [Integer] How often, in seconds, to run the query. Valid # values are 10, 20, 30, and any multiple of 60, up to 3600. # @param alarm_description [String] A description of the alarm. # @return [Boolean] true if the alarm was created or updated; otherwise, false. def promql_alarm_created_or_updated?( cloudwatch_client, alarm_name, criteria, evaluation_interval, alarm_description ) cloudwatch_client.put_metric_alarm( alarm_name: alarm_name, alarm_description: alarm_description, evaluation_criteria: { prom_ql_criteria: { query: criteria[:query], pending_period: criteria[:pending_period], recovery_period: criteria[:recovery_period] } }, evaluation_interval: evaluation_interval ) true rescue StandardError => e puts "Error creating PromQL alarm: #{e.message}" false end

Create an alarm that evaluates a single CloudWatch metric.

# 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: 'amzn-s3-demo-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 ) true rescue StandardError => e puts "Error creating alarm: #{e.message}" false end
  • For API details, see PutMetricAlarm in Amazon SDK for Ruby API Reference.

SAP ABAP
SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

TRY. lo_cwt->putmetricalarm( iv_alarmname = iv_alarm_name iv_comparisonoperator = iv_comparison_operator iv_evaluationperiods = iv_evaluation_periods iv_metricname = iv_metric_name iv_namespace = iv_namespace iv_statistic = iv_statistic iv_threshold = iv_threshold iv_actionsenabled = iv_actions_enabled iv_alarmdescription = iv_alarm_description iv_unit = iv_unit iv_period = iv_period it_dimensions = it_dimensions ). MESSAGE 'Alarm created.' TYPE 'I'. CATCH /aws1/cx_cwtlimitexceededfault. MESSAGE 'The request processing has exceeded the limit' TYPE 'E'. ENDTRY.
  • For API details, see PutMetricAlarm in Amazon SDK for SAP ABAP API reference.

For a complete list of Amazon SDK developer guides and code examples, see Using CloudWatch with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.