CloudWatch examples using SDK for C++ - AWS SDK Code Examples

View a markdown version of this page

CloudWatch examples using SDK for C++ - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

CloudWatch examples using SDK for C++

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for C++ with CloudWatch.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use DeleteAlarmMuteRule.

SDK for C++
Note

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

Include the required files.

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

Delete the alarm mute rule.

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::DeleteAlarmMuteRuleRequest request; request.SetAlarmMuteRuleName(mute_rule_name); auto outcome = cw.DeleteAlarmMuteRule(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to delete alarm mute rule: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully deleted alarm mute rule " << mute_rule_name << std::endl; }

The following code example shows how to use DeleteAlarms.

SDK for C++
Note

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

Include the required files.

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

Delete the alarm.

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; }
  • For API details, see DeleteAlarms in AWS SDK for C++ API Reference.

The following code example shows how to use DescribeAlarmContributors.

SDK for C++
Note

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

Include the required files.

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

Describe the contributors to a PromQL alarm.

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::DescribeAlarmContributorsRequest request; request.SetAlarmName(alarm_name); // Collect every page before reporting. A page can come back empty while still // carrying a next token, so the loop must keep going until the token is empty // rather than stopping at the first empty page. Aws::Vector<Aws::CloudWatch::Model::AlarmContributor> contributors; bool failed = false; bool done = false; while (!done) { auto outcome = cw.DescribeAlarmContributors(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to describe alarm contributors: " << outcome.GetError().GetMessage() << std::endl; failed = true; break; } const auto &page = outcome.GetResult().GetAlarmContributors(); contributors.insert(contributors.end(), page.begin(), page.end()); const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); } if (!failed) { if (contributors.empty()) { std::cout << "No contributors yet. The query matched no series, " "which usually means no OTel metrics with these labels " "have arrived." << std::endl; } else { std::cout << "Contributors for alarm " << alarm_name << ":" << std::endl; for (const auto &contributor : contributors) { std::cout << " " << contributor.GetContributorId() << ": "; bool first = true; for (const auto &label : contributor.GetContributorAttributes()) { if (!first) { std::cout << ", "; } std::cout << label.first << "=" << label.second; first = false; } std::cout << std::endl; std::cout << " reason: " << contributor.GetStateReason() << std::endl; } } }

The following code example shows how to use DescribeAlarmsForMetric.

SDK for C++
Note

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

Include the required files.

#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>

Describe the alarms.

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 following code example shows how to use DisableAlarmActions.

SDK for C++
Note

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

Include the required files.

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

Disable the alarm actions.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::DisableAlarmActionsRequest disableAlarmActionsRequest; disableAlarmActionsRequest.AddAlarmNames(alarm_name); auto disableAlarmActionsOutcome = cw.DisableAlarmActions(disableAlarmActionsRequest); if (!disableAlarmActionsOutcome.IsSuccess()) { std::cout << "Failed to disable actions for alarm " << alarm_name << ": " << disableAlarmActionsOutcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully disabled actions for alarm " << alarm_name << std::endl; }

The following code example shows how to use EnableAlarmActions.

SDK for C++
Note

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

Include the required files.

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

Enable the alarm actions.

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); request.AddAlarmActions(actionArn); 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; return; } Aws::CloudWatch::Model::EnableAlarmActionsRequest enable_request; enable_request.AddAlarmNames(alarm_name); auto enable_outcome = cw.EnableAlarmActions(enable_request); if (!enable_outcome.IsSuccess()) { std::cout << "Failed to enable alarm actions:" << enable_outcome.GetError().GetMessage() << std::endl; return; } std::cout << "Successfully created alarm " << alarm_name << " and enabled actions on it." << std::endl;

The following code example shows how to use GetAlarmMuteRule.

SDK for C++
Note

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

Include the required files.

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

