DeleteAlarm.go - AWS Code Sample

DeleteAlarm.go

/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package main import ( "flag" "fmt" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudwatch" ) // DeleteAlarm deletes an alarm // Inputs: // sess is the current session, which provides configuration for the SDK's service clients // alarmName is the name of the alarm // Output: // If successful, nil // Otherwise, the error from a call to DeleteAlarms func DeleteAlarm(sess *session.Session, alarmName *string) error { // Create service client svc := cloudwatch.New(sess) _, err := svc.DeleteAlarms(&cloudwatch.DeleteAlarmsInput{ AlarmNames: []*string{ alarmName, }, }) if err != nil { return err } return nil } func main() { alarmName := flag.String("a", "", "The name of the alarm to delete") flag.Parse() if *alarmName == "" { fmt.Println("You must supply an alarm name to disable") return } // Initialize a session that the SDK uses to load // credentials from the shared credentials file ~/.aws/credentials // and configuration from the shared configuration file ~/.aws/config. sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) err := DeleteAlarm(sess, alarmName) if err != nil { fmt.Println("Could not delete alarm " + *alarmName) } else { fmt.Println("Deleted alarm " + *alarmName) } }