Describing CloudWatch Alarms - AWS SDK for Go (version 1)

We announced the upcoming end-of-support for AWS SDK for Go V1. We recommend that you migrate to AWS SDK for Go V2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

Describing CloudWatch Alarms

This example shows you how to retrieve basic information that describes your CloudWatch alarms.

You can download complete versions of these example files from the aws-doc-sdk-examples repository on GitHub.

Scenario

An alarm watches a single metric over a time period you specify. The alarm performs one or more actions based on the value of the metric relative to a given threshold over a number of time periods.

In this example, Go code is used to describe alarms in CloudWatch. The code uses the AWS SDK for Go to describe alarms by using this method of the AWS.CloudWatch client class:

Prerequisites

Describe Alarms

Choose Copy to save the code locally.

Create the file describe_alarms.go. Import the packages used in the example.

import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudwatch" "fmt" "os" )

Initialize a session that the SDK will use to load credentials from the shared credentials file ~/.aws/credentials, load your configuration from the shared configuration file ~/.aws/config, and create a CloudWatch client.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := cloudwatch.New(sess)

Call DescribeAlarms, and print the results.

resp, err := svc.DescribeAlarms(nil) for _, alarm := range resp.MetricAlarms { fmt.Println(*alarm.AlarmName) }

See the complete example on GitHub.