-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Chore] Added Cleanup for Launch Configurations (#1335)
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 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
59 changes: 59 additions & 0 deletions
59
tool/clean/clean_launch_configuration/clean_launch_configuration.go
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,59 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/autoscaling" | ||
|
||
"github.com/aws/amazon-cloudwatch-agent/tool/clean" | ||
) | ||
|
||
func main() { | ||
err := cleanLaunchConfiguration() | ||
if err != nil { | ||
log.Fatalf("errors cleaning %v", err) | ||
} | ||
} | ||
func cleanLaunchConfiguration() error { | ||
expirationDate := time.Now().UTC().Add(clean.KeepDurationSixtyDay) | ||
ctx := context.Background() | ||
defaultConfig, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
autoScalingClient := autoscaling.NewFromConfig(defaultConfig) | ||
describeLaunchConfigurationsInput := autoscaling.DescribeLaunchConfigurationsInput{} | ||
launchConfigOut, err := autoScalingClient.DescribeLaunchConfigurations(ctx, &describeLaunchConfigurationsInput) | ||
if err != nil { | ||
return err | ||
} | ||
if len(launchConfigOut.LaunchConfigurations) == 0 { | ||
return errors.New("no launch configuration found") | ||
} | ||
log.Printf("Found %d launch configurations", len(launchConfigOut.LaunchConfigurations)) | ||
for _, launchConfig := range launchConfigOut.LaunchConfigurations { | ||
log.Printf("Found %s with creation date: %v", *launchConfig.LaunchConfigurationName, *launchConfig.CreatedTime) | ||
// if not cwagent-integ-test ignore it | ||
if !strings.Contains(*launchConfig.LaunchConfigurationName, "integ") { | ||
continue | ||
} | ||
if expirationDate.After(*launchConfig.CreatedTime) { | ||
log.Printf("Try to delete %s", *launchConfig.LaunchConfigurationName) | ||
_, err := autoScalingClient.DeleteLaunchConfiguration(ctx, &autoscaling.DeleteLaunchConfigurationInput{ | ||
LaunchConfigurationName: launchConfig.LaunchConfigurationName, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("Succesfully deleted %s", *launchConfig.LaunchConfigurationName) | ||
} | ||
} | ||
return nil | ||
} |