Get the alarm mute rule.

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::GetAlarmMuteRuleRequest request; request.SetAlarmMuteRuleName(mute_rule_name); auto outcome = cw.GetAlarmMuteRule(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to get alarm mute rule: " << outcome.GetError().GetMessage() << std::endl; } else { const auto &result = outcome.GetResult(); std::cout << "Mute rule " << result.GetName() << " is " << Aws::CloudWatch::Model::AlarmMuteRuleStatusMapper:: GetNameForAlarmMuteRuleStatus(result.GetStatus()) << "." << std::endl; std::cout << " ARN: " << result.GetAlarmMuteRuleArn() << std::endl; std::cout << " schedule: " << result.GetRule().GetSchedule().GetExpression() << " for " << result.GetRule().GetSchedule().GetDuration() << std::endl; const auto &alarm_names = result.GetMuteTargets().GetAlarmNames(); if (!alarm_names.empty()) { std::cout << " muted alarms:"; for (const auto &alarm_name : alarm_names) { std::cout << " " << alarm_name; } std::cout << std::endl; } }

The following code example shows how to use GetOTelEnrichment.

SDK for C++
Note

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

Include the required files.

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

Get the OpenTelemetry enrichment status.

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::GetOTelEnrichmentRequest request; auto outcome = cw.GetOTelEnrichment(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to get OTel enrichment status: " << outcome.GetError().GetMessage() << std::endl; } else { auto status = outcome.GetResult().GetStatus(); std::cout << "OTel enrichment status is " << Aws::CloudWatch::Model::OTelEnrichmentStatusMapper:: GetNameForOTelEnrichmentStatus(status) << "." << std::endl; if (status == Aws::CloudWatch::Model::OTelEnrichmentStatus::Running) { std::cout << "Vended metrics are queryable with PromQL." << std::endl; } else { std::cout << "Start enrichment to enrich vended metrics with resource " "ARN and tag labels." << std::endl; } }

The following code example shows how to use ListAlarmMuteRules.

SDK for C++
Note

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

Include the required files.

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

List the alarm mute rules.

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::ListAlarmMuteRulesRequest request; if (argc == 2) { request.SetAlarmName(argv[1]); } bool done = false; while (!done) { auto outcome = cw.ListAlarmMuteRules(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to list alarm mute rules: " << outcome.GetError().GetMessage() << std::endl; break; } for (const auto &summary : outcome.GetResult().GetAlarmMuteRuleSummaries()) { std::cout << summary.GetAlarmMuteRuleArn() << " (" << Aws::CloudWatch::Model::AlarmMuteRuleStatusMapper:: GetNameForAlarmMuteRuleStatus(summary.GetStatus()) << ")" << std::endl; } const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); }

The following code example shows how to use ListMetrics.

SDK for C++
Note

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

Include the required files.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/ListMetricsRequest.h> #include <aws/monitoring/model/ListMetricsResult.h> #include <iomanip> #include <iostream>

List the metrics.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::ListMetricsRequest request; if (argc > 1) { request.SetMetricName(argv[1]); } if (argc > 2) { request.SetNamespace(argv[2]); } bool done = false; bool header = false; while (!done) { auto outcome = cw.ListMetrics(request); if (!outcome.IsSuccess()) { std::cout << "Failed to list CloudWatch metrics:" << outcome.GetError().GetMessage() << std::endl; break; } if (!header) { std::cout << std::left << std::setw(48) << "MetricName" << std::setw(32) << "Namespace" << "DimensionNameValuePairs" << std::endl; header = true; } const auto &metrics = outcome.GetResult().GetMetrics(); for (const auto &metric : metrics) { std::cout << std::left << std::setw(48) << metric.GetMetricName() << std::setw(32) << metric.GetNamespace(); const auto &dimensions = metric.GetDimensions(); for (auto iter = dimensions.cbegin(); iter != dimensions.cend(); ++iter) { const auto &dimkv = *iter; std::cout << dimkv.GetName() << " = " << dimkv.GetValue(); if (iter + 1 != dimensions.cend()) { std::cout << ", "; } } std::cout << std::endl; } const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); }
  • For API details, see ListMetrics in AWS SDK for C++ API Reference.

The following code example shows how to use PutAlarmMuteRule.

SDK for C++
Note

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

Include the required files.

#include <aws/core/Aws.h> #include <aws/monitoring/CloudWatchClient.h> #include <aws/monitoring/model/MuteTargets.h> #include <aws/monitoring/model/PutAlarmMuteRuleRequest.h> #include <aws/monitoring/model/Rule.h> #include <aws/monitoring/model/Schedule.h> #include <iostream>

Create the alarm mute rule.

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::CloudWatch::CloudWatchClient cw(clientConfig); // For a recurring window, use a five-field cron expression, // cron(Minutes Hours Day-of-month Month Day-of-week). Note that this is five // fields, not the six that Amazon EventBridge uses. For a one-time window, use // an at expression such as at(2026-09-05T02:00). Aws::CloudWatch::Model::Schedule schedule; schedule.SetExpression("cron(0 2 * * SUN)"); // The duration is in ISO 8601 duration format, from PT1M (one minute) to // P15D (15 days). schedule.SetDuration("PT2H"); schedule.SetTimezone("America/Los_Angeles"); Aws::CloudWatch::Model::Rule rule; rule.SetSchedule(schedule); // Target up to 100 alarms. If MuteTargets is not set, the rule applies to every // alarm in the account. Aws::CloudWatch::Model::MuteTargets muteTargets; muteTargets.AddAlarmNames(alarm_name); Aws::CloudWatch::Model::PutAlarmMuteRuleRequest request; request.SetName(mute_rule_name); request.SetDescription("A mute rule created by the AWS SDK for C++."); request.SetRule(rule); request.SetMuteTargets(muteTargets); auto outcome = cw.PutAlarmMuteRule(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to put alarm mute rule: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully put alarm mute rule " << mute_rule_name << std::endl; }

The following code example shows how to use PutMetricAlarm.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS 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 AWS SDK for C++ API Reference.

The following code example shows how to use PutMetricData.

SDK for C++
Note

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

Include the required files.

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

Put data into the metric.

Aws::CloudWatch::CloudWatchClient cw; Aws::CloudWatch::Model::Dimension dimension; dimension.SetName("UNIQUE_PAGES"); dimension.SetValue("URLS"); Aws::CloudWatch::Model::MetricDatum datum; datum.SetMetricName("PAGES_VISITED"); datum.SetUnit(Aws::CloudWatch::Model::StandardUnit::None); datum.SetValue(data_point); datum.AddDimensions(dimension); Aws::CloudWatch::Model::PutMetricDataRequest request; request.SetNamespace("SITE/TRAFFIC"); request.AddMetricData(datum); auto outcome = cw.PutMetricData(request); if (!outcome.IsSuccess()) { std::cout << "Failed to put sample metric data:" << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully put sample metric data" << std::endl; }
  • For API details, see PutMetricData in AWS SDK for C++ API Reference.

The following code example shows how to use StartOTelEnrichment.

SDK for C++
Note

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

Include the required files.

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

Start OpenTelemetry enrichment.

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::StartOTelEnrichmentRequest request; auto outcome = cw.StartOTelEnrichment(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to start OTel enrichment: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully started OTel enrichment for this account." << std::endl; }

The following code example shows how to use StopOTelEnrichment.

SDK for C++
Note

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

Include the required files.

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

Stop OpenTelemetry enrichment.

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::StopOTelEnrichmentRequest request; auto outcome = cw.StopOTelEnrichment(request); if (!outcome.IsSuccess()) { std::cerr << "Failed to stop OTel enrichment: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully stopped OTel enrichment for this account." << std::endl; }