Created
February 28, 2023 03:28
-
-
Save niski84/4e6cf88300b6e4738ba868467dbe4b93 to your computer and use it in GitHub Desktop.
Nicholas Skitch, the The AWS Budget Bandit, Strikes Again! AWS Suspend and Resume ElasticBeanstalk EB
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 main | |
import ( | |
"fmt" | |
"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 | |
} | |
// PauseEnvironment pauses an Elastic Beanstalk environment | |
func PauseEnvironment(instance Instance, envName string) error { | |
// Create Elastic Beanstalk client | |
svc := elasticbeanstalk.New(instance.AwsSession) | |
// Pause the environment | |
params := &elasticbeanstalk.UpdateEnvironmentInput{ | |
EnvironmentName: aws.String(envName), | |
EnvironmentStatus: aws.String("Paused"), | |
} | |
_, err := svc.UpdateEnvironment(params) | |
if err != nil { | |
return fmt.Errorf("failed to pause environment: %v", err) | |
} | |
return nil | |
} | |
// ResumeEnvironment resumes an Elastic Beanstalk environment | |
func ResumeEnvironment(instance Instance, envName string) error { | |
// Create Elastic Beanstalk client | |
svc := elasticbeanstalk.New(instance.AwsSession) | |
// Resume the environment | |
params := &elasticbeanstalk.UpdateEnvironmentInput{ | |
EnvironmentName: aws.String(envName), | |
EnvironmentStatus: aws.String("Ready"), | |
} | |
_, err := svc.UpdateEnvironment(params) | |
if err != nil { | |
return fmt.Errorf("failed to resume environment: %v", err) | |
} | |
return nil | |
} | |
func main() { | |
// Create a new AWS session | |
sess := session.Must(session.NewSession(&aws.Config{ | |
Region: aws.String("us-west-2"), | |
})) | |
// Create an instance of the struct | |
instance := Instance{AwsSession: sess} | |
// Test the PauseEnvironment function | |
err := PauseEnvironment(instance, "my-environment") | |
if err != nil { | |
fmt.Println("Failed to pause environment:", err) | |
} else { | |
fmt.Println("Environment paused successfully") | |
} | |
// Test the ResumeEnvironment function | |
err = ResumeEnvironment(instance, "my-environment") | |
if err != nil { | |
fmt.Println("Failed to resume environment:", err) | |
} else { | |
fmt.Println("Environment resumed successfully") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment