-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
DescribeAlarms.go
64 lines (53 loc) · 1.76 KB
/
DescribeAlarms.go
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[cloudwatch.go.describe_alarms]
package main
// snippet-start:[cloudwatch.go.describe_alarms.imports]
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
// snippet-end:[cloudwatch.go.describe_alarms.imports]
// ListAlarms returns a list of your Amazon CloudWatch alarms
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
//
// Output:
//
// If success, the list of alarms and nil
// Otherwise, nil and an error from the call to DescribeAlarms
func ListAlarms(sess *session.Session) (*cloudwatch.DescribeAlarmsOutput, error) {
// Create new service client
// snippet-start:[cloudwatch.go.describe_alarms.call]
svc := cloudwatch.New(sess)
resp, err := svc.DescribeAlarms(nil)
// snippet-end:[cloudwatch.go.describe_alarms.call]
if err != nil {
return nil, err
}
return resp, nil
}
func main() {
// Initialize a session that the SDK uses to load
// credentials from the shared credentials file (~/.aws/credentials)
// snippet-start:[cloudwatch.go.describe_alarms.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[cloudwatch.go.describe_alarms.session]
resp, err := ListAlarms(sess)
if err != nil {
fmt.Println("Got an error listing alarms:")
fmt.Println(err)
return
}
// snippet-start:[cloudwatch.go.describe_alarms.display]
fmt.Println("Alarms:")
for _, alarm := range resp.MetricAlarms {
fmt.Println(" " + *alarm.AlarmName)
}
// snippet-end:[cloudwatch.go.describe_alarms.display]
}
// snippet-end:[cloudwatch.go.describe_alarms]