Get started with CloudWatch alarms using an AWS SDK - Amazon CloudWatch

Get started with CloudWatch alarms using an AWS SDK

The following code example shows how to:

  • Create an alarm.

  • Disable alarm actions.

  • Describe an alarm.

  • Delete an alarm.


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

DATA lt_alarmnames TYPE /aws1/cl_cwtalarmnames_w=>tt_alarmnames. DATA lo_alarmname TYPE REF TO /aws1/cl_cwtalarmnames_w. "Create an alarm" 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. "Create an ABAP internal table for the created alarm." CREATE OBJECT lo_alarmname EXPORTING iv_value = iv_alarm_name. INSERT lo_alarmname INTO TABLE lt_alarmnames. "Disable alarm actions." TRY. lo_cwt->disablealarmactions( it_alarmnames = lt_alarmnames ). MESSAGE 'Alarm actions disabled' TYPE 'I'. CATCH /aws1/cx_rt_service_generic INTO DATA(lo_disablealarm_exception). DATA(lv_disablealarm_error) = |"{ lo_disablealarm_exception->av_err_code }" - { lo_disablealarm_exception->av_err_msg }|. MESSAGE lv_disablealarm_error TYPE 'E'. ENDTRY. "Describe alarm using the same ABAP internal table." TRY. oo_result = lo_cwt->describealarms( " oo_result is returned for testing purpose " it_alarmnames = lt_alarmnames ). MESSAGE 'Alarms retrieved' TYPE 'I'. CATCH /aws1/cx_rt_service_generic INTO DATA(lo_describealarms_exception). DATA(lv_describealarms_error) = |"{ lo_describealarms_exception->av_err_code }" - { lo_describealarms_exception->av_err_msg }|. MESSAGE lv_describealarms_error TYPE 'E'. ENDTRY. "Delete alarm." TRY. lo_cwt->deletealarms( it_alarmnames = lt_alarmnames ). MESSAGE 'Alarms deleted' TYPE 'I'. CATCH /aws1/cx_cwtresourcenotfound . MESSAGE 'Resource being access is not found.' TYPE 'E'. ENDTRY.

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.