-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the option to add cloudwatch alarms on errors for lambdas
- Loading branch information
Showing
7 changed files
with
210 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/omerh/awsctl/pkg/helpers" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var getCloudwatchAlarmCmd = &cobra.Command{ | ||
Use: "cloudwatchalarm", | ||
Short: "Get cloudwatch alarms", | ||
Run: func(cmd *cobra.Command, Args []string) { | ||
region, _ := cmd.Flags().GetString("region") | ||
// out, _ := cmd.Flags().GetString("out") | ||
|
||
if region == "all" { | ||
awsRegions, _ := helpers.GetAllAwsRegions() | ||
for _, r := range awsRegions { | ||
alarms := helpers.ListCloudwatchAlarms(r) | ||
fmt.Println(alarms) | ||
} | ||
return | ||
} | ||
|
||
if region == "" { | ||
region = helpers.GetDefaultAwsRegion() | ||
} | ||
|
||
alarms := helpers.ListCloudwatchAlarms(region) | ||
fmt.Println(alarms) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/cloudwatch" | ||
) | ||
|
||
// ListCloudwatchAlarms list all cloudwatch alarms | ||
func ListCloudwatchAlarms(region string) []*cloudwatch.MetricAlarm { | ||
awsSession, _ := InitAwsSession(region) | ||
svc := cloudwatch.New(awsSession) | ||
input := &cloudwatch.DescribeAlarmsInput{} | ||
result, _ := svc.DescribeAlarms(input) | ||
cloudwatchAlarms := result.MetricAlarms | ||
|
||
for result.NextToken != nil { | ||
input = &cloudwatch.DescribeAlarmsInput{ | ||
NextToken: result.NextToken, | ||
} | ||
result, _ = svc.DescribeAlarms(input) | ||
for _, c := range result.MetricAlarms { | ||
cloudwatchAlarms = append(cloudwatchAlarms, c) | ||
} | ||
} | ||
|
||
return cloudwatchAlarms | ||
} | ||
|
||
// CreateLambdaCloudwatchAlarm will create a new alarm for cloudwatch | ||
func CreateLambdaCloudwatchAlarm(region string, functionName string, metricName string, namespace string, threshold float64, action string) bool { | ||
awsSession, _ := InitAwsSession(region) | ||
svc := cloudwatch.New(awsSession) | ||
input := &cloudwatch.PutMetricAlarmInput{ | ||
AlarmName: aws.String(fmt.Sprintf("%v on %v", metricName, functionName)), | ||
ComparisonOperator: aws.String(cloudwatch.ComparisonOperatorGreaterThanOrEqualToThreshold), | ||
EvaluationPeriods: aws.Int64(1), | ||
MetricName: aws.String(metricName), | ||
Namespace: aws.String(namespace), | ||
Period: aws.Int64(60), | ||
Statistic: aws.String(cloudwatch.StatisticSum), | ||
Threshold: aws.Float64(threshold), | ||
ActionsEnabled: aws.Bool(true), | ||
AlarmDescription: aws.String(fmt.Sprintf("%v on %v greater than %v", metricName, functionName, threshold)), | ||
|
||
Dimensions: []*cloudwatch.Dimension{ | ||
{ | ||
Name: aws.String("FunctionName"), | ||
Value: aws.String(functionName), | ||
}, | ||
}, | ||
|
||
AlarmActions: []*string{ | ||
aws.String(action), | ||
}, | ||
} | ||
|
||
// Debug input | ||
// fmt.Println(input) | ||
|
||
_, err := svc.PutMetricAlarm(input) | ||
if err != nil { | ||
fmt.Println("Error", err) | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters