-
-
Save vvalorous/a707b5942e0e1ab415c460823df13864 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
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
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