Created
February 28, 2023 20:11
-
-
Save niski84/39745e0086efaca0102a105522994138 to your computer and use it in GitHub Desktop.
AWS ASG TimeBasedScaling
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
package awsapi | |
import ( | |
"fmt" | |
"time" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/elasticbeanstalk" | |
) | |
type Instance struct { | |
AwsSession *session.Session | |
} | |
// TimeBasedScaling changes the Elastic Beanstalk configuration to use time-based scaling | |
func TimeBasedScaling(instance Instance, envName string, capacity int64, occurrence string, startTime time.Time) error { | |
// Create Elastic Beanstalk client | |
svc := elasticbeanstalk.New(instance.AwsSession) | |
// Set the time-based scaling configuration | |
params := &elasticbeanstalk.UpdateEnvironmentRequest{ | |
EnvironmentName: aws.String(envName), | |
OptionSettings: []*elasticbeanstalk.ConfigurationOptionSetting{ | |
{ | |
Namespace: aws.String("aws:autoscaling:asg"), | |
OptionName: aws.String("MinSize"), | |
Value: aws.String("1"), | |
}, | |
{ | |
Namespace: aws.String("aws:autoscaling:asg"), | |
OptionName: aws.String("MaxSize"), | |
Value: aws.String(fmt.Sprintf("%d", capacity)), | |
}, | |
{ | |
Namespace: aws.String("aws:autoscaling:scheduledaction"), | |
OptionName: aws.String("MinSize"), | |
Value: aws.String("1"), | |
}, | |
{ | |
Namespace: aws.String("aws:autoscaling:scheduledaction"), | |
OptionName: aws.String("MaxSize"), | |
Value: aws.String(fmt.Sprintf("%d", capacity)), | |
}, | |
{ | |
Namespace: aws.String("aws:autoscaling:scheduledaction"), | |
OptionName: aws.String("ScheduledActionTimeZone"), | |
Value: aws.String("UTC"), | |
}, | |
{ | |
Namespace: aws.String("aws:autoscaling:scheduledaction"), | |
OptionName: aws.String("ScheduledActionStartTimestamp"), | |
Value: aws.String(startTime.Format(time.RFC3339)), | |
}, | |
{ | |
Namespace: aws.String("aws:autoscaling:scheduledaction"), | |
OptionName: aws.String("ScheduledActionRecurrence"), | |
Value: aws.String(occurrence), | |
}, | |
}, | |
} | |
_, err := svc.UpdateEnvironment(params) | |
if err != nil { | |
return fmt.Errorf("failed to set Elastic Beanstalk configuration to use time-based scaling: %v", err) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment