-
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.
Cleaner for unused old EBS Volumes (#1065)
- Loading branch information
Showing
2 changed files
with
86 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
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,68 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
|
||
"github.com/aws/amazon-cloudwatch-agent/tool/clean" | ||
) | ||
|
||
// Clean ebs volumes if they have been open longer than 7 day and unused | ||
func main() { | ||
err := cleanVolumes() | ||
if err != nil { | ||
log.Fatalf("errors cleaning %v", err) | ||
} | ||
} | ||
|
||
func cleanVolumes() error { | ||
log.Print("Begin to clean EBS Volumes") | ||
ctx := context.Background() | ||
defaultConfig, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
ec2Client := ec2.NewFromConfig(defaultConfig) | ||
|
||
return deleteUnusedVolumes(ctx, ec2Client) | ||
|
||
} | ||
|
||
func deleteUnusedVolumes(ctx context.Context, client *ec2.Client) error { | ||
|
||
input := &ec2.DescribeVolumesInput{ | ||
Filters: []types.Filter{ | ||
{ | ||
//if the status is availble, then EBS volume is not currently attached to any ec2 instance (so not being used) | ||
Name: aws.String("status"), | ||
Values: []string{"available"}, | ||
}, | ||
}, | ||
} | ||
|
||
volumes, err := client.DescribeVolumes(ctx, input) | ||
if err != nil { | ||
return err | ||
} | ||
for _, volume := range volumes.Volumes { | ||
if time.Since(*volume.CreateTime) > clean.KeepDurationOneWeek && len(volume.Attachments) == 0 { | ||
log.Printf("Deleting unused volume %s", *volume.VolumeId) | ||
_, err = client.DeleteVolume(ctx, &ec2.DeleteVolumeInput{ | ||
VolumeId: volume.VolumeId, | ||
}) | ||
} | ||
if err != nil { | ||
log.Printf("Error deleting volume %s: %v", *volume.VolumeId, err) | ||
} | ||
} | ||
return nil | ||
} |