Created
October 18, 2022 17:05
-
-
Save Aminechakr/5dd9da6eff7c7a55f800b92dba06978e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
import boto3 | |
sess = boto3.Session( | |
region_name="us-west-2", | |
) | |
ec2 = sess.resource('ec2') | |
volumes = ec2.volumes.all() | |
to_terminate=[] | |
def lambda_handler(event, context): | |
for volume in volumes: | |
print('Evaluating volume {0}'.format(volume.id)) | |
print('The number of attachments for this volume is {0}'.format(len(volume.attachments))) | |
# Here's where you might add other business logic for deletion criteria | |
if len(volume.attachments) == 0: | |
to_terminate.append(volume) | |
if len(to_terminate) == 0: | |
print ("No volumes to terminate! Exiting.") | |
exit() | |
for volume in to_terminate: | |
print('Deleting volume {0}'.format(volume.id)) | |
volume.delete() |
Author
Aminechakr
commented
Oct 18, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment