-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db33a77
commit 22a507d
Showing
1 changed file
with
17 additions
and
29 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
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) | ||
|
||
# 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") |