Skip to content

Commit

Permalink
[Chore] Added Cleanup for Launch Configurations (#1335)
Browse files Browse the repository at this point in the history
  • Loading branch information
okankoAMZ authored Sep 19, 2024
1 parent a42b661 commit 8543462
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/clean-aws-resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,24 @@ jobs:
working-directory: tool/clean
run: go run ./clean_auto_scaling_groups/clean_auto_scaling_groups.go --tags=clean

clean-launch-configs:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ secrets.TERRAFORM_AWS_ASSUME_ROLE }}
aws-region: us-west-2

- name: Clean old launch configuration
working-directory: tool/clean
run: go run ./clean_launch_configuration/clean_launch_configuration.go --tags=clean
clean-iam-roles:
runs-on: ubuntu-latest
permissions:
Expand Down
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
}

0 comments on commit 8543462

Please sign in to comment.