Use DescribeAlarms with an AWS SDK or CLI - Amazon CloudWatch

Use DescribeAlarms with an AWS SDK or CLI

The following code examples show how to use DescribeAlarms.

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
AWS SDK for .NET
Note

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

/// <summary> /// Describe the current alarms, optionally filtered by state. /// </summary> /// <param name="stateValue">Optional filter for alarm state.</param> /// <returns>The list of alarm data.</returns> public async Task<List<MetricAlarm>> DescribeAlarms(StateValue? stateValue = null) { List<MetricAlarm> alarms = new List<MetricAlarm>(); var paginatedDescribeAlarms = _amazonCloudWatch.Paginators.DescribeAlarms( new DescribeAlarmsRequest() { StateValue = stateValue }); await foreach (var data in paginatedDescribeAlarms.MetricAlarms) { alarms.Add(data); } return alarms; }
  • For API details, see DescribeAlarms in AWS SDK for .NET API Reference.

CLI
AWS CLI

To list information about an alarm

The following example uses the describe-alarms command to provide information about the alarm named "myalarm":

aws cloudwatch describe-alarms --alarm-names "myalarm"

Output:

{ "MetricAlarms": [ { "EvaluationPeriods": 2, "AlarmArn": "arn:aws:cloudwatch:us-east-1:123456789012:alarm:myalarm", "StateUpdatedTimestamp": "2014-04-09T18:59:06.442Z", "AlarmConfigurationUpdatedTimestamp": "2012-12-27T00:49:54.032Z", "ComparisonOperator": "GreaterThanThreshold", "AlarmActions": [ "arn:aws:sns:us-east-1:123456789012:myHighCpuAlarm" ], "Namespace": "AWS/EC2", "AlarmDescription": "CPU usage exceeds 70 percent", "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2014-04-09T18:59:06.419+0000\",\"startDate\":\"2014-04-09T18:44:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[38.958,40.292],\"threshold\":70.0}", "Period": 300, "StateValue": "OK", "Threshold": 70.0, "AlarmName": "myalarm", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0c986c72" } ], "Statistic": "Average", "StateReason": "Threshold Crossed: 2 datapoints were not greater than the threshold (70.0). The most recent datapoints: [38.958, 40.292].", "InsufficientDataActions": [], "OKActions": [], "ActionsEnabled": true, "MetricName": "CPUUtilization" } ] }
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 AWS Code Examples Repository.

/** * Describes the CloudWatch alarms of the 'METRIC_ALARM' type. * * @return a {@link CompletableFuture} that represents the asynchronous operation * of describing the CloudWatch alarms. The future completes when the * operation is finished, either successfully or with an error. */ public CompletableFuture<Void> describeAlarmsAsync() { List<AlarmType> typeList = new ArrayList<>(); typeList.add(AlarmType.METRIC_ALARM); DescribeAlarmsRequest alarmsRequest = DescribeAlarmsRequest.builder() .alarmTypes(typeList) .maxRecords(10) .build(); return getAsyncClient().describeAlarms(alarmsRequest) .thenAccept(response -> { List<MetricAlarm> alarmList = response.metricAlarms(); for (MetricAlarm alarm : alarmList) { logger.info("Alarm name: {}", alarm.alarmName()); logger.info("Alarm description: {} ", alarm.alarmDescription()); } }) .whenComplete((response, ex) -> { if (ex != null) { logger.info("Failed to describe alarms: {}", ex.getMessage()); } else { logger.info("Successfully described alarms."); } }); }
  • For API details, see DescribeAlarms in AWS SDK for Java 2.x API Reference.

Kotlin
SDK for Kotlin
Note

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

suspend fun describeAlarms() { val typeList = ArrayList<AlarmType>() typeList.add(AlarmType.MetricAlarm) val alarmsRequest = DescribeAlarmsRequest { alarmTypes = typeList maxRecords = 10 } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.describeAlarms(alarmsRequest) response.metricAlarms?.forEach { alarm -> println("Alarm name: ${alarm.alarmName}") println("Alarm description: ${alarm.alarmDescription}") } } }
  • For API details, see DescribeAlarms in AWS SDK for Kotlin 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 AWS Code Examples Repository.

require 'aws-sdk-cloudwatch' # Lists the names of available Amazon CloudWatch alarms. # # @param cloudwatch_client [Aws::CloudWatch::Client] # An initialized CloudWatch client. # @example # list_alarms(Aws::CloudWatch::Client.new(region: 'us-east-1')) def list_alarms(cloudwatch_client) response = cloudwatch_client.describe_alarms if response.metric_alarms.count.positive? response.metric_alarms.each do |alarm| puts alarm.alarm_name end else puts 'No alarms found.' end rescue StandardError => e puts "Error getting information about alarms: #{e.message}" end
  • For API details, see DescribeAlarms in AWS 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 AWS Code Examples Repository.

TRY. oo_result = lo_cwt->describealarms( " oo_result is returned for testing purposes. " it_alarmnames = it_alarm_names ). MESSAGE 'Alarms retrieved.' TYPE 'I'. CATCH /aws1/cx_rt_service_generic INTO DATA(lo_exception). DATA(lv_error) = |"{ lo_exception->av_err_code }" - { lo_exception->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDTRY.
  • For API details, see DescribeAlarms in AWS SDK for SAP ABAP API reference.

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