Skip to content

Commit

Permalink
updated lambda fn implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremycook123 committed Dec 18, 2023
1 parent db33a77 commit 22a507d
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
import boto3
import logging

ec2 = boto3.client('ec2')
ec2 = boto3.resource('ec2')

logger = logging.getLogger()
logger.setLevel(logging.INFO)

# sns_client = boto3.client('sns')
volumes = ec2.describe_volumes()

def lambda_handler(event, context):
unused_volumes = []
for vol in volumes['Volumes']:
if len(vol['Attachments']) == 0:
vol1 = ("-----Unused Volume ID = {}------".format(vol['VolumeId']))
unused_volumes.append(vol1)

#email
# sns_client.publish(
# TopicArn='<SNS Topic ARN>',
# Subject='Warning - Unused Volume List',
# Message=str(unused_volumes)
# )

print("EBS vols to delete:")
for vol in unused_volumes:
print(vol)

return {
"statusCode": 200,
"isBase64Encoded": False,
"headers": {
"Content-Type": "application/json"
},
"body": {"unused_volumes": unused_volumes}
}
logger.info("identifying unattached volumes...")

vol_filter = [
{'Name':'tag:AutoDelete', 'Values':['True']},
{'Name': 'status','Values': ['available']}
]

volumes = [v for v in ec2.volumes.filter(Filters=vol_filter)]

logger.info(f"volumes unattached: {volumes}")

for vol in volumes:
logger.info(f"deleting volume: {vol.id}")
vol.delete()

logger.info("unattached volumes deleted")

0 comments on commit 22a507d

Please sign in to comment.