Working with CloudWatch Alarms - AWS SDK for C++

Working with CloudWatch Alarms

Prerequisites

Before you begin, we recommend you read Getting started using the AWS SDK for C++.

Download the example code and build the solution as described in Get started on code examples.

To run the examples, the user profile your code uses to make the requests must have proper permissions in AWS (for the service and the action). For more information, see Providing AWS credentials.

Create an Alarm

To create an alarm based on a CloudWatch metric, call the CloudWatchClient’s PutMetricAlarm function with a PutMetricAlarmRequest filled with the alarm conditions.

Includes

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

Code

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; }

See the complete example.

List Alarms

To list the CloudWatch alarms that you have created, call the CloudWatchClient’s DescribeAlarms function with a DescribeAlarmsRequest that you can use to set options for the result.

Includes

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/DescribeAlarmsRequest.h> #include <aws/monitoring/model/DescribeAlarmsResult.h> #include <iomanip> #include <iostream>

Code

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::DescribeAlarmsRequest request; request.SetMaxRecords(1); bool done = false; bool header = false; while (!done) { auto outcome = cw.DescribeAlarms(request); if (!outcome.IsSuccess()) { std::cout << "Failed to describe CloudWatch alarms:" << outcome.GetError().GetMessage() << std::endl; break; } if (!header) { std::cout << std::left << std::setw(32) << "Name" << std::setw(64) << "Arn" << std::setw(64) << "Description" << std::setw(20) << "LastUpdated" << std::endl; header = true; } const auto &alarms = outcome.GetResult().GetMetricAlarms(); for (const auto &alarm : alarms) { std::cout << std::left << std::setw(32) << alarm.GetAlarmName() << std::setw(64) << alarm.GetAlarmArn() << std::setw(64) << alarm.GetAlarmDescription() << std::setw(20) << alarm.GetAlarmConfigurationUpdatedTimestamp().ToGmtString( SIMPLE_DATE_FORMAT_STR) << std::endl; } const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); }

The list of alarms can be obtained by calling getMetricAlarms on the DescribeAlarmsResult that is returned by DescribeAlarms.

The results may be paged. To retrieve the next batch of results, call SetNextToken on the original request object with the return value of the DescribeAlarmsResult object’s GetNextToken function, and pass the modified request object back to another call to DescribeAlarms.

Note

You can also retrieve alarms for a specific metric by using the CloudWatchClient’s DescribeAlarmsForMetric function. Its use is similar to DescribeAlarms.

See the complete example.

Delete Alarms

To delete CloudWatch alarms, call the CloudWatchClient’s DeleteAlarms function with a DeleteAlarmsRequest containing one or more names of alarms that you want to delete.

Includes

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

Code

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::DeleteAlarmsRequest request; request.AddAlarmNames(alarm_name); auto outcome = cw.DeleteAlarms(request); if (!outcome.IsSuccess()) { std::cout << "Failed to delete CloudWatch alarm:" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully deleted CloudWatch alarm " << alarm_name << std::endl; }

See the complete example.

More Information