Menu
AWS SDK for Go
Developer Guide

Using Alarms and Alarm Actions in CloudWatch

These Go examples show you how to change the state of your Amazon EC2 instances automatically based on a CloudWatch alarm, as follows:

  • Creating and enabling actions on an alarm

  • Disabling actions on an alarm

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

Scenario

You can use alarm actions to create alarms that automatically stop, terminate, reboot, or recover your Amazon EC2 instances. You can use the stop or terminate actions when you no longer need an instance to be running. You can use the reboot and recover actions to automatically reboot the instance.

In this example, Go code is used to define an alarm action in CloudWatch that triggers the reboot of an Amazon EC2 instance. The code uses the AWS SDK for Go to manage instances by using these methods of PutMetricAlarm type:

Prerequisites

Create and Enable Actions on an Alarm

Create a new Go file named create_enable_alarms.go.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

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

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials, and create a new Amazon EC2 service client.

Copy
func main() { // Load session from shared config. sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create new cloudwatch client. svc := cloudwatch.New(sess)

Create a metric alarm that will reboot an instance if its CPU utilization is greater than 70 percent.

Copy
_, err := svc.PutMetricAlarm(&cloudwatch.PutMetricAlarmInput{ AlarmName: &os.Args[3], ComparisonOperator: aws.String(cloudwatch.ComparisonOperatorGreaterThanThreshold), EvaluationPeriods: aws.Int64(1), MetricName: aws.String("CPUUtilization"), Namespace: aws.String("AWS/EC2"), Period: aws.Int64(60), Statistic: aws.String(cloudwatch.StatisticAverage), Threshold: aws.Float64(70.0), ActionsEnabled: aws.Bool(true), AlarmDescription: aws.String("Alarm when server CPU exceeds 70%"), Unit: aws.String(cloudwatch.StandardUnitSeconds),

You can use one of the default workflow actions to reboot the instance if the alarm is triggered.

Copy
AlarmActions: []*string{ aws.String(fmt.Sprintf("arn:aws:swf:us-east-1:%s:action/actions/AWS_EC2.InstanceId.Reboot/1.0", os.Args[1])), }, Dimensions: []*cloudwatch.Dimension{ &cloudwatch.Dimension{ Name: aws.String("InstanceId"), Value: &os.Args[2], }, }, }) if err != nil { fmt.Println("Error", err) return }

Call EnableAlarmActions with the new alarm for the instance.

Copy
result, err := svc.EnableAlarmActions(&cloudwatch.EnableAlarmActionsInput{ AlarmNames: []*string{ &os.Args[3], }, }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Alarm action enabled", result) }

Disable Actions on an Alarm

Create a new Go file named disable_alarm.go.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

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

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials, and create a new Amazon EC2 service client.

Copy
func main() { // Load session from shared config. sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create new cloudwatch client. svc := cloudwatch.New(sess)

Call the DisableAlarmActions method to disable the actions for this alarm.

Copy
result, err := svc.DisableAlarmActions(&cloudwatch.DisableAlarmActionsInput{ AlarmNames: []*string{ &os.Args[1], }, }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success", result) }