Scripts - AMI & Snapshot
_________
AMI Status Check
_________
__________
AMI_Daily_Backup
__________
*********************************************************************************
_________
AMI_deletion
_________
*********************************************************************************
__________
Backup snapshot
__________
_________
Deleat Snapshot
_________
AMI Status Check
_________
#!/bin/bash
dataset_date=`date`
EX_DAY=`date -d "$dataset_date 0 days" +%d-%m-%G`
result=`aws ec2 describe-images --owners self --query 'Images[].[Name,ImageId,CreationDate]' --output table | grep "$EX_DAY"`
count=`aws ec2 describe-images --owners self --query 'Images[].[Name,ImageId,CreationDate]' --output text | grep "$EX_DAY" | wc -l`
total=`aws ec2 describe-instances --filter Name=tag:backup,Values=yes --output text --query 'Reservations[*].Instances[*].InstanceId' | wc -l`
cat <<EOT >> mail.txt
=========================
Region - (Mention Region)
=========================
Total number of AMI's to be taken : $total
The number of AMI's taken : $count
==========================================
The list of AMI's are
$result
EOT
if [ $count -eq $total ]
then
echo "Today's AMI List successfull!"
else
cat mail.txt | mailx -s "Today's AMI List Failed!!!" mail id
fi
rm -rf mail.txt
echo "File removed successfully"
**************************************************************__________
AMI_Daily_Backup
__________
# script will search for all instances having a tag with "backup" or "ami" on it
# Replace your Region to take ami
# Replace your Retention value
import boto3
import collections
import datetime
ec = boto3.client('ec2','eu-west-1') #mention your region to backup
def lambda_handler(event, context):
reservations = ec.describe_instances(
Filters=[
{'Name': 'tag-key', 'Values': ['backup', 'ami']},
]
).get(
'Reservations', []
)
instances = sum(
[
[i for i in r['Instances']]
for r in reservations
], [])
print "Found %d instances that need backing up" % len(instances)
to_tag = collections.defaultdict(list)
for instance in instances:
try:
retention_days = [
int(t.get('Value')) for t in instance['Tags']
if t['Key'] == 'Retention'][0]
except IndexError:
retention_days = 7 #mention Retention day
create_time = datetime.datetime.now()
create_fmt = create_time.strftime('%d-%m-%Y')
create_tm = create_time.strftime('%H.%M.%S')
for name in instance['Tags']:
Instancename= name['Value']
key_fil= name['Key']
if key_fil == 'Name' :
AMIid = ec.create_image(InstanceId=instance['InstanceId'], Name="Lambda - " + instance['InstanceId'] + " ("+ Instancename + ") at " +create_tm+ " From " + create_fmt, Description="Lambda created AMI of instance " + instance['InstanceId'], NoReboot=True, DryRun=False)
to_tag[retention_days].append(AMIid['ImageId'])
print "Retaining AMI %s of instance %s for %d days" % (
AMIid['ImageId'],
instance['InstanceId'],
retention_days,
)
for retention_days in to_tag.keys():
delete_date = datetime.date.today() + datetime.timedelta(days=retention_days)
delete_fmt = delete_date.strftime('%d-%m-%Y')
print "Will delete %d AMIs on %s" % (len(to_tag[retention_days]), delete_fmt)
ec.create_tags(
Resources=to_tag[retention_days],
Tags=[
{'Key': 'DeleteOn', 'Value': delete_fmt},
]
)
*********************************************************************************
_________
AMI_deletion
_________
# This script will search for all instances having a tag with "backup" or "ami" on it.
# Then script will check latest ami is available. if found it will deregister old ami and remove all the snapshots associated with ami
# Replace your Account ID
import boto3
import collections
import datetime
import time
import sys
ec = boto3.client('ec2','eu-west-1') #mention your region to delete
ec2 = boto3.resource('ec2','eu-west-1') #mention your region to delete
images = ec2.images.filter(Owners=["305241987619"]) # replace your Acc ID
def lambda_handler(event, context):
reservations = ec.describe_instances(
Filters=[
{'Name': 'tag-key', 'Values': ['backup', 'ami']},
]
).get(
'Reservations', []
)
instances = sum(
[
[i for i in r['Instances']]
for r in reservations
], [])
print "Found %d instances that need evaluated" % len(instances)
to_tag = collections.defaultdict(list)
date = datetime.datetime.now()
date_fmt = date.strftime('%d-%m-%Y')
imagesList = []
# Set to true once we confirm we have a backup taken today
backupSuccess = False
# Loop through all of our instances with a tag named "Backup"
for instance in instances:
imagecount = 0
# Loop through each image of our current instance
for image in images:
# Our other Lambda Function names its AMIs Lambda - i-instancenumber.
# We now know these images are auto created
if image.name.startswith('Lambda - ' + instance['InstanceId']):
# print "FOUND IMAGE " + image.id + " FOR INSTANCE " + instance['InstanceId']
# Count this image's occcurance
imagecount = imagecount + 1
try:
if image.tags is not None:
deletion_date = [
t.get('Value') for t in image.tags
if t['Key'] == 'DeleteOn'][0]
delete_date = time.strptime(deletion_date, "%d-%m-%Y")
except IndexError:
deletion_date = False
delete_date = False
today_time = datetime.datetime.now().strftime('%d-%m-%Y')
# today_fmt = today_time.strftime('%d-%m-%Y')
today_date = time.strptime(today_time, '%d-%m-%Y')
# If image's DeleteOn date is less than or equal to today,
# add this image to our list of images to process later
if delete_date <= today_date:
imagesList.append(image.id)
# Make sure we have an AMI from today and mark backupSuccess as true
if image.name.endswith(date_fmt):
# Our latest backup from our other Lambda Function succeeded
backupSuccess = True
print "Latest backup from " + date_fmt + " was a success"
print "instance " + instance['InstanceId'] + " has " + str(imagecount) + " AMIs"
print "============="
print "About to process the following AMIs:"
print imagesList
if backupSuccess == True:
snapshots = ec.describe_snapshots(MaxResults=1000, OwnerIds=['305241987619'])['Snapshots'] # replace your Acc ID
# loop through list of image IDs
for image in imagesList:
print "deregistering image %s" % image
amiResponse = ec.deregister_image(
DryRun=False,
ImageId=image,
)
for snapshot in snapshots:
if snapshot['Description'].find(image) > 0:
snap = ec.delete_snapshot(SnapshotId=snapshot['SnapshotId'])
print "Deleting snapshot " + snapshot['SnapshotId']
print "-------------"
else:
print "No current backup found. Termination suspended."
*********************************************************************************
__________
Backup snapshot
__________
import boto3
import collections
import datetime
import re
#Please mention your region name
#below line code is call cross region
ec = boto3.client('ec2', region_name='us-east-1')
#begins lambda function
def lambda_handler(event, context):
# mention your tag values below example "Backup-snap"
reservations = ec.describe_instances(
Filters=[
{'Name': 'tag-key', 'Values': ['Backup-snapshot', 'True']},
]
).get(
'Reservations', []
)
instances = sum(
[
[i for i in r['Instances']]
for r in reservations
], [])
print "Number of the Instances : %d" % len(instances)
to_tag = collections.defaultdict(list)
for instance in instances:
try:
retention_days = [
int(t.get('Value')) for t in instance['Tags']
if t['Key'] == 'Retention'][0]
except IndexError:
# Please give your retention period day
retention_days = 7
for dev in instance['BlockDeviceMappings']:
if dev.get('Ebs', None) is None:
continue
vol_id = dev['Ebs']['VolumeId']
for name in instance['Tags']:
# To store the instance tag value
Instancename= name['Value']
# To store the instance key value
key= name['Key']
# Below the code is create Snapshot name as instance Name
if key == 'Name' :
ins_name = Instancename
print "Found EBS volume %s on instance %s" % (
vol_id, instance['InstanceId'])
#To get all the instance tags deatils
for name in instance['Tags']:
# To store the instance tag value
Instancename= name['Value']
# To store the instance key value
key= name['Key']
# Below the code is create Snapshot name as instance Name
if key == 'Name' :
snap = ec.create_snapshot(
VolumeId=vol_id,
Description=Instancename,
)
print "snap %s" %snap
to_tag[retention_days].append(snap['SnapshotId'])
print "Retaining snapshot %s of volume %s from instance %s for %d days" % (
snap['SnapshotId'],
vol_id,
instance['InstanceId'],
retention_days,
)
for retention_days in to_tag.keys():
delete_date = datetime.date.today() + datetime.timedelta(days=retention_days)
snap = snap['Description'] + str('_')
# Here to get current date
snapshot = snap + str(datetime.date.today())
# to mention the current date formet
*********************************************************************************_________
Deleat Snapshot
_________
import boto3
import re
import datetime
#Please mention your region name
#below line code is call cross region
ec = boto3.client('ec2', region_name='us-east-1')
iam = boto3.client('iam')
#begins lambda function
def lambda_handler(event, context):
account_ids = list()
try:
iam.get_user()
except Exception as e:
# use the exception message to get the account ID the function executes under
account_ids.append(re.search(r'(arn:aws:sts::)([0-9]+)', str(e)).groups()[1])
delete_on = datetime.date.today().strftime('%Y-%m-%d')
'''filters = [
{'Name': 'tag-key', 'Values': ['DeleteOn']},
{'Name': 'tag-value', 'Values': [delete_on]},
]
snapshot_response = ec.describe_snapshots(OwnerIds=account_ids, Filters=filters)
for snap in snapshot_response['Snapshots']:
print "Deleting snapshot %s" % snap['SnapshotId']
ec.delete_snapshot(SnapshotId=snap['SnapshotId'])'''
Comments
Post a Comment