Skip to content

Instantly share code, notes, and snippets.

@mlapida
Last active April 29, 2022 08:10
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mlapida/622a73927ac927e947c2 to your computer and use it in GitHub Desktop.
Save mlapida/622a73927ac927e947c2 to your computer and use it in GitHub Desktop.
Generate a report of orphaned EBS volumes and send an SNS. A full writeup can be found on my site http://mlapida.com/thoughts/lambda-tracking-orphaned-ebs-volumes
import boto3
import logging
from datetime import *
#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.WARNING)
#define the connection
ec2 = boto3.resource('ec2', region_name="us-west-2")
sns = boto3.resource('sns')
platform_endpoint = sns.PlatformEndpoint('[your SNS arn here]')
#set the date to today for the snapshot
today = datetime.now().date()
def lambda_handler(event, context):
#collect all volumes in a region
volumes = ec2.volumes.all()
#report header
missingReport = "The Following Volumes are Orphaned: \n"
x = 0
#loop through by volumes
for vol in volumes:
if vol.state == "available":
missingReport = missingReport + "- " + str(vol.id) + " - Size: " + str(vol.size) + " - Created: " + str(vol.create_time) + "\n"
x= x + 1
#only send a report if there are orphanes
if x == 0:
print "Nothing to Report"
else:
response = platform_endpoint.publish(
Message=missingReport,
Subject='Orphaned Volume Report: ' + str(today),
MessageStructure='string',
)
print missingReport
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment