Skip to content

Commit

Permalink
Add a parameter to the sam stack and rename variables after it
Browse files Browse the repository at this point in the history
  • Loading branch information
triarius committed Mar 31, 2023
1 parent 5776d24 commit 715f975
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
15 changes: 9 additions & 6 deletions lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions scaler/asg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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, '' ]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 715f975

Please sign in to comment.