From 715f975ecee9b10d04b79f20288f49806c882b4b Mon Sep 17 00:00:00 2001 From: Narthana Epa Date: Fri, 31 Mar 2023 19:57:31 +1100 Subject: [PATCH] Add a parameter to the sam stack and rename variables after it --- lambda/main.go | 15 +++++++++------ scaler/asg.go | 10 +++++----- template.yaml | 8 +++++++- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lambda/main.go b/lambda/main.go index 796e778..b0a2743 100644 --- a/lambda/main.go +++ b/lambda/main.go @@ -133,9 +133,12 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) { } } - maxPages := 0 - if v := os.Getenv("MAX_PAGES"); v != "" { - maxPages, _ = strconv.Atoi(v) + maxDescribeScalingActivityPages := 0 + if v := os.Getenv("MAX_DESCRIBE_SCALING_ACTIVITIES_PAGES"); v != "" { + maxDescribeScalingActivityPages, err = strconv.Atoi(v) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to parse MAX_DESCRIBE_SCALING_ACTIVITIES_PAGES: %v", err) + } } var mustGetEnv = func(env string) string { @@ -160,9 +163,9 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) { // set last scale in and out from asg's activities asg := &scaler.ASGDriver{ - Name: mustGetEnv(`ASG_NAME`), - Sess: sess, - MaxPages: maxPages, + Name: mustGetEnv(`ASG_NAME`), + Sess: sess, + MaxDescribeScalingActivityPages: maxDescribeScalingActivityPages, } c1 := make(chan LastScaleASGResult, 1) diff --git a/scaler/asg.go b/scaler/asg.go index 80c63bb..f0d7bdf 100644 --- a/scaler/asg.go +++ b/scaler/asg.go @@ -24,9 +24,9 @@ type AutoscaleGroupDetails struct { } type ASGDriver struct { - Name string - Sess *session.Session - MaxPages int + Name string + Sess *session.Session + MaxDescribeScalingActivityPages int } func (a *ASGDriver) Describe() (AutoscaleGroupDetails, error) { @@ -104,8 +104,8 @@ func (a *ASGDriver) GetLastScalingInAndOutActivity() (*autoscaling.Activity, *au var lastScalingInActivity *autoscaling.Activity hasFoundScalingActivities := false for i := 0; !hasFoundScalingActivities; i++ { - if a.MaxPages > 0 && i > a.MaxPages { - return nil, nil, fmt.Errorf("%d exceedes allowed pages for autoscaling:DescribeScalingActivities, %d", i, a.MaxPages) + if a.MaxDescribeScalingActivityPages > 0 && i > a.MaxDescribeScalingActivityPages { + return nil, nil, fmt.Errorf("%d exceedes allowed pages for autoscaling:DescribeScalingActivities, %d", i, a.MaxDescribeScalingActivityPages) } output, err := a.GetAutoscalingActivities(nextToken) diff --git a/template.yaml b/template.yaml index e96d86d..07204a8 100644 --- a/template.yaml +++ b/template.yaml @@ -68,7 +68,7 @@ Parameters: - "true" - "false" Default: "true" - + RolePermissionsBoundaryARN: Type: String Description: The ARN of the policy used to set the permissions boundary for the role. @@ -79,6 +79,11 @@ Parameters: Description: The number of days to retain the Cloudwatch Logs of the lambda. Default: 1 + MaxDescribeScalingActivityPages: + Type: Number + Description: The number of pages to retrive for DescribeScalingActivity. 0 means unlimited. + Default: 0 + Conditions: CreateRole: !Equals [ !Ref AutoscalingLambdaExecutionRole, '' ] @@ -192,6 +197,7 @@ Resources: INCLUDE_WAITING: !Ref ScaleOutForWaitingJobs LAMBDA_TIMEOUT: "50s" LAMBDA_INTERVAL: "10s" + MAX_DESCRIBE_SCALING_ACTIVITY_PAGES: !Ref MaxDescribeScalingActivityPages Events: Timer: Type: Schedule