/
DeleteAlarm.java
67 lines (56 loc) · 2.23 KB
/
DeleteAlarm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//snippet-sourcedescription:[DeleteAlarm.java demonstrates how to delete an Amazon CloudWatch alarm.]
//snippet-keyword:[AWS SDK for Java v2]
//snippet-service:[Amazon CloudWatch]
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package com.example.cloudwatch;
// snippet-start:[cloudwatch.java2.delete_metrics.main]
// snippet-start:[cloudwatch.java2.delete_metrics.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;
import software.amazon.awssdk.services.cloudwatch.model.DeleteAlarmsRequest;
// snippet-end:[cloudwatch.java2.delete_metrics.import]
/**
* 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
*/
public class DeleteAlarm {
public static void main(String[] args) {
final String usage = """
Usage:
<alarmName>
Where:
alarmName - An alarm name to delete (for example, MyAlarm).
""";
if (args.length != 1) {
System.out.println(usage);
System.exit(1);
}
String alarmName = args[0];
Region region = Region.US_EAST_2;
CloudWatchClient cw = CloudWatchClient.builder()
.region(region)
.build();
deleteCWAlarm(cw, alarmName) ;
cw.close();
}
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);
}
}
}
// snippet-end:[cloudwatch.java2.delete_metrics.main]