Script - Unused EBS & EIP
__________
Unused EBS Volume
_________
Unused EBS Volume
_________
import boto3
import logging
from datetime import *
#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.WARNING)
### Variables Declared
regions = ['us-east-1', 'us-east-2', 'us-west-1','us-west-2', 'ap-south-1', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'eu-central-1', 'eu-west-1', 'sa-east-1']
Account_Name = ['Worxogoprod']
AccessID = ['AKIAJRBAJGZKMR36Y6UA']
SecretID = ['UoO+4574nDnqq57zlVCAYy+w0IOMur5vw4fWnvxy']
sns = boto3.resource('sns')
platform_endpoint = sns.PlatformEndpoint('arn:aws:sns:ap-southeast-1:316720341464:ELB-unused-alerts')
#set the date to today for the snapshot
today = datetime.now().date()
missingReport=" "
def lambda_handler(event, context):
missingReport=" "
for i,j,k in zip(AccessID, SecretID, Account_Name):
missingReport = missingReport + "\n" + "**************** " + str(k) + " ***************" + "\n \n"
y = 0
missingReport = missingReport + ""
for region in regions:
ec2 = boto3.resource('ec2',region_name=region,aws_access_key_id=i,aws_secret_access_key=j)
missingReport = missingReport + "The Following Volumes are unused and are in AVAILABLE state %s: \n" %(region)
volumes=ec2.volumes.all()
x = 0
for vol in volumes:
if vol.state == "available":
missingReport = missingReport + "-------------- Volume ID: " + str(vol.id) + " Volume Size: " + str(vol.size) + " Volume Created: " + str(vol.create_time) + "\n"
#x= x + 1
# if x >= 1:
# missingReport = missingReport + "\n"
#else:
#missingReport = missingReport + "___________No Volumes Found__________"
response = platform_endpoint.publish(
Message=missingReport,
Subject='Worxogo EBS Unused Volume Report: ' + str(today),
MessageStructure='string',
)
print missingReport
________________
EIP
________________
import boto3
import logging
from datetime import *
#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.WARNING)
### Variables Declared
regions = ['us-east-1', 'us-east-2', 'us-west-1','us-west-2', 'ap-south-1', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'eu-central-1', 'eu-west-1', 'sa-east-1']
Account_Name = ['connectandsell']
AccessID = ['AKIAILQCWDHAMVFF3YRA']
SecretID = ['QGW8sslhyqsWMZKPCOlAcLYoGFtiLT2BF0fy3HPE']
sns = boto3.resource('sns')
platform_endpoint = sns.PlatformEndpoint('arn:aws:sns:us-west-2:936023071971:unusedip')
#set the date to today for the snapshot
today = datetime.now().date()
missingReport=" "
unused_eips=[]
def lambda_handler(event, context):
missingReport=" "
unused_eips=[]
for i,j,k in zip(AccessID, SecretID, Account_Name):
missingReport = missingReport + "\n" + "**************** " + str(k) + " ***************" + "\n \n"
y = 0
missingReport = missingReport + ""
for region in regions:
ec2 = boto3.resource('ec2',region_name=region,aws_access_key_id=i,aws_secret_access_key=j)
client = boto3.client('ec2',region_name=region,aws_access_key_id=i,aws_secret_access_key=j)
filters = [{'Name': 'domain', 'Values': ['vpc']}]
response = client.describe_addresses(Filters=filters)
missingReport = missingReport + "Following EIP's are not associated with any instance in %s: \n" %(region)
#volumes=ec2.volumes.all()
x = 0
for eips in response['Addresses']:
try:
eips['InstanceId']
except Exception, e:
unused_eips.append(eips['PublicIp'])
missingReport = missingReport + "-------------- EIP:" + str(unused_eips) + "\n"
#print "Unused Elasitic Ips:"
#print unused_eips
response = platform_endpoint.publish(
Message=missingReport,
Subject='Unused EIP Report: ' + str(today),
MessageStructure='string',
)
print missingReport
Comments
Post a Comment