Get started with CloudWatch metrics, dashboards, and alarms using an Amazon SDK - 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).

Get started with CloudWatch metrics, dashboards, and alarms using an Amazon SDK

The following code examples show how to:

  • List CloudWatch namespaces and metrics.

  • Get statistics for a metric and for estimated billing.

  • Create and update a dashboard.

  • Create and add data to a metric.

  • Create and trigger an alarm, then view alarm history.

  • Add an anomaly detector.

  • Get a metric image, then clean up resources.

.NET
Amazon SDK for .NET
Note

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

Run an interactive scenario at a command prompt.

public class CloudWatchScenario { /* Before running this .NET code example, set up your development environment, including your credentials. To enable billing metrics and statistics for this example, make sure billing alerts are enabled for your account: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/monitor_estimated_charges_with_cloudwatch.html#turning_on_billing_metrics This .NET example performs the following tasks: 1. List and select a CloudWatch namespace. 2. List and select a CloudWatch metric. 3. Get statistics for a CloudWatch metric. 4. Get estimated billing statistics for the last week. 5. Create a new CloudWatch dashboard with two metrics. 6. List current CloudWatch dashboards. 7. Create a CloudWatch custom metric and add metric data. 8. Add the custom metric to the dashboard. 9. Create a CloudWatch alarm for the custom metric. 10. Describe current CloudWatch alarms. 11. Get recent data for the custom metric. 12. Add data to the custom metric to trigger the alarm. 13. Wait for an alarm state. 14. Get history for the CloudWatch alarm. 15. Add an anomaly detector. 16. Describe current anomaly detectors. 17. Get and display a metric image. 18. Clean up resources. */ private static ILogger logger = null!; private static CloudWatchWrapper _cloudWatchWrapper = null!; private static IConfiguration _configuration = null!; private static readonly List<string> _statTypes = new List<string> { "SampleCount", "Average", "Sum", "Minimum", "Maximum" }; private static SingleMetricAnomalyDetector? anomalyDetector = null!; static async Task Main(string[] args) { // Set up dependency injection for the Amazon service. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonCloudWatch>() .AddTransient<CloudWatchWrapper>() ) .Build(); _configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally, load local settings. .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<CloudWatchScenario>(); _cloudWatchWrapper = host.Services.GetRequiredService<CloudWatchWrapper>(); Console.WriteLine(new string('-', 80)); Console.WriteLine("Welcome to the Amazon CloudWatch example scenario."); Console.WriteLine(new string('-', 80)); try { var selectedNamespace = await SelectNamespace(); var selectedMetric = await SelectMetric(selectedNamespace); await GetAndDisplayMetricStatistics(selectedNamespace, selectedMetric); await GetAndDisplayEstimatedBilling(); await CreateDashboardWithMetrics(); await ListDashboards(); await CreateNewCustomMetric(); await AddMetricToDashboard(); await CreateMetricAlarm(); await DescribeAlarms(); await GetCustomMetricData(); await AddMetricDataForAlarm(); await CheckForMetricAlarm(); await GetAlarmHistory(); anomalyDetector = await AddAnomalyDetector(); await DescribeAnomalyDetectors(); await GetAndOpenMetricImage(); await CleanupResources(); } catch (Exception ex) { logger.LogError(ex, "There was a problem executing the scenario."); await CleanupResources(); } } /// <summary> /// Select a namespace. /// </summary> /// <returns>The selected namespace.</returns> private static async Task<string> SelectNamespace() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"1. Select a CloudWatch Namespace from a list of Namespaces."); var metrics = await _cloudWatchWrapper.ListMetrics(); // Get a distinct list of namespaces. var namespaces = metrics.Select(m => m.Namespace).Distinct().ToList(); for (int i = 0; i < namespaces.Count; i++) { Console.WriteLine($"\t{i + 1}. {namespaces[i]}"); } var namespaceChoiceNumber = 0; while (namespaceChoiceNumber < 1 || namespaceChoiceNumber > namespaces.Count) { Console.WriteLine( "Select a namespace by entering a number from the preceding list:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out namespaceChoiceNumber); } var selectedNamespace = namespaces[namespaceChoiceNumber - 1]; Console.WriteLine(new string('-', 80)); return selectedNamespace; } /// <summary> /// Select a metric from a namespace. /// </summary> /// <param name="metricNamespace">The namespace for metrics.</param> /// <returns>The metric name.</returns> private static async Task<Metric> SelectMetric(string metricNamespace) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"2. Select a CloudWatch metric from a namespace."); var namespaceMetrics = await _cloudWatchWrapper.ListMetrics(metricNamespace); for (int i = 0; i < namespaceMetrics.Count && i < 15; i++) { var dimensionsWithValues = namespaceMetrics[i].Dimensions .Where(d => !string.Equals("None", d.Value)); Console.WriteLine($"\t{i + 1}. {namespaceMetrics[i].MetricName} " + $"{string.Join(", :", dimensionsWithValues.Select(d => d.Value))}"); } var metricChoiceNumber = 0; while (metricChoiceNumber < 1 || metricChoiceNumber > namespaceMetrics.Count) { Console.WriteLine( "Select a metric by entering a number from the preceding list:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out metricChoiceNumber); } var selectedMetric = namespaceMetrics[metricChoiceNumber - 1]; Console.WriteLine(new string('-', 80)); return selectedMetric; } /// <summary> /// Get and display metric statistics for a specific metric. /// </summary> /// <param name="metricNamespace">The namespace for metrics.</param> /// <param name="metric">The CloudWatch metric.</param> /// <returns>Async task.</returns> private static async Task GetAndDisplayMetricStatistics(string metricNamespace, Metric metric) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"3. Get CloudWatch metric statistics for the last day."); for (int i = 0; i < _statTypes.Count; i++) { Console.WriteLine($"\t{i + 1}. {_statTypes[i]}"); } var statisticChoiceNumber = 0; while (statisticChoiceNumber < 1 || statisticChoiceNumber > _statTypes.Count) { Console.WriteLine( "Select a metric statistic by entering a number from the preceding list:"); var choice = Console.ReadLine(); Int32.TryParse(choice, out statisticChoiceNumber); } var selectedStatistic = _statTypes[statisticChoiceNumber - 1]; var statisticsList = new List<string> { selectedStatistic }; var metricStatistics = await _cloudWatchWrapper.GetMetricStatistics(metricNamespace, metric.MetricName, statisticsList, metric.Dimensions, 1, 60); if (!metricStatistics.Any()) { Console.WriteLine($"No {selectedStatistic} statistics found for {metric} in namespace {metricNamespace}."); } metricStatistics = metricStatistics.OrderBy(s => s.Timestamp).ToList(); for (int i = 0; i < metricStatistics.Count && i < 10; i++) { var metricStat = metricStatistics[i]; var statValue = metricStat.GetType().GetProperty(selectedStatistic)!.GetValue(metricStat, null); Console.WriteLine($"\t{i + 1}. Timestamp {metricStatistics[i].Timestamp:G} {selectedStatistic}: {statValue}"); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Get and display estimated billing statistics. /// </summary> /// <param name="metricNamespace">The namespace for metrics.</param> /// <param name="metric">The CloudWatch metric.</param> /// <returns>Async task.</returns> private static async Task GetAndDisplayEstimatedBilling() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"4. Get CloudWatch estimated billing for the last week."); var billingStatistics = await SetupBillingStatistics(); for (int i = 0; i < billingStatistics.Count; i++) { Console.WriteLine($"\t{i + 1}. Timestamp {billingStatistics[i].Timestamp:G} : {billingStatistics[i].Maximum}"); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Get billing statistics using a call to a wrapper class. /// </summary> /// <returns>A collection of billing statistics.</returns> private static async Task<List<Datapoint>> SetupBillingStatistics() { // Make a request for EstimatedCharges with a period of one day for the past seven days. var billingStatistics = await _cloudWatchWrapper.GetMetricStatistics( "AWS/Billing", "EstimatedCharges", new List<string>() { "Maximum" }, new List<Dimension>() { new Dimension { Name = "Currency", Value = "USD" } }, 7, 86400); billingStatistics = billingStatistics.OrderBy(n => n.Timestamp).ToList(); return billingStatistics; } /// <summary> /// Create a dashboard with metrics. /// </summary> /// <param name="metricNamespace">The namespace for metrics.</param> /// <param name="metric">The CloudWatch metric.</param> /// <returns>Async task.</returns> private static async Task CreateDashboardWithMetrics() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"5. Create a new CloudWatch dashboard with metrics."); var dashboardName = _configuration["dashboardName"]; var newDashboard = new DashboardModel(); _configuration.GetSection("dashboardExampleBody").Bind(newDashboard); var newDashboardString = JsonSerializer.Serialize( newDashboard, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); var validationMessages = await _cloudWatchWrapper.PutDashboard(dashboardName, newDashboardString); Console.WriteLine(validationMessages.Any() ? $"\tValidation messages:" : null); for (int i = 0; i < validationMessages.Count; i++) { Console.WriteLine($"\t{i + 1}. {validationMessages[i].Message}"); } Console.WriteLine($"\tDashboard {dashboardName} was created."); Console.WriteLine(new string('-', 80)); } /// <summary> /// List dashboards. /// </summary> /// <returns>Async task.</returns> private static async Task ListDashboards() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"6. List the CloudWatch dashboards in the current account."); var dashboards = await _cloudWatchWrapper.ListDashboards(); for (int i = 0; i < dashboards.Count; i++) { Console.WriteLine($"\t{i + 1}. {dashboards[i].DashboardName}"); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Create and add data for a new custom metric. /// </summary> /// <returns>Async task.</returns> private static async Task CreateNewCustomMetric() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"7. Create and add data for a new custom metric."); var customMetricNamespace = _configuration["customMetricNamespace"]; var customMetricName = _configuration["customMetricName"]; var customData = await PutRandomMetricData(customMetricName, customMetricNamespace); var valuesString = string.Join(',', customData.Select(d => d.Value)); Console.WriteLine($"\tAdded metric values for for metric {customMetricName}: \n\t{valuesString}"); Console.WriteLine(new string('-', 80)); } /// <summary> /// Add some metric data using a call to a wrapper class. /// </summary> /// <param name="customMetricName">The metric name.</param> /// <param name="customMetricNamespace">The metric namespace.</param> /// <returns></returns> private static async Task<List<MetricDatum>> PutRandomMetricData(string customMetricName, string customMetricNamespace) { List<MetricDatum> customData = new List<MetricDatum>(); Random rnd = new Random(); // Add 10 random values up to 100, starting with a timestamp 15 minutes in the past. var utcNowMinus15 = DateTime.UtcNow.AddMinutes(-15); for (int i = 0; i < 10; i++) { var metricValue = rnd.Next(0, 100); customData.Add( new MetricDatum { MetricName = customMetricName, Value = metricValue, TimestampUtc = utcNowMinus15.AddMinutes(i) } ); } await _cloudWatchWrapper.PutMetricData(customMetricNamespace, customData); return customData; } /// <summary> /// Add the custom metric to the dashboard. /// </summary> /// <returns>Async task.</returns> private static async Task AddMetricToDashboard() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"8. Add the new custom metric to the dashboard."); var dashboardName = _configuration["dashboardName"]; var customMetricNamespace = _configuration["customMetricNamespace"]; var customMetricName = _configuration["customMetricName"]; var validationMessages = await SetupDashboard(customMetricNamespace, customMetricName, dashboardName); Console.WriteLine(validationMessages.Any() ? $"\tValidation messages:" : null); for (int i = 0; i < validationMessages.Count; i++) { Console.WriteLine($"\t{i + 1}. {validationMessages[i].Message}"); } Console.WriteLine($"\tDashboard {dashboardName} updated with metric {customMetricName}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Set up a dashboard using a call to the wrapper class. /// </summary> /// <param name="customMetricNamespace">The metric namespace.</param> /// <param name="customMetricName">The metric name.</param> /// <param name="dashboardName">The name of the dashboard.</param> /// <returns>A list of validation messages.</returns> private static async Task<List<DashboardValidationMessage>> SetupDashboard( string customMetricNamespace, string customMetricName, string dashboardName) { // Get the dashboard model from configuration. var newDashboard = new DashboardModel(); _configuration.GetSection("dashboardExampleBody").Bind(newDashboard); // Add a new metric to the dashboard. newDashboard.Widgets.Add(new Widget { Height = 8, Width = 8, Y = 8, X = 0, Type = "metric", Properties = new Properties { Metrics = new List<List<object>> { new() { customMetricNamespace, customMetricName } }, View = "timeSeries", Region = "us-east-1", Stat = "Sum", Period = 86400, YAxis = new YAxis { Left = new Left { Min = 0, Max = 100 } }, Title = "Custom Metric Widget", LiveData = true, Sparkline = true, Trend = true, Stacked = false, SetPeriodToTimeRange = false } }); var newDashboardString = JsonSerializer.Serialize(newDashboard, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); var validationMessages = await _cloudWatchWrapper.PutDashboard(dashboardName, newDashboardString); return validationMessages; } /// <summary> /// Create a CloudWatch alarm for the new metric. /// </summary> /// <returns>Async task.</returns> private static async Task CreateMetricAlarm() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"9. Create a CloudWatch alarm for the new metric."); var customMetricNamespace = _configuration["customMetricNamespace"]; var customMetricName = _configuration["customMetricName"]; var alarmName = _configuration["exampleAlarmName"]; var accountId = _configuration["accountId"]; var region = _configuration["region"]; var emailTopic = _configuration["emailTopic"]; var alarmActions = new List<string>(); if (GetYesNoResponse( $"\tAdd an email action for topic {emailTopic} to alarm {alarmName}? (y/n)")) { _cloudWatchWrapper.AddEmailAlarmAction(accountId, region, emailTopic, alarmActions); } await _cloudWatchWrapper.PutMetricEmailAlarm( "Example metric alarm", alarmName, ComparisonOperator.GreaterThanOrEqualToThreshold, customMetricName, customMetricNamespace, 100, alarmActions); Console.WriteLine($"\tAlarm {alarmName} added for metric {customMetricName}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Describe Alarms. /// </summary> /// <returns>Async task.</returns> private static async Task DescribeAlarms() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"10. Describe CloudWatch alarms in the current account."); var alarms = await _cloudWatchWrapper.DescribeAlarms(); alarms = alarms.OrderByDescending(a => a.StateUpdatedTimestamp).ToList(); for (int i = 0; i < alarms.Count && i < 10; i++) { var alarm = alarms[i]; Console.WriteLine($"\t{i + 1}. {alarm.AlarmName}"); Console.WriteLine($"\tState: {alarm.StateValue} for {alarm.MetricName} {alarm.ComparisonOperator} {alarm.Threshold}"); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Get the recent data for the metric. /// </summary> /// <returns>Async task.</returns> private static async Task GetCustomMetricData() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"11. Get current data for new custom metric."); var customMetricNamespace = _configuration["customMetricNamespace"]; var customMetricName = _configuration["customMetricName"]; var accountId = _configuration["accountId"]; var query = new List<MetricDataQuery> { new MetricDataQuery { AccountId = accountId, Id = "m1", Label = "Custom Metric Data", MetricStat = new MetricStat { Metric = new Metric { MetricName = customMetricName, Namespace = customMetricNamespace, }, Period = 1, Stat = "Maximum" } } }; var metricData = await _cloudWatchWrapper.GetMetricData( 20, true, DateTime.UtcNow.AddMinutes(1), 20, query); for (int i = 0; i < metricData.Count; i++) { for (int j = 0; j < metricData[i].Values.Count; j++) { Console.WriteLine( $"\tTimestamp {metricData[i].Timestamps[j]:G} Value: {metricData[i].Values[j]}"); } } Console.WriteLine(new string('-', 80)); } /// <summary> /// Add metric data to trigger an alarm. /// </summary> /// <returns>Async task.</returns> private static async Task AddMetricDataForAlarm() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"12. Add metric data to the custom metric to trigger an alarm."); var customMetricNamespace = _configuration["customMetricNamespace"]; var customMetricName = _configuration["customMetricName"]; var nowUtc = DateTime.UtcNow; List<MetricDatum> customData = new List<MetricDatum> { new MetricDatum { MetricName = customMetricName, Value = 101, TimestampUtc = nowUtc.AddMinutes(-2) }, new MetricDatum { MetricName = customMetricName, Value = 101, TimestampUtc = nowUtc.AddMinutes(-1) }, new MetricDatum { MetricName = customMetricName, Value = 101, TimestampUtc = nowUtc } }; var valuesString = string.Join(',', customData.Select(d => d.Value)); Console.WriteLine($"\tAdded metric values for for metric {customMetricName}: \n\t{valuesString}"); await _cloudWatchWrapper.PutMetricData(customMetricNamespace, customData); Console.WriteLine(new string('-', 80)); } /// <summary> /// Check for a metric alarm using the DescribeAlarmsForMetric action. /// </summary> /// <returns>Async task.</returns> private static async Task CheckForMetricAlarm() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"13. Checking for an alarm state."); var customMetricNamespace = _configuration["customMetricNamespace"]; var customMetricName = _configuration["customMetricName"]; var hasAlarm = false; var retries = 10; while (!hasAlarm && retries > 0) { var alarms = await _cloudWatchWrapper.DescribeAlarmsForMetric(customMetricNamespace, customMetricName); hasAlarm = alarms.Any(a => a.StateValue == StateValue.ALARM); retries--; Thread.Sleep(20000); } Console.WriteLine(hasAlarm ? $"\tAlarm state found for {customMetricName}." : $"\tNo Alarm state found for {customMetricName} after 10 retries."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Get history for an alarm. /// </summary> /// <returns>Async task.</returns> private static async Task GetAlarmHistory() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"14. Get alarm history."); var exampleAlarmName = _configuration["exampleAlarmName"]; var alarmHistory = await _cloudWatchWrapper.DescribeAlarmHistory(exampleAlarmName, 2); for (int i = 0; i < alarmHistory.Count; i++) { var history = alarmHistory[i]; Console.WriteLine($"\t{i + 1}. {history.HistorySummary}, time {history.Timestamp:g}"); } if (!alarmHistory.Any()) { Console.WriteLine($"\tNo alarm history data found for {exampleAlarmName}."); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Add an anomaly detector. /// </summary> /// <returns>Async task.</returns> private static async Task<SingleMetricAnomalyDetector> AddAnomalyDetector() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"15. Add an anomaly detector."); var customMetricNamespace = _configuration["customMetricNamespace"]; var customMetricName = _configuration["customMetricName"]; var detector = new SingleMetricAnomalyDetector { MetricName = customMetricName, Namespace = customMetricNamespace, Stat = "Maximum" }; await _cloudWatchWrapper.PutAnomalyDetector(detector); Console.WriteLine($"\tAdded anomaly detector for metric {customMetricName}."); Console.WriteLine(new string('-', 80)); return detector; } /// <summary> /// Describe anomaly detectors. /// </summary> /// <returns>Async task.</returns> private static async Task DescribeAnomalyDetectors() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"16. Describe anomaly detectors in the current account."); var customMetricNamespace = _configuration["customMetricNamespace"]; var customMetricName = _configuration["customMetricName"]; var detectors = await _cloudWatchWrapper.DescribeAnomalyDetectors(customMetricNamespace, customMetricName); for (int i = 0; i < detectors.Count; i++) { var detector = detectors[i]; Console.WriteLine($"\t{i + 1}. {detector.SingleMetricAnomalyDetector.MetricName}, state {detector.StateValue}"); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Fetch and open a metrics image for a CloudWatch metric and namespace. /// </summary> /// <returns>Async task.</returns> private static async Task GetAndOpenMetricImage() { Console.WriteLine(new string('-', 80)); Console.WriteLine("17. Get a metric image from CloudWatch."); Console.WriteLine($"\tGetting Image data for custom metric."); var customMetricNamespace = _configuration["customMetricNamespace"]; var customMetricName = _configuration["customMetricName"]; var memoryStream = await _cloudWatchWrapper.GetTimeSeriesMetricImage(customMetricNamespace, customMetricName, "Maximum", 10); var file = _cloudWatchWrapper.SaveMetricImage(memoryStream, "MetricImages"); ProcessStartInfo info = new ProcessStartInfo(); Console.WriteLine($"\tFile saved as {Path.GetFileName(file)}."); Console.WriteLine($"\tPress enter to open the image."); Console.ReadLine(); info.FileName = Path.Combine("ms-photos://", file); info.UseShellExecute = true; info.CreateNoWindow = true; info.Verb = string.Empty; Process.Start(info); Console.WriteLine(new string('-', 80)); } /// <summary> /// Clean up created resources. /// </summary> /// <param name="metricNamespace">The namespace for metrics.</param> /// <param name="metric">The CloudWatch metric.</param> /// <returns>Async task.</returns> private static async Task CleanupResources() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"18. Clean up resources."); var dashboardName = _configuration["dashboardName"]; if (GetYesNoResponse($"\tDelete dashboard {dashboardName}? (y/n)")) { Console.WriteLine($"\tDeleting dashboard."); var dashboardList = new List<string> { dashboardName }; await _cloudWatchWrapper.DeleteDashboards(dashboardList); } var alarmName = _configuration["exampleAlarmName"]; if (GetYesNoResponse($"\tDelete alarm {alarmName}? (y/n)")) { Console.WriteLine($"\tCleaning up alarms."); var alarms = new List<string> { alarmName }; await _cloudWatchWrapper.DeleteAlarms(alarms); } if (GetYesNoResponse($"\tDelete anomaly detector? (y/n)") && anomalyDetector != null) { Console.WriteLine($"\tCleaning up anomaly detector."); await _cloudWatchWrapper.DeleteAnomalyDetector( anomalyDetector); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Get a yes or no response from the user. /// </summary> /// <param name="question">The question string to print on the console.</param> /// <returns>True if the user responds with a yes.</returns> private static bool GetYesNoResponse(string question) { Console.WriteLine(question); var ynResponse = Console.ReadLine(); var response = ynResponse != null && ynResponse.Equals("y", StringComparison.InvariantCultureIgnoreCase); return response; } }

Wrapper methods used by the scenario for CloudWatch actions.

/// <summary> /// Wrapper class for Amazon CloudWatch methods. /// </summary> public class CloudWatchWrapper { private readonly IAmazonCloudWatch _amazonCloudWatch; private readonly ILogger<CloudWatchWrapper> _logger; /// <summary> /// Constructor for the CloudWatch wrapper. /// </summary> /// <param name="amazonCloudWatch">The injected CloudWatch client.</param> /// <param name="logger">The injected logger for the wrapper.</param> public CloudWatchWrapper(IAmazonCloudWatch amazonCloudWatch, ILogger<CloudWatchWrapper> logger) { _logger = logger; _amazonCloudWatch = amazonCloudWatch; } /// <summary> /// List metrics available, optionally within a namespace. /// </summary> /// <param name="metricNamespace">Optional CloudWatch namespace to use when listing metrics.</param> /// <param name="filter">Optional dimension filter.</param> /// <param name="metricName">Optional metric name filter.</param> /// <returns>The list of metrics.</returns> public async Task<List<Metric>> ListMetrics(string? metricNamespace = null, DimensionFilter? filter = null, string? metricName = null) { var results = new List<Metric>(); var paginateMetrics = _amazonCloudWatch.Paginators.ListMetrics( new ListMetricsRequest { Namespace = metricNamespace, Dimensions = filter != null ? new List<DimensionFilter> { filter } : null, MetricName = metricName }); // Get the entire list using the paginator. await foreach (var metric in paginateMetrics.Metrics) { results.Add(metric); } return results; } /// <summary> /// Wrapper to get statistics for a specific CloudWatch metric. /// </summary> /// <param name="metricNamespace">The namespace of the metric.</param> /// <param name="metricName">The name of the metric.</param> /// <param name="statistics">The list of statistics to include.</param> /// <param name="dimensions">The list of dimensions to include.</param> /// <param name="days">The number of days in the past to include.</param> /// <param name="period">The period for the data.</param> /// <returns>A list of DataPoint objects for the statistics.</returns> public async Task<List<Datapoint>> GetMetricStatistics(string metricNamespace, string metricName, List<string> statistics, List<Dimension> dimensions, int days, int period) { var metricStatistics = await _amazonCloudWatch.GetMetricStatisticsAsync( new GetMetricStatisticsRequest() { Namespace = metricNamespace, MetricName = metricName, Dimensions = dimensions, Statistics = statistics, StartTimeUtc = DateTime.UtcNow.AddDays(-days), EndTimeUtc = DateTime.UtcNow, Period = period }); return metricStatistics.Datapoints; } /// <summary> /// Wrapper to create or add to a dashboard with metrics. /// </summary> /// <param name="dashboardName">The name for the dashboard.</param> /// <param name="dashboardBody">The metric data in JSON for the dashboard.</param> /// <returns>A list of validation messages for the dashboard.</returns> public async Task<List<DashboardValidationMessage>> PutDashboard(string dashboardName, string dashboardBody) { // Updating a dashboard replaces all contents. // Best practice is to include a text widget indicating this dashboard was created programmatically. var dashboardResponse = await _amazonCloudWatch.PutDashboardAsync( new PutDashboardRequest() { DashboardName = dashboardName, DashboardBody = dashboardBody }); return dashboardResponse.DashboardValidationMessages; } /// <summary> /// Get information on a dashboard. /// </summary> /// <param name="dashboardName">The name of the dashboard.</param> /// <returns>A JSON object with dashboard information.</returns> public async Task<string> GetDashboard(string dashboardName) { var dashboardResponse = await _amazonCloudWatch.GetDashboardAsync( new GetDashboardRequest() { DashboardName = dashboardName }); return dashboardResponse.DashboardBody; } /// <summary> /// Get a list of dashboards. /// </summary> /// <returns>A list of DashboardEntry objects.</returns> public async Task<List<DashboardEntry>> ListDashboards() { var results = new List<DashboardEntry>(); var paginateDashboards = _amazonCloudWatch.Paginators.ListDashboards( new ListDashboardsRequest()); // Get the entire list using the paginator. await foreach (var data in paginateDashboards.DashboardEntries) { results.Add(data); } return results; } /// <summary> /// Wrapper to add metric data to a CloudWatch metric. /// </summary> /// <param name="metricNamespace">The namespace of the metric.</param> /// <param name="metricData">A data object for the metric data.</param> /// <returns>True if successful.</returns> public async Task<bool> PutMetricData(string metricNamespace, List<MetricDatum> metricData) { var putDataResponse = await _amazonCloudWatch.PutMetricDataAsync( new PutMetricDataRequest() { MetricData = metricData, Namespace = metricNamespace, }); return putDataResponse.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Get an image for a metric graphed over time. /// </summary> /// <param name="metricNamespace">The namespace of the metric.</param> /// <param name="metric">The name of the metric.</param> /// <param name="stat">The name of the stat to chart.</param> /// <param name="period">The period to use for the chart.</param> /// <returns>A memory stream for the chart image.</returns> public async Task<MemoryStream> GetTimeSeriesMetricImage(string metricNamespace, string metric, string stat, int period) { var metricImageWidget = new { title = "Example Metric Graph", view = "timeSeries", stacked = false, period = period, width = 1400, height = 600, metrics = new List<List<object>> { new() { metricNamespace, metric, new { stat } } } }; var metricImageWidgetString = JsonSerializer.Serialize(metricImageWidget); var imageResponse = await _amazonCloudWatch.GetMetricWidgetImageAsync( new GetMetricWidgetImageRequest() { MetricWidget = metricImageWidgetString }); return imageResponse.MetricWidgetImage; } /// <summary> /// Save a metric image to a file. /// </summary> /// <param name="memoryStream">The MemoryStream for the metric image.</param> /// <param name="metricName">The name of the metric.</param> /// <returns>The path to the file.</returns> public string SaveMetricImage(MemoryStream memoryStream, string metricName) { var metricFileName = $"{metricName}_{DateTime.Now.Ticks}.png"; using var sr = new StreamReader(memoryStream); // Writes the memory stream to a file. File.WriteAllBytes(metricFileName, memoryStream.ToArray()); var filePath = Path.Join(AppDomain.CurrentDomain.BaseDirectory, metricFileName); return filePath; } /// <summary> /// Get data for CloudWatch metrics. /// </summary> /// <param name="minutesOfData">The number of minutes of data to include.</param> /// <param name="useDescendingTime">True to return the data descending by time.</param> /// <param name="endDateUtc">The end date for the data, in UTC.</param> /// <param name="maxDataPoints">The maximum data points to include.</param> /// <param name="dataQueries">Optional data queries to include.</param> /// <returns>A list of the requested metric data.</returns> public async Task<List<MetricDataResult>> GetMetricData(int minutesOfData, bool useDescendingTime, DateTime? endDateUtc = null, int maxDataPoints = 0, List<MetricDataQuery>? dataQueries = null) { var metricData = new List<MetricDataResult>(); // If no end time is provided, use the current time for the end time. endDateUtc ??= DateTime.UtcNow; var timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(endDateUtc.Value.ToLocalTime()); var startTimeUtc = endDateUtc.Value.AddMinutes(-minutesOfData); // The timezone string should be in the format +0000, so use the timezone offset to format it correctly. var timeZoneString = $"{timeZoneOffset.Hours:D2}{timeZoneOffset.Minutes:D2}"; var paginatedMetricData = _amazonCloudWatch.Paginators.GetMetricData( new GetMetricDataRequest() { StartTimeUtc = startTimeUtc, EndTimeUtc = endDateUtc.Value, LabelOptions = new LabelOptions { Timezone = timeZoneString }, ScanBy = useDescendingTime ? ScanBy.TimestampDescending : ScanBy.TimestampAscending, MaxDatapoints = maxDataPoints, MetricDataQueries = dataQueries, }); await foreach (var data in paginatedMetricData.MetricDataResults) { metricData.Add(data); } return metricData; } /// <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; } /// <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; } /// <summary> /// Describe the current alarms for a specific metric. /// </summary> /// <param name="metricNamespace">The namespace of the metric.</param> /// <param name="metricName">The name of the metric.</param> /// <returns>The list of alarm data.</returns> public async Task<List<MetricAlarm>> DescribeAlarmsForMetric(string metricNamespace, string metricName) { var alarmsResult = await _amazonCloudWatch.DescribeAlarmsForMetricAsync( new DescribeAlarmsForMetricRequest() { Namespace = metricNamespace, MetricName = metricName }); return alarmsResult.MetricAlarms; } /// <summary> /// Describe the history of an alarm for a number of days in the past. /// </summary> /// <param name="alarmName">The name of the alarm.</param> /// <param name="historyDays">The number of days in the past.</param> /// <returns>The list of alarm history data.</returns> public async Task<List<AlarmHistoryItem>> DescribeAlarmHistory(string alarmName, int historyDays) { List<AlarmHistoryItem> alarmHistory = new List<AlarmHistoryItem>(); var paginatedAlarmHistory = _amazonCloudWatch.Paginators.DescribeAlarmHistory( new DescribeAlarmHistoryRequest() { AlarmName = alarmName, EndDateUtc = DateTime.UtcNow, HistoryItemType = HistoryItemType.StateUpdate, StartDateUtc = DateTime.UtcNow.AddDays(-historyDays) }); await foreach (var data in paginatedAlarmHistory.AlarmHistoryItems) { alarmHistory.Add(data); } return alarmHistory; } /// <summary> /// Delete a list of alarms from CloudWatch. /// </summary> /// <param name="alarmNames">A list of names of alarms to delete.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteAlarms(List<string> alarmNames) { var deleteAlarmsResult = await _amazonCloudWatch.DeleteAlarmsAsync( new DeleteAlarmsRequest() { AlarmNames = alarmNames }); return deleteAlarmsResult.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Disable the actions for a list of alarms from CloudWatch. /// </summary> /// <param name="alarmNames">A list of names of alarms.</param> /// <returns>True if successful.</returns> public async Task<bool> DisableAlarmActions(List<string> alarmNames) { var disableAlarmActionsResult = await _amazonCloudWatch.DisableAlarmActionsAsync( new DisableAlarmActionsRequest() { AlarmNames = alarmNames }); return disableAlarmActionsResult.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Enable the actions for a list of alarms from CloudWatch. /// </summary> /// <param name="alarmNames">A list of names of alarms.</param> /// <returns>True if successful.</returns> public async Task<bool> EnableAlarmActions(List<string> alarmNames) { var enableAlarmActionsResult = await _amazonCloudWatch.EnableAlarmActionsAsync( new EnableAlarmActionsRequest() { AlarmNames = alarmNames }); return enableAlarmActionsResult.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Add an anomaly detector for a single metric. /// </summary> /// <param name="anomalyDetector">A single metric anomaly detector.</param> /// <returns>True if successful.</returns> public async Task<bool> PutAnomalyDetector(SingleMetricAnomalyDetector anomalyDetector) { var putAlarmDetectorResult = await _amazonCloudWatch.PutAnomalyDetectorAsync( new PutAnomalyDetectorRequest() { SingleMetricAnomalyDetector = anomalyDetector }); return putAlarmDetectorResult.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Describe anomaly detectors for a metric and namespace. /// </summary> /// <param name="metricNamespace">The namespace of the metric.</param> /// <param name="metricName">The metric of the anomaly detectors.</param> /// <returns>The list of detectors.</returns> public async Task<List<AnomalyDetector>> DescribeAnomalyDetectors(string metricNamespace, string metricName) { List<AnomalyDetector> detectors = new List<AnomalyDetector>(); var paginatedDescribeAnomalyDetectors = _amazonCloudWatch.Paginators.DescribeAnomalyDetectors( new DescribeAnomalyDetectorsRequest() { MetricName = metricName, Namespace = metricNamespace }); await foreach (var data in paginatedDescribeAnomalyDetectors.AnomalyDetectors) { detectors.Add(data); } return detectors; } /// <summary> /// Delete a single metric anomaly detector. /// </summary> /// <param name="anomalyDetector">The anomaly detector to delete.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteAnomalyDetector(SingleMetricAnomalyDetector anomalyDetector) { var deleteAnomalyDetectorResponse = await _amazonCloudWatch.DeleteAnomalyDetectorAsync( new DeleteAnomalyDetectorRequest() { SingleMetricAnomalyDetector = anomalyDetector }); return deleteAnomalyDetectorResponse.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Delete a list of CloudWatch dashboards. /// </summary> /// <param name="dashboardNames">List of dashboard names to delete.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteDashboards(List<string> dashboardNames) { var deleteDashboardsResponse = await _amazonCloudWatch.DeleteDashboardsAsync( new DeleteDashboardsRequest() { DashboardNames = dashboardNames }); return deleteDashboardsResponse.HttpStatusCode == HttpStatusCode.OK; } }
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.

import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; import software.amazon.awssdk.services.cloudwatch.model.AlarmHistoryItem; import software.amazon.awssdk.services.cloudwatch.model.AlarmType; import software.amazon.awssdk.services.cloudwatch.model.AnomalyDetector; import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatch.model.ComparisonOperator; import software.amazon.awssdk.services.cloudwatch.model.DashboardValidationMessage; import software.amazon.awssdk.services.cloudwatch.model.Datapoint; import software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsRequest; import software.amazon.awssdk.services.cloudwatch.model.DeleteAnomalyDetectorRequest; import software.amazon.awssdk.services.cloudwatch.model.DeleteDashboardsRequest; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmHistoryRequest; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmHistoryResponse; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsForMetricRequest; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsForMetricResponse; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsRequest; import software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse; import software.amazon.awssdk.services.cloudwatch.model.DescribeAnomalyDetectorsRequest; import software.amazon.awssdk.services.cloudwatch.model.DescribeAnomalyDetectorsResponse; import software.amazon.awssdk.services.cloudwatch.model.Dimension; import software.amazon.awssdk.services.cloudwatch.model.GetMetricDataRequest; import software.amazon.awssdk.services.cloudwatch.model.GetMetricDataResponse; import software.amazon.awssdk.services.cloudwatch.model.GetMetricStatisticsRequest; import software.amazon.awssdk.services.cloudwatch.model.GetMetricStatisticsResponse; import software.amazon.awssdk.services.cloudwatch.model.GetMetricWidgetImageRequest; import software.amazon.awssdk.services.cloudwatch.model.GetMetricWidgetImageResponse; import software.amazon.awssdk.services.cloudwatch.model.HistoryItemType; import software.amazon.awssdk.services.cloudwatch.model.ListMetricsRequest; import software.amazon.awssdk.services.cloudwatch.model.ListMetricsResponse; import software.amazon.awssdk.services.cloudwatch.model.Metric; import software.amazon.awssdk.services.cloudwatch.model.MetricAlarm; import software.amazon.awssdk.services.cloudwatch.model.MetricDataQuery; import software.amazon.awssdk.services.cloudwatch.model.MetricDataResult; import software.amazon.awssdk.services.cloudwatch.model.MetricDatum; import software.amazon.awssdk.services.cloudwatch.model.MetricStat; import software.amazon.awssdk.services.cloudwatch.model.PutAnomalyDetectorRequest; import software.amazon.awssdk.services.cloudwatch.model.PutDashboardRequest; import software.amazon.awssdk.services.cloudwatch.model.PutDashboardResponse; import software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest; import software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest; import software.amazon.awssdk.services.cloudwatch.model.ScanBy; import software.amazon.awssdk.services.cloudwatch.model.SingleMetricAnomalyDetector; import software.amazon.awssdk.services.cloudwatch.model.StandardUnit; import software.amazon.awssdk.services.cloudwatch.model.Statistic; import software.amazon.awssdk.services.cloudwatch.paginators.ListDashboardsIterable; import software.amazon.awssdk.services.cloudwatch.paginators.ListMetricsIterable; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Paths; import java.time.Instant; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * To enable billing metrics and statistics for this example, make sure billing * alerts are enabled for your account: * https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/monitor_estimated_charges_with_cloudwatch.html#turning_on_billing_metrics * * This Java code example performs the following tasks: * * 1. List available namespaces from Amazon CloudWatch. * 2. List available metrics within the selected Namespace. * 3. Get statistics for the selected metric over the last day. * 4. Get CloudWatch estimated billing for the last week. * 5. Create a new CloudWatch dashboard with metrics. * 6. List dashboards using a paginator. * 7. Create a new custom metric by adding data for it. * 8. Add the custom metric to the dashboard. * 9. Create an alarm for the custom metric. * 10. Describe current alarms. * 11. Get current data for the new custom metric. * 12. Push data into the custom metric to trigger the alarm. * 13. Check the alarm state using the action DescribeAlarmsForMetric. * 14. Get alarm history for the new alarm. * 15. Add an anomaly detector for the custom metric. * 16. Describe current anomaly detectors. * 17. Get a metric image for the custom metric. * 18. Clean up the Amazon CloudWatch resources. */ public class CloudWatchScenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); public static void main(String[] args) throws IOException { final String usage = """ Usage: <myDate> <costDateWeek> <dashboardName> <dashboardJson> <dashboardAdd> <settings> <metricImage> \s Where: myDate - The start date to use to get metric statistics. (For example, 2023-01-11T18:35:24.00Z.)\s costDateWeek - The start date to use to get AWS/Billinget statistics. (For example, 2023-01-11T18:35:24.00Z.)\s dashboardName - The name of the dashboard to create.\s dashboardJson - The location of a JSON file to use to create a dashboard. (See Readme file.)\s dashboardAdd - The location of a JSON file to use to update a dashboard. (See Readme file.)\s settings - The location of a JSON file from which various values are read. (See Readme file.)\s metricImage - The location of a BMP file that is used to create a graph.\s """; if (args.length != 7) { System.out.println(usage); System.exit(1); } Region region = Region.US_EAST_1; String myDate = args[0]; String costDateWeek = args[1]; String dashboardName = args[2]; String dashboardJson = args[3]; String dashboardAdd = args[4]; String settings = args[5]; String metricImage = args[6]; Double dataPoint = Double.parseDouble("10.0"); Scanner sc = new Scanner(System.in); CloudWatchClient cw = CloudWatchClient.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); System.out.println(DASHES); System.out.println("Welcome to the Amazon CloudWatch example scenario."); System.out.println(DASHES); System.out.println(DASHES); System.out.println( "1. List at least five available unique namespaces from Amazon CloudWatch. Select one from the list."); ArrayList<String> list = listNameSpaces(cw); for (int z = 0; z < 5; z++) { int index = z + 1; System.out.println(" " + index + ". " + list.get(z)); } String selectedNamespace = ""; String selectedMetrics = ""; int num = Integer.parseInt(sc.nextLine()); if (1 <= num && num <= 5) { selectedNamespace = list.get(num - 1); } else { System.out.println("You did not select a valid option."); System.exit(1); } System.out.println("You selected " + selectedNamespace); System.out.println(DASHES); System.out.println(DASHES); System.out.println("2. List available metrics within the selected namespace and select one from the list."); ArrayList<String> metList = listMets(cw, selectedNamespace); for (int z = 0; z < 5; z++) { int index = z + 1; System.out.println(" " + index + ". " + metList.get(z)); } num = Integer.parseInt(sc.nextLine()); if (1 <= num && num <= 5) { selectedMetrics = metList.get(num - 1); } else { System.out.println("You did not select a valid option."); System.exit(1); } System.out.println("You selected " + selectedMetrics); Dimension myDimension = getSpecificMet(cw, selectedNamespace); System.out.println(DASHES); System.out.println(DASHES); System.out.println("3. Get statistics for the selected metric over the last day."); String metricOption = ""; ArrayList<String> statTypes = new ArrayList<>(); statTypes.add("SampleCount"); statTypes.add("Average"); statTypes.add("Sum"); statTypes.add("Minimum"); statTypes.add("Maximum"); for (int t = 0; t < 5; t++) { System.out.println(" " + (t + 1) + ". " + statTypes.get(t)); } System.out.println("Select a metric statistic by entering a number from the preceding list:"); num = Integer.parseInt(sc.nextLine()); if (1 <= num && num <= 5) { metricOption = statTypes.get(num - 1); } else { System.out.println("You did not select a valid option."); System.exit(1); } System.out.println("You selected " + metricOption); getAndDisplayMetricStatistics(cw, selectedNamespace, selectedMetrics, metricOption, myDate, myDimension); System.out.println(DASHES); System.out.println(DASHES); System.out.println("4. Get CloudWatch estimated billing for the last week."); getMetricStatistics(cw, costDateWeek); System.out.println(DASHES); System.out.println(DASHES); System.out.println("5. Create a new CloudWatch dashboard with metrics."); createDashboardWithMetrics(cw, dashboardName, dashboardJson); System.out.println(DASHES); System.out.println(DASHES); System.out.println("6. List dashboards using a paginator."); listDashboards(cw); System.out.println(DASHES); System.out.println(DASHES); System.out.println("7. Create a new custom metric by adding data to it."); createNewCustomMetric(cw, dataPoint); System.out.println(DASHES); System.out.println(DASHES); System.out.println("8. Add an additional metric to the dashboard."); addMetricToDashboard(cw, dashboardAdd, dashboardName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("9. Create an alarm for the custom metric."); String alarmName = createAlarm(cw, settings); System.out.println(DASHES); System.out.println(DASHES); System.out.println("10. Describe ten current alarms."); describeAlarms(cw); System.out.println(DASHES); System.out.println(DASHES); System.out.println("11. Get current data for new custom metric."); getCustomMetricData(cw, settings); System.out.println(DASHES); System.out.println(DASHES); System.out.println("12. Push data into the custom metric to trigger the alarm."); addMetricDataForAlarm(cw, settings); System.out.println(DASHES); System.out.println(DASHES); System.out.println("13. Check the alarm state using the action DescribeAlarmsForMetric."); checkForMetricAlarm(cw, settings); System.out.println(DASHES); System.out.println(DASHES); System.out.println("14. Get alarm history for the new alarm."); getAlarmHistory(cw, settings, myDate); System.out.println(DASHES); System.out.println(DASHES); System.out.println("15. Add an anomaly detector for the custom metric."); addAnomalyDetector(cw, settings); System.out.println(DASHES); System.out.println(DASHES); System.out.println("16. Describe current anomaly detectors."); describeAnomalyDetectors(cw, settings); System.out.println(DASHES); System.out.println(DASHES); System.out.println("17. Get a metric image for the custom metric."); getAndOpenMetricImage(cw, metricImage); System.out.println(DASHES); System.out.println(DASHES); System.out.println("18. Clean up the Amazon CloudWatch resources."); deleteDashboard(cw, dashboardName); deleteCWAlarm(cw, alarmName); deleteAnomalyDetector(cw, settings); System.out.println(DASHES); System.out.println(DASHES); System.out.println("The Amazon CloudWatch example scenario is complete."); System.out.println(DASHES); cw.close(); } public static void deleteAnomalyDetector(CloudWatchClient cw, String fileName) { try { // Read values from the JSON file. JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText(); String customMetricName = rootNode.findValue("customMetricName").asText(); SingleMetricAnomalyDetector singleMetricAnomalyDetector = SingleMetricAnomalyDetector.builder() .metricName(customMetricName) .namespace(customMetricNamespace) .stat("Maximum") .build(); DeleteAnomalyDetectorRequest request = DeleteAnomalyDetectorRequest.builder() .singleMetricAnomalyDetector(singleMetricAnomalyDetector) .build(); cw.deleteAnomalyDetector(request); System.out.println("Successfully deleted the Anomaly Detector."); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } catch (IOException e) { e.printStackTrace(); } } public static void deleteCWAlarm(CloudWatchClient cw, String alarmName) { try { DeleteAlarmsRequest request = DeleteAlarmsRequest.builder() .alarmNames(alarmName) .build(); cw.deleteAlarms(request); System.out.println("Successfully deleted alarm " + alarmName); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void deleteDashboard(CloudWatchClient cw, String dashboardName) { try { DeleteDashboardsRequest dashboardsRequest = DeleteDashboardsRequest.builder() .dashboardNames(dashboardName) .build(); cw.deleteDashboards(dashboardsRequest); System.out.println(dashboardName + " was successfully deleted."); } catch (CloudWatchException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void getAndOpenMetricImage(CloudWatchClient cw, String fileName) { System.out.println("Getting Image data for custom metric."); try { String myJSON = "{\n" + " \"title\": \"Example Metric Graph\",\n" + " \"view\": \"timeSeries\",\n" + " \"stacked \": false,\n" + " \"period\": 10,\n" + " \"width\": 1400,\n" + " \"height\": 600,\n" + " \"metrics\": [\n" + " [\n" + " \"AWS/Billing\",\n" + " \"EstimatedCharges\",\n" + " \"Currency\",\n" + " \"USD\"\n" + " ]\n" + " ]\n" + "}"; GetMetricWidgetImageRequest imageRequest = GetMetricWidgetImageRequest.builder() .metricWidget(myJSON) .build(); GetMetricWidgetImageResponse response = cw.getMetricWidgetImage(imageRequest); SdkBytes sdkBytes = response.metricWidgetImage(); byte[] bytes = sdkBytes.asByteArray(); File outputFile = new File(fileName); try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { outputStream.write(bytes); } } catch (CloudWatchException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void describeAnomalyDetectors(CloudWatchClient cw, String fileName) { try { // Read values from the JSON file. JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText(); String customMetricName = rootNode.findValue("customMetricName").asText(); DescribeAnomalyDetectorsRequest detectorsRequest = DescribeAnomalyDetectorsRequest.builder() .maxResults(10) .metricName(customMetricName) .namespace(customMetricNamespace) .build(); DescribeAnomalyDetectorsResponse response = cw.describeAnomalyDetectors(detectorsRequest); List<AnomalyDetector> anomalyDetectorList = response.anomalyDetectors(); for (AnomalyDetector detector : anomalyDetectorList) { System.out.println("Metric name: " + detector.singleMetricAnomalyDetector().metricName()); System.out.println("State: " + detector.stateValue()); } } catch (CloudWatchException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void addAnomalyDetector(CloudWatchClient cw, String fileName) { try { // Read values from the JSON file. JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText(); String customMetricName = rootNode.findValue("customMetricName").asText(); SingleMetricAnomalyDetector singleMetricAnomalyDetector = SingleMetricAnomalyDetector.builder() .metricName(customMetricName) .namespace(customMetricNamespace) .stat("Maximum") .build(); PutAnomalyDetectorRequest anomalyDetectorRequest = PutAnomalyDetectorRequest.builder() .singleMetricAnomalyDetector(singleMetricAnomalyDetector) .build(); cw.putAnomalyDetector(anomalyDetectorRequest); System.out.println("Added anomaly detector for metric " + customMetricName + "."); } catch (CloudWatchException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void getAlarmHistory(CloudWatchClient cw, String fileName, String date) { try { // Read values from the JSON file. JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); String alarmName = rootNode.findValue("exampleAlarmName").asText(); Instant start = Instant.parse(date); Instant endDate = Instant.now(); DescribeAlarmHistoryRequest historyRequest = DescribeAlarmHistoryRequest.builder() .startDate(start) .endDate(endDate) .alarmName(alarmName) .historyItemType(HistoryItemType.ACTION) .build(); DescribeAlarmHistoryResponse response = cw.describeAlarmHistory(historyRequest); List<AlarmHistoryItem> historyItems = response.alarmHistoryItems(); if (historyItems.isEmpty()) { System.out.println("No alarm history data found for " + alarmName + "."); } else { for (AlarmHistoryItem item : historyItems) { System.out.println("History summary: " + item.historySummary()); System.out.println("Time stamp: " + item.timestamp()); } } } catch (CloudWatchException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void checkForMetricAlarm(CloudWatchClient cw, String fileName) { try { // Read values from the JSON file. JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText(); String customMetricName = rootNode.findValue("customMetricName").asText(); boolean hasAlarm = false; int retries = 10; DescribeAlarmsForMetricRequest metricRequest = DescribeAlarmsForMetricRequest.builder() .metricName(customMetricName) .namespace(customMetricNamespace) .build(); while (!hasAlarm && retries > 0) { DescribeAlarmsForMetricResponse response = cw.describeAlarmsForMetric(metricRequest); hasAlarm = response.hasMetricAlarms(); retries--; Thread.sleep(20000); System.out.println("."); } if (!hasAlarm) System.out.println("No Alarm state found for " + customMetricName + " after 10 retries."); else System.out.println("Alarm state found for " + customMetricName + "."); } catch (CloudWatchException | IOException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void addMetricDataForAlarm(CloudWatchClient cw, String fileName) { try { // Read values from the JSON file. JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText(); String customMetricName = rootNode.findValue("customMetricName").asText(); // Set an Instant object. String time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT); Instant instant = Instant.parse(time); MetricDatum datum = MetricDatum.builder() .metricName(customMetricName) .unit(StandardUnit.NONE) .value(1001.00) .timestamp(instant) .build(); MetricDatum datum2 = MetricDatum.builder() .metricName(customMetricName) .unit(StandardUnit.NONE) .value(1002.00) .timestamp(instant) .build(); List<MetricDatum> metricDataList = new ArrayList<>(); metricDataList.add(datum); metricDataList.add(datum2); PutMetricDataRequest request = PutMetricDataRequest.builder() .namespace(customMetricNamespace) .metricData(metricDataList) .build(); cw.putMetricData(request); System.out.println("Added metric values for for metric " + customMetricName); } catch (CloudWatchException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void getCustomMetricData(CloudWatchClient cw, String fileName) { try { // Read values from the JSON file. JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText(); String customMetricName = rootNode.findValue("customMetricName").asText(); // Set the date. Instant nowDate = Instant.now(); long hours = 1; long minutes = 30; Instant date2 = nowDate.plus(hours, ChronoUnit.HOURS).plus(minutes, ChronoUnit.MINUTES); Metric met = Metric.builder() .metricName(customMetricName) .namespace(customMetricNamespace) .build(); MetricStat metStat = MetricStat.builder() .stat("Maximum") .period(1) .metric(met) .build(); MetricDataQuery dataQUery = MetricDataQuery.builder() .metricStat(metStat) .id("foo2") .returnData(true) .build(); List<MetricDataQuery> dq = new ArrayList<>(); dq.add(dataQUery); GetMetricDataRequest getMetReq = GetMetricDataRequest.builder() .maxDatapoints(10) .scanBy(ScanBy.TIMESTAMP_DESCENDING) .startTime(nowDate) .endTime(date2) .metricDataQueries(dq) .build(); GetMetricDataResponse response = cw.getMetricData(getMetReq); List<MetricDataResult> data = response.metricDataResults(); for (MetricDataResult item : data) { System.out.println("The label is " + item.label()); System.out.println("The status code is " + item.statusCode().toString()); } } catch (CloudWatchException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void describeAlarms(CloudWatchClient cw) { try { List<AlarmType> typeList = new ArrayList<>(); typeList.add(AlarmType.METRIC_ALARM); DescribeAlarmsRequest alarmsRequest = DescribeAlarmsRequest.builder() .alarmTypes(typeList) .maxRecords(10) .build(); DescribeAlarmsResponse response = cw.describeAlarms(alarmsRequest); List<MetricAlarm> alarmList = response.metricAlarms(); for (MetricAlarm alarm : alarmList) { System.out.println("Alarm name: " + alarm.alarmName()); System.out.println("Alarm description: " + alarm.alarmDescription()); } } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static String createAlarm(CloudWatchClient cw, String fileName) { try { // Read values from the JSON file. JsonParser parser = new JsonFactory().createParser(new File(fileName)); com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser); 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(); cw.putMetricAlarm(alarmRequest); System.out.println(alarmName + " was successfully created!"); return alarmName; } catch (CloudWatchException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } public static void addMetricToDashboard(CloudWatchClient cw, String fileName, String dashboardName) { try { PutDashboardRequest dashboardRequest = PutDashboardRequest.builder() .dashboardName(dashboardName) .dashboardBody(readFileAsString(fileName)) .build(); cw.putDashboard(dashboardRequest); System.out.println(dashboardName + " was successfully updated."); } catch (CloudWatchException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } public static void createNewCustomMetric(CloudWatchClient cw, Double dataPoint) { try { Dimension dimension = Dimension.builder() .name("UNIQUE_PAGES") .value("URLS") .build(); // Set an Instant object. String time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT); Instant instant = Instant.parse(time); MetricDatum datum = MetricDatum.builder() .metricName("PAGES_VISITED") .unit(StandardUnit.NONE) .value(dataPoint) .timestamp(instant) .dimensions(dimension) .build(); PutMetricDataRequest request = PutMetricDataRequest.builder() .namespace("SITE/TRAFFIC") .metricData(datum) .build(); cw.putMetricData(request); System.out.println("Added metric values for for metric PAGES_VISITED"); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void listDashboards(CloudWatchClient cw) { try { ListDashboardsIterable listRes = cw.listDashboardsPaginator(); listRes.stream() .flatMap(r -> r.dashboardEntries().stream()) .forEach(entry -> { System.out.println("Dashboard name is: " + entry.dashboardName()); System.out.println("Dashboard ARN is: " + entry.dashboardArn()); }); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void createDashboardWithMetrics(CloudWatchClient cw, String dashboardName, String fileName) { try { PutDashboardRequest dashboardRequest = PutDashboardRequest.builder() .dashboardName(dashboardName) .dashboardBody(readFileAsString(fileName)) .build(); PutDashboardResponse response = cw.putDashboard(dashboardRequest); System.out.println(dashboardName + " was successfully created."); List<DashboardValidationMessage> messages = response.dashboardValidationMessages(); if (messages.isEmpty()) { System.out.println("There are no messages in the new Dashboard"); } else { for (DashboardValidationMessage message : messages) { System.out.println("Message is: " + message.message()); } } } catch (CloudWatchException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } } public static String readFileAsString(String file) throws IOException { return new String(Files.readAllBytes(Paths.get(file))); } public static void getMetricStatistics(CloudWatchClient cw, String costDateWeek) { try { Instant start = Instant.parse(costDateWeek); Instant endDate = Instant.now(); Dimension dimension = Dimension.builder() .name("Currency") .value("USD") .build(); List<Dimension> dimensionList = new ArrayList<>(); dimensionList.add(dimension); GetMetricStatisticsRequest statisticsRequest = GetMetricStatisticsRequest.builder() .metricName("EstimatedCharges") .namespace("AWS/Billing") .dimensions(dimensionList) .statistics(Statistic.MAXIMUM) .startTime(start) .endTime(endDate) .period(86400) .build(); GetMetricStatisticsResponse response = cw.getMetricStatistics(statisticsRequest); List<Datapoint> data = response.datapoints(); if (!data.isEmpty()) { for (Datapoint datapoint : data) { System.out .println("Timestamp: " + datapoint.timestamp() + " Maximum value: " + datapoint.maximum()); } } else { System.out.println("The returned data list is empty"); } } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } public static void getAndDisplayMetricStatistics(CloudWatchClient cw, String nameSpace, String metVal, String metricOption, String date, Dimension myDimension) { try { Instant start = Instant.parse(date); Instant endDate = Instant.now(); GetMetricStatisticsRequest statisticsRequest = GetMetricStatisticsRequest.builder() .endTime(endDate) .startTime(start) .dimensions(myDimension) .metricName(metVal) .namespace(nameSpace) .period(86400) .statistics(Statistic.fromValue(metricOption)) .build(); GetMetricStatisticsResponse response = cw.getMetricStatistics(statisticsRequest); List<Datapoint> data = response.datapoints(); if (!data.isEmpty()) { for (Datapoint datapoint : data) { System.out .println("Timestamp: " + datapoint.timestamp() + " Maximum value: " + datapoint.maximum()); } } else { System.out.println("The returned data list is empty"); } } catch (CloudWatchException e) { System.err.println(e.getMessage()); System.exit(1); } } public static Dimension getSpecificMet(CloudWatchClient cw, String namespace) { try { ListMetricsRequest request = ListMetricsRequest.builder() .namespace(namespace) .build(); ListMetricsResponse response = cw.listMetrics(request); List<Metric> myList = response.metrics(); Metric metric = myList.get(0); return metric.dimensions().get(0); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; } public static ArrayList<String> listMets(CloudWatchClient cw, String namespace) { try { ArrayList<String> metList = new ArrayList<>(); ListMetricsRequest request = ListMetricsRequest.builder() .namespace(namespace) .build(); ListMetricsIterable listRes = cw.listMetricsPaginator(request); listRes.stream() .flatMap(r -> r.metrics().stream()) .forEach(metrics -> metList.add(metrics.metricName())); return metList; } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; } public static ArrayList<String> listNameSpaces(CloudWatchClient cw) { try { ArrayList<String> nameSpaceList = new ArrayList<>(); ListMetricsRequest request = ListMetricsRequest.builder() .build(); ListMetricsIterable listRes = cw.listMetricsPaginator(request); listRes.stream() .flatMap(r -> r.metrics().stream()) .forEach(metrics -> { String data = metrics.namespace(); if (!nameSpaceList.contains(data)) { nameSpaceList.add(data); } }); return nameSpaceList; } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; } }
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.

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html To enable billing metrics and statistics for this example, make sure billing alerts are enabled for your account: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/monitor_estimated_charges_with_cloudwatch.html#turning_on_billing_metrics This Kotlin code example performs the following tasks: 1. List available namespaces from Amazon CloudWatch. Select a namespace from the list. 2. List available metrics within the selected namespace. 3. Get statistics for the selected metric over the last day. 4. Get CloudWatch estimated billing for the last week. 5. Create a new CloudWatch dashboard with metrics. 6. List dashboards using a paginator. 7. Create a new custom metric by adding data for it. 8. Add the custom metric to the dashboard. 9. Create an alarm for the custom metric. 10. Describe current alarms. 11. Get current data for the new custom metric. 12. Push data into the custom metric to trigger the alarm. 13. Check the alarm state using the action DescribeAlarmsForMetric. 14. Get alarm history for the new alarm. 15. Add an anomaly detector for the custom metric. 16. Describe current anomaly detectors. 17. Get a metric image for the custom metric. 18. Clean up the Amazon CloudWatch resources. */ val DASHES: String? = String(CharArray(80)).replace("\u0000", "-") suspend fun main(args: Array<String>) { val usage = """ Usage: <myDate> <costDateWeek> <dashboardName> <dashboardJson> <dashboardAdd> <settings> <metricImage> Where: myDate - The start date to use to get metric statistics. (For example, 2023-01-11T18:35:24.00Z.) costDateWeek - The start date to use to get AWS Billing and Cost Management statistics. (For example, 2023-01-11T18:35:24.00Z.) dashboardName - The name of the dashboard to create. dashboardJson - The location of a JSON file to use to create a dashboard. (See Readme file.) dashboardAdd - The location of a JSON file to use to update a dashboard. (See Readme file.) settings - The location of a JSON file from which various values are read. (See Readme file.) metricImage - The location of a BMP file that is used to create a graph. """ if (args.size != 7) { println(usage) System.exit(1) } val myDate = args[0] val costDateWeek = args[1] val dashboardName = args[2] val dashboardJson = args[3] val dashboardAdd = args[4] val settings = args[5] var metricImage = args[6] val dataPoint = "10.0".toDouble() val inOb = Scanner(System.`in`) println(DASHES) println("Welcome to the Amazon CloudWatch example scenario.") println(DASHES) println(DASHES) println("1. List at least five available unique namespaces from Amazon CloudWatch. Select a CloudWatch namespace from the list.") val list: ArrayList<String> = listNameSpaces() for (z in 0..4) { println(" ${z + 1}. ${list[z]}") } var selectedNamespace: String var selectedMetrics = "" var num = inOb.nextLine().toInt() println("You selected $num") if (1 <= num && num <= 5) { selectedNamespace = list[num - 1] } else { println("You did not select a valid option.") exitProcess(1) } println("You selected $selectedNamespace") println(DASHES) println(DASHES) println("2. List available metrics within the selected namespace and select one from the list.") val metList = listMets(selectedNamespace) for (z in 0..4) { println(" ${ z + 1}. ${metList?.get(z)}") } num = inOb.nextLine().toInt() if (1 <= num && num <= 5) { selectedMetrics = metList!![num - 1] } else { println("You did not select a valid option.") System.exit(1) } println("You selected $selectedMetrics") val myDimension = getSpecificMet(selectedNamespace) if (myDimension == null) { println("Error - Dimension is null") exitProcess(1) } println(DASHES) println(DASHES) println("3. Get statistics for the selected metric over the last day.") val metricOption: String val statTypes = ArrayList<String>() statTypes.add("SampleCount") statTypes.add("Average") statTypes.add("Sum") statTypes.add("Minimum") statTypes.add("Maximum") for (t in 0..4) { println(" ${t + 1}. ${statTypes[t]}") } println("Select a metric statistic by entering a number from the preceding list:") num = inOb.nextLine().toInt() if (1 <= num && num <= 5) { metricOption = statTypes[num - 1] } else { println("You did not select a valid option.") exitProcess(1) } println("You selected $metricOption") getAndDisplayMetricStatistics(selectedNamespace, selectedMetrics, metricOption, myDate, myDimension) println(DASHES) println(DASHES) println("4. Get CloudWatch estimated billing for the last week.") getMetricStatistics(costDateWeek) println(DASHES) println(DASHES) println("5. Create a new CloudWatch dashboard with metrics.") createDashboardWithMetrics(dashboardName, dashboardJson) println(DASHES) println(DASHES) println("6. List dashboards using a paginator.") listDashboards() println(DASHES) println(DASHES) println("7. Create a new custom metric by adding data to it.") createNewCustomMetric(dataPoint) println(DASHES) println(DASHES) println("8. Add an additional metric to the dashboard.") addMetricToDashboard(dashboardAdd, dashboardName) println(DASHES) println(DASHES) println("9. Create an alarm for the custom metric.") val alarmName: String = createAlarm(settings) println(DASHES) println(DASHES) println("10. Describe 10 current alarms.") describeAlarms() println(DASHES) println(DASHES) println("11. Get current data for the new custom metric.") getCustomMetricData(settings) println(DASHES) println(DASHES) println("12. Push data into the custom metric to trigger the alarm.") addMetricDataForAlarm(settings) println(DASHES) println(DASHES) println("13. Check the alarm state using the action DescribeAlarmsForMetric.") checkForMetricAlarm(settings) println(DASHES) println(DASHES) println("14. Get alarm history for the new alarm.") getAlarmHistory(settings, myDate) println(DASHES) println(DASHES) println("15. Add an anomaly detector for the custom metric.") addAnomalyDetector(settings) println(DASHES) println(DASHES) println("16. Describe current anomaly detectors.") describeAnomalyDetectors(settings) println(DASHES) println(DASHES) println("17. Get a metric image for the custom metric.") getAndOpenMetricImage(metricImage) println(DASHES) println(DASHES) println("18. Clean up the Amazon CloudWatch resources.") deleteDashboard(dashboardName) deleteAlarm(alarmName) deleteAnomalyDetector(settings) println(DASHES) println(DASHES) println("The Amazon CloudWatch example scenario is complete.") println(DASHES) } suspend fun deleteAnomalyDetector(fileName: String) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText() val customMetricName = rootNode.findValue("customMetricName").asText() val singleMetricAnomalyDetectorVal = SingleMetricAnomalyDetector { metricName = customMetricName namespace = customMetricNamespace stat = "Maximum" } val request = DeleteAnomalyDetectorRequest { singleMetricAnomalyDetector = singleMetricAnomalyDetectorVal } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.deleteAnomalyDetector(request) println("Successfully deleted the Anomaly Detector.") } } suspend fun deleteAlarm(alarmNameVal: String) { val request = DeleteAlarmsRequest { alarmNames = listOf(alarmNameVal) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.deleteAlarms(request) println("Successfully deleted alarm $alarmNameVal") } } suspend fun deleteDashboard(dashboardName: String) { val dashboardsRequest = DeleteDashboardsRequest { dashboardNames = listOf(dashboardName) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.deleteDashboards(dashboardsRequest) println("$dashboardName was successfully deleted.") } } suspend fun getAndOpenMetricImage(fileName: String) { println("Getting Image data for custom metric.") val myJSON = """{ "title": "Example Metric Graph", "view": "timeSeries", "stacked ": false, "period": 10, "width": 1400, "height": 600, "metrics": [ [ "AWS/Billing", "EstimatedCharges", "Currency", "USD" ] ] }""" val imageRequest = GetMetricWidgetImageRequest { metricWidget = myJSON } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricWidgetImage(imageRequest) val bytes = response.metricWidgetImage if (bytes != null) { File(fileName).writeBytes(bytes) } } println("You have successfully written data to $fileName") } suspend fun describeAnomalyDetectors(fileName: String) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText() val customMetricName = rootNode.findValue("customMetricName").asText() val detectorsRequest = DescribeAnomalyDetectorsRequest { maxResults = 10 metricName = customMetricName namespace = customMetricNamespace } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.describeAnomalyDetectors(detectorsRequest) response.anomalyDetectors?.forEach { detector -> println("Metric name: ${detector.singleMetricAnomalyDetector?.metricName}") println("State: ${detector.stateValue}") } } } suspend fun addAnomalyDetector(fileName: String?) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText() val customMetricName = rootNode.findValue("customMetricName").asText() val singleMetricAnomalyDetectorVal = SingleMetricAnomalyDetector { metricName = customMetricName namespace = customMetricNamespace stat = "Maximum" } val anomalyDetectorRequest = PutAnomalyDetectorRequest { singleMetricAnomalyDetector = singleMetricAnomalyDetectorVal } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putAnomalyDetector(anomalyDetectorRequest) println("Added anomaly detector for metric $customMetricName.") } } suspend fun getAlarmHistory( fileName: String, date: String, ) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val alarmNameVal = rootNode.findValue("exampleAlarmName").asText() val start = Instant.parse(date) val endDateVal = Instant.now() val historyRequest = DescribeAlarmHistoryRequest { startDate = aws.smithy.kotlin.runtime.time .Instant(start) endDate = aws.smithy.kotlin.runtime.time .Instant(endDateVal) alarmName = alarmNameVal historyItemType = HistoryItemType.Action } CloudWatchClient { credentialsProvider = EnvironmentCredentialsProvider() region = "us-east-1" }.use { cwClient -> val response = cwClient.describeAlarmHistory(historyRequest) val historyItems = response.alarmHistoryItems if (historyItems != null) { if (historyItems.isEmpty()) { println("No alarm history data found for $alarmNameVal.") } else { for (item in historyItems) { println("History summary ${item.historySummary}") println("Time stamp: ${item.timestamp}") } } } } } suspend fun checkForMetricAlarm(fileName: String?) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText() val customMetricName = rootNode.findValue("customMetricName").asText() var hasAlarm = false var retries = 10 val metricRequest = DescribeAlarmsForMetricRequest { metricName = customMetricName namespace = customMetricNamespace } CloudWatchClient { region = "us-east-1" }.use { cwClient -> while (!hasAlarm && retries > 0) { val response = cwClient.describeAlarmsForMetric(metricRequest) if (response.metricAlarms?.count()!! > 0) { hasAlarm = true } retries-- delay(20000) println(".") } if (!hasAlarm) { println("No Alarm state found for $customMetricName after 10 retries.") } else { println("Alarm state found for $customMetricName.") } } } suspend fun addMetricDataForAlarm(fileName: String?) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText() val customMetricName = rootNode.findValue("customMetricName").asText() // Set an Instant object. val time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT) val instant = Instant.parse(time) val datum = MetricDatum { metricName = customMetricName unit = StandardUnit.None value = 1001.00 timestamp = aws.smithy.kotlin.runtime.time .Instant(instant) } val datum2 = MetricDatum { metricName = customMetricName unit = StandardUnit.None value = 1002.00 timestamp = aws.smithy.kotlin.runtime.time .Instant(instant) } val metricDataList = ArrayList<MetricDatum>() metricDataList.add(datum) metricDataList.add(datum2) val request = PutMetricDataRequest { namespace = customMetricNamespace metricData = metricDataList } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putMetricData(request) println("Added metric values for for metric $customMetricName") } } suspend fun getCustomMetricData(fileName: String) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText() val customMetricName = rootNode.findValue("customMetricName").asText() // Set the date. val nowDate = Instant.now() val hours: Long = 1 val minutes: Long = 30 val date2 = nowDate.plus(hours, ChronoUnit.HOURS).plus( minutes, ChronoUnit.MINUTES, ) val met = Metric { metricName = customMetricName namespace = customMetricNamespace } val metStat = MetricStat { stat = "Maximum" period = 1 metric = met } val dataQUery = MetricDataQuery { metricStat = metStat id = "foo2" returnData = true } val dq = ArrayList<MetricDataQuery>() dq.add(dataQUery) val getMetReq = GetMetricDataRequest { maxDatapoints = 10 scanBy = ScanBy.TimestampDescending startTime = aws.smithy.kotlin.runtime.time .Instant(nowDate) endTime = aws.smithy.kotlin.runtime.time .Instant(date2) metricDataQueries = dq } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricData(getMetReq) response.metricDataResults?.forEach { item -> println("The label is ${item.label}") println("The status code is ${item.statusCode}") } } } 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}") } } } suspend fun createAlarm(fileName: String): String { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode: JsonNode = ObjectMapper().readTree(parser) val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText() val customMetricName = rootNode.findValue("customMetricName").asText() val alarmNameVal = rootNode.findValue("exampleAlarmName").asText() val emailTopic = rootNode.findValue("emailTopic").asText() val accountId = rootNode.findValue("accountId").asText() val region2 = rootNode.findValue("region").asText() // Create a List for alarm actions. val alarmActionObs: MutableList<String> = ArrayList() alarmActionObs.add("arn:aws:sns:$region2:$accountId:$emailTopic") val alarmRequest = PutMetricAlarmRequest { alarmActions = alarmActionObs alarmDescription = "Example metric alarm" alarmName = alarmNameVal comparisonOperator = ComparisonOperator.GreaterThanOrEqualToThreshold threshold = 100.00 metricName = customMetricName namespace = customMetricNamespace evaluationPeriods = 1 period = 10 statistic = Statistic.Maximum datapointsToAlarm = 1 treatMissingData = "ignore" } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putMetricAlarm(alarmRequest) println("$alarmNameVal was successfully created!") return alarmNameVal } } suspend fun addMetricToDashboard( fileNameVal: String, dashboardNameVal: String, ) { val dashboardRequest = PutDashboardRequest { dashboardName = dashboardNameVal dashboardBody = readFileAsString(fileNameVal) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putDashboard(dashboardRequest) println("$dashboardNameVal was successfully updated.") } } suspend fun createNewCustomMetric(dataPoint: Double) { val dimension = Dimension { name = "UNIQUE_PAGES" value = "URLS" } // Set an Instant object. val time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT) val instant = Instant.parse(time) val datum = MetricDatum { metricName = "PAGES_VISITED" unit = StandardUnit.None value = dataPoint timestamp = aws.smithy.kotlin.runtime.time .Instant(instant) dimensions = listOf(dimension) } val request = PutMetricDataRequest { namespace = "SITE/TRAFFIC" metricData = listOf(datum) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.putMetricData(request) println("Added metric values for for metric PAGES_VISITED") } } suspend fun listDashboards() { CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient .listDashboardsPaginated({}) .transform { it.dashboardEntries?.forEach { obj -> emit(obj) } } .collect { obj -> println("Name is ${obj.dashboardName}") println("Dashboard ARN is ${obj.dashboardArn}") } } } suspend fun createDashboardWithMetrics( dashboardNameVal: String, fileNameVal: String, ) { val dashboardRequest = PutDashboardRequest { dashboardName = dashboardNameVal dashboardBody = readFileAsString(fileNameVal) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.putDashboard(dashboardRequest) println("$dashboardNameVal was successfully created.") val messages = response.dashboardValidationMessages if (messages != null) { if (messages.isEmpty()) { println("There are no messages in the new Dashboard") } else { for (message in messages) { println("Message is: ${message.message}") } } } } } fun readFileAsString(file: String): String = String(Files.readAllBytes(Paths.get(file))) suspend fun getMetricStatistics(costDateWeek: String?) { val start = Instant.parse(costDateWeek) val endDate = Instant.now() val dimension = Dimension { name = "Currency" value = "USD" } val dimensionList: MutableList<Dimension> = ArrayList() dimensionList.add(dimension) val statisticsRequest = GetMetricStatisticsRequest { metricName = "EstimatedCharges" namespace = "AWS/Billing" dimensions = dimensionList statistics = listOf(Statistic.Maximum) startTime = aws.smithy.kotlin.runtime.time .Instant(start) endTime = aws.smithy.kotlin.runtime.time .Instant(endDate) period = 86400 } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricStatistics(statisticsRequest) val data: List<Datapoint>? = response.datapoints if (data != null) { if (!data.isEmpty()) { for (datapoint in data) { println("Timestamp: ${datapoint.timestamp} Maximum value: ${datapoint.maximum}") } } else { println("The returned data list is empty") } } } } suspend fun getAndDisplayMetricStatistics( nameSpaceVal: String, metVal: String, metricOption: String, date: String, myDimension: Dimension, ) { val start = Instant.parse(date) val endDate = Instant.now() val statisticsRequest = GetMetricStatisticsRequest { endTime = aws.smithy.kotlin.runtime.time .Instant(endDate) startTime = aws.smithy.kotlin.runtime.time .Instant(start) dimensions = listOf(myDimension) metricName = metVal namespace = nameSpaceVal period = 86400 statistics = listOf(Statistic.fromValue(metricOption)) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.getMetricStatistics(statisticsRequest) val data = response.datapoints if (data != null) { if (data.isNotEmpty()) { for (datapoint in data) { println("Timestamp: ${datapoint.timestamp} Maximum value: ${datapoint.maximum}") } } else { println("The returned data list is empty") } } } } suspend fun listMets(namespaceVal: String?): ArrayList<String>? { val metList = ArrayList<String>() val request = ListMetricsRequest { namespace = namespaceVal } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val reponse = cwClient.listMetrics(request) reponse.metrics?.forEach { metrics -> val data = metrics.metricName if (!metList.contains(data)) { metList.add(data!!) } } } return metList } suspend fun getSpecificMet(namespaceVal: String?): Dimension? { val request = ListMetricsRequest { namespace = namespaceVal } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.listMetrics(request) val myList = response.metrics if (myList != null) { return myList[0].dimensions?.get(0) } } return null } suspend fun listNameSpaces(): ArrayList<String> { val nameSpaceList = ArrayList<String>() CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.listMetrics(ListMetricsRequest {}) response.metrics?.forEach { metrics -> val data = metrics.namespace if (!nameSpaceList.contains(data)) { nameSpaceList.add(data!!) } } } return nameSpaceList }

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.