使用 Amazon 开发工具包删除 CloudWatch 告警 - Amazon CloudWatch
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 Amazon 开发工具包删除 CloudWatch 告警

以下代码示例展示如何删除 Amazon CloudWatch 告警。

.NET
Amazon SDK for .NET

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.CloudWatch; using Amazon.CloudWatch.Model; /// <summary> /// This example shows how to delete Amazon CloudWatch alarms. The example /// was created using the AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class DeleteAlarms { public static async Task Main() { IAmazonCloudWatch cwClient = new AmazonCloudWatchClient(); var alarmNames = CreateAlarmNameList(); await DeleteAlarmsAsyncExample(cwClient, alarmNames); } /// <summary> /// Delete the alarms whose names are listed in the alarmNames parameter. /// </summary> /// <param name="client">The initialized Amazon CloudWatch client.</param> /// <param name="alarmNames">A list of names for the alarms to be /// deleted.</param> public static async Task DeleteAlarmsAsyncExample(IAmazonCloudWatch client, List<string> alarmNames) { var request = new DeleteAlarmsRequest { AlarmNames = alarmNames, }; try { var response = await client.DeleteAlarmsAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine("Alarms successfully deleted:"); alarmNames .ForEach(name => Console.WriteLine($"{name}")); } } catch (ResourceNotFoundException ex) { Console.WriteLine($"Error: {ex.Message}"); } } /// <summary> /// Defines and returns the list of alarm names to delete. /// </summary> /// <returns>A list of alarm names.</returns> public static List<string> CreateAlarmNameList() { // The list of alarm names passed to DeleteAlarmsAsync // can contain up to 100 alarm names. var theList = new List<string> { "ALARM_NAME_1", "ALARM_NAME_2", }; return theList; } }
Java
SDK for Java 2.x

public static void deleteCWAlarm(CloudWatchClient cw, String alarmName) { try { DeleteAlarmsRequest request = DeleteAlarmsRequest.builder() .alarmNames(alarmName) .build(); cw.deleteAlarms(request); System.out.printf("Successfully deleted alarm %s", alarmName); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
JavaScript
SDK for JavaScript V3

在单独的模块中创建客户端并将其导出。

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });

导入开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import { DeleteAlarmsCommand } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { AlarmNames: "ALARM_NAME" }; // e.g., "Web_Server_CPU_Utilization" export const run = async () => { try { const data = await cwClient.send(new DeleteAlarmsCommand(params)); console.log("Success, alarm deleted; requestID:", data); return data; } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
SDK for JavaScript V2

导入开发工具包和客户端模块,然后调用 API。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); var params = { AlarmNames: ['Web_Server_CPU_Utilization'] }; cw.deleteAlarms(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
Kotlin
SDK for Kotlin
注意

这是适用于预览版中功能的预发行文档。本文档随时可能更改。

suspend fun deleteCWAlarm(alarmNameVal: String) { val request = DeleteAlarmsRequest { alarmNames = listOf(alarmNameVal) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> cwClient.deleteAlarms(request) println("Successfully deleted alarm $alarmNameVal") } }
  • GitHub 中查找说明和更多代码。

  • 有关 API 详细信息,请参阅 Amazon SDK for Kotlin API 参考中的 DeleteAlarms

Python
适用于 Python (Boto3) 的 SDK

class CloudWatchWrapper: """Encapsulates Amazon CloudWatch functions.""" def __init__(self, cloudwatch_resource): """ :param cloudwatch_resource: A Boto3 CloudWatch resource. """ self.cloudwatch_resource = cloudwatch_resource def delete_metric_alarms(self, metric_namespace, metric_name): """ Deletes all of the alarms that are currently watching the specified metric. :param metric_namespace: The namespace of the metric. :param metric_name: The name of the metric. """ try: metric = self.cloudwatch_resource.Metric(metric_namespace, metric_name) metric.alarms.delete() logger.info( "Deleted alarms for metric %s.%s.", metric_namespace, metric_name) except ClientError: logger.exception( "Couldn't delete alarms for metric %s.%s.", metric_namespace, metric_name) raise
  • GitHub 中查找说明和更多代码。

  • 有关 API 详细信息,请参阅 Amazon 适用于 Python (Boto3) 的 SDK API 参考中的 DeleteAlarms

有关 Amazon 开发工具包开发人员指南和代码示例的完整列表,包括帮助入门和有关早期版本的信息,请参阅 将 CloudWatch 与 Amazon 开发工具包结合使